自動的に閉じるJavaファイルストリーム

JRubyで外部のJavaライブラリと連携とかするとき、RubyのFileクラスのopenのように自動的に閉じるFileInputStream、FileOutputStreamがほしいので作ってみた。
わりと簡単に作れる

include Java
class FileInput
  def self.open(file, &block)
    raise "no block given" unless block_given?
    begin
      input = java.io.FileInputStream.new(file)
      yield input
    ensure
      input.close unless input.nil?
    end
  end
end

class FileOutput
  def self.open(file, &block)
    raise "no block given" unless block_given?
    begin
      output = java.io.FileOutputStream.new(file)
      yield output
    ensure
      output.close unless output.nil?
    end
  end
end

つかうときはFileOutput.open("filepath"){|out| ... }みたいな感じで