1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
require File.dirname(__FILE__) + "/../spec_helper"
require 'tempfile'
describe "Ruby IO" do
let(:input_number){"1234567890"}
it "gets an IO from a java.io.InputStream" do
io = java.io.ByteArrayInputStream.new(input_number.to_java_bytes).to_io
io.class.should == IO
io.read(5).should == "12345"
end
it "gets an IO from a java.io.OutputStream" do
output = java.io.ByteArrayOutputStream.new
io = output.to_io
io.class.should == IO
io.write("12345")
io.flush
String.from_java_bytes(output.to_byte_array).should == "12345"
end
it "is coercible to java.io.InputStream with IO#to_inputstream" do
file = File.open(__FILE__)
first_ten = file.read(10)
file.seek(0)
stream = file.to_inputstream
java.io.InputStream.should === stream
bytes = "0000000000".to_java_bytes
stream.read(bytes).should == 10
String.from_java_bytes(bytes).should == first_ten
end
it "is coercible to java.io.OutputStream with IO#to_outputstream" do
file = Tempfile.new("io_spec")
stream = file.to_outputstream
java.io.OutputStream.should === stream
bytes = input_number.to_java_bytes
stream.write(bytes)
stream.flush
file.seek(0)
str = file.read(10)
str.should == String.from_java_bytes(bytes)
end
it "gets an IO from a java.nio.channels.Channel" do
input = java.io.ByteArrayInputStream.new(input_number.to_java_bytes)
channel = java.nio.channels.Channels.newChannel(input)
io = channel.to_io
io.class.should == IO
io.read(5).should == "12345"
output = java.io.ByteArrayOutputStream.new
channel = java.nio.channels.Channels.newChannel(output)
io = channel.to_io
io.class.should == IO
io.write("12345")
io.flush
String.from_java_bytes(output.to_byte_array).should == "12345"
end
it "is coercible to java.nio.channels.Channel with IO#to_channel" do
file = Tempfile.new("io_spec")
channel = file.to_channel
java.nio.channels.Channel.should === channel
bytes = java.nio.ByteBuffer.wrap(input_number.to_java_bytes)
channel.write(bytes)
file.seek(0)
str = file.read(10)
str.should == String.from_java_bytes(bytes.array)
end
end
|