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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
require File.dirname(__FILE__) + "/../spec_helper"
describe "Ruby IO" do
before(:all) { require 'tempfile' }
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
expect(io.class).to eq(IO)
expect(io.read(5)).to eq("12345")
end
it "rewinds a java.io.ByteArrayInputStream" do
io = java.io.ByteArrayInputStream.new(input_number.to_java_bytes).to_io
expect(io.class).to eq(IO)
expect(io.read(5)).to eq("12345")
expect(io.read(5)).to eq("67890")
io.rewind
expect(io.read(5)).to eq("12345")
end
it "seeks a java.io.ByteArrayInputStream" do
bytes = input_number.to_java_bytes
io = java.io.ByteArrayInputStream.new(input_number.to_java_bytes, 3, 6).to_io
expect(io.class).to eq(IO)
expect(io.read(4)).to eq("4567")
expect(io.read(4)).to eq("89")
io.seek 0
expect(io.read(5)).to eq("45678")
io.seek 4
expect(io.read(4)).to eq("89")
io.seek 9
expect(io.read(2)).to eq(nil)
end
it "gets an IO from a java.io.OutputStream" do
output = java.io.ByteArrayOutputStream.new
io = output.to_io
expect(io.class).to eq(IO)
io.write("12345")
io.flush
expect(String.from_java_bytes(output.to_byte_array)).to eq("12345")
end
it "is coercible to java.io.InputStream with IO#to_input_stream" do
file = File.open(__FILE__)
first_ten = file.read(10)
file.seek(0)
stream = file.to_input_stream
expect(java.io.InputStream).to be === stream
bytes = "0000000000".to_java_bytes
expect(stream.read(bytes)).to eq(10)
expect(String.from_java_bytes(bytes)).to eq(first_ten)
expect(java.io.InputStream).to be === file.to_inputstream # old-naming
end
it "is coercible using to_java to java.io.InputStream" do
file = File.open(__FILE__)
first_ten = file.read(10)
file.seek(0)
stream = file.to_java java.io.InputStream
expect(java.io.InputStream).to be === stream
bytes = "0000000000".to_java_bytes
expect(stream.read(bytes)).to eq(10)
expect(String.from_java_bytes(bytes)).to eq(first_ten)
end
it "is coercible to java.io.OutputStream with IO#to_output_stream" do
file = Tempfile.new("io_spec")
stream = file.to_output_stream
expect(java.io.OutputStream).to be === stream
bytes = input_number.to_java_bytes
stream.write(bytes)
stream.flush
file.seek(0)
str = file.read(10)
expect(str).to eq(String.from_java_bytes(bytes))
expect(java.io.OutputStream).to be === file.to_outputstream # old-naming
end
it "is coercible using to_java to java.io.OutputStream" do
file = Tempfile.new("io_spec")
stream = file.to_java 'java.io.OutputStream'
expect(java.io.OutputStream).to be === stream
bytes = input_number.to_java_bytes
stream.write(bytes)
stream.flush
file.seek(0)
str = file.read(10)
expect(str).to eq(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
expect(io.class).to eq(IO)
expect(io.read(5)).to eq("12345")
output = java.io.ByteArrayOutputStream.new
channel = java.nio.channels.Channels.newChannel(output)
io = channel.to_io
expect(io.class).to eq(IO)
io.write("12345")
io.flush
expect(String.from_java_bytes(output.to_byte_array)).to eq("12345")
end
it "is coercible to java.nio.channels.Channel with IO#to_channel" do
file = Tempfile.new("io_spec")
channel = file.to_channel
expect(java.nio.channels.Channel).to be === channel
bytes = java.nio.ByteBuffer.wrap(input_number.to_java_bytes)
channel.write(bytes)
file.seek(0)
str = file.read(10)
expect(str).to eq(String.from_java_bytes(bytes.array))
end
end
|