No commit activity in last 3 years
No release in over 3 years
JRuby extension to expose an interruptible NIO FileChannel for STDIN
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 10.0.0
>= 2.0.0
 Project Readme

jruby-stdin-channel

JRuby Java extension gem which extracts an interruptible FileChannel from Java System.in stdin. Using this gem, calling close on the blocking read method will unblock it, unlike the normal JRuby $stdin.

Escaping the blocking read using close only works with Java 8. When using Java 7, the behaviour is identical to the normal JRuby $stdin where an input character needs to be typed for the read method to unblock.

I have only tested it with

  • OSX 10.10.3
  • JRuby 1.7.20
  • Java 7 & 8

Background

This was created to help solve an issue in logstash with the stdin input plugin that prevents normally exiting logstash upon SIGINT or SIGTERM. See elastic/logstash#1769 for details.

Installation

gem "jruby-stdin-channel"

Usage

stdin = StdinChannel::Reader.new

# blocking reads stdin upto a maximum of 1024 bytes, similar to IO#sysread
data = stdin.read(1024)

# close stdin to unblock read
stdin.close

Example

require "jruby-stdin-channel"

stdin = StdinChannel::Reader.new

trap('INT') do
  puts("\n> INT")
  Thread.new{stdin.close}
end

puts("> start")
begin
  loop do
    data = stdin.read(1024)
    puts("> read:#{data.inspect}")
  end
rescue EOFError
  puts("> EOF")
rescue StdinChannel::ClosedChannelError
  puts("> closed")
rescue => e
  puts("> exception e=#{e.inspect}")
end
puts("> end")

Author

Colin Surprenant

License

Apache License, Version 2.0. See the LICENSE.md file.