Skip to content

Instantly share code, notes, and snippets.

@joncooper
Created June 23, 2011 22:56
Show Gist options
  • Select an option

  • Save joncooper/1043829 to your computer and use it in GitHub Desktop.

Select an option

Save joncooper/1043829 to your computer and use it in GitHub Desktop.
Strange behavior with JRuby IO#rewind when backed by a ByteArrayInputStream
# https://github.com/jruby/jruby/blob/master/src/org/jruby/RubyIO.java
#
# public RubyFixnum rewind (...) {
# calls OpenFile#getMainStreamSafe#lseek(0L, Stream.SEEK_SET)
#
# https://github.com/jruby/jruby/blob/master/src/org/jruby/util/io/ChannelStream.java
#
# public synchronized void lseek (...) {
# checks if the fd is seekable
# checks if the fd is a SelectableChannel
# if not, drops out the bottom silently
require 'java'
java_import java.io.ByteArrayInputStream
java_input_stream = ByteArrayInputStream.new((1..100).to_a.to_java(:byte))
ruby_input_stream = java_input_stream.to_io
data = ruby_input_stream.read
puts "data size before rewind: #{data.size}"
ruby_input_stream.rewind
data = ruby_input_stream.read
puts "data size after rewind: #{data.size}"
# ➜ jruby-bug rvm use jruby-1.5.6
# Using /Users/jdc/.rvm/gems/jruby-1.5.6
# ➜ jruby-bug ruby bug.rb
# data size before rewind: 100
# data size after rewind: 0
# ➜ jruby-bug rvm use jruby-1.6.2
# Using /Users/jdc/.rvm/gems/jruby-1.6.2
# ➜ jruby-bug ruby bug.rb
# data size before rewind: 100
# data size after rewind: 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment