Sha256: f06921e91582da17443964004c2904badd568d27acb269402bf97c905a147174

Contents?: true

Size: 1.65 KB

Versions: 29

Compression:

Stored size: 1.65 KB

Contents

module Embulk
  require 'embulk/buffer'

  class FileInput
    def initialize(java_file_input)
      @java_file_input = java_file_input
      @buffer = nil
    end

    def next_file
      if @java_file_input.nextFile
        return self
      else
        return nil
      end
    end

    def each(&block)
      if @buffer
        yield @buffer
        @buffer = nil
      end

      while java_buffer = @java_file_input.poll
        buffer = Buffer.from_java(java_buffer)
        java_buffer.release
        yield buffer
      end
    end

    def read(count=nil, dest=nil)
      if count == nil
        @buffer ||= Buffer.new
        while java_buffer = @java_file_input.poll
          @buffer << Buffer.from_java(java_buffer)
          java_buffer.release
        end

        return nil if @buffer.empty? && count != 0

        if dest
          dest.replace(@buffer)
        else
          dest = @buffer
        end
        @buffer = nil

      else
        @buffer ||= Buffer.new
        until @buffer.size >= count
          java_buffer = @java_file_input.poll
          break unless java_buffer
          @buffer << Buffer.from_java(java_buffer)
          java_buffer.release
        end

        return nil if @buffer.empty? && count != 0

        if @buffer.size <= count
          if dest
            dest.replace(@buffer)
          else
            dest = @buffer
          end
          @buffer = nil
        else
          data = @buffer.slice!(0, count)
          if dest
            dest.replace(data)
          else
            dest = data
          end
        end
      end
      return dest
    end

    def close
      @java_file_input.close
    end
  end
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
embulk-0.6.16 lib/embulk/file_input.rb
embulk-0.6.15 lib/embulk/file_input.rb
embulk-0.6.14 lib/embulk/file_input.rb
embulk-0.6.13 lib/embulk/file_input.rb
embulk-0.6.12 lib/embulk/file_input.rb
embulk-0.6.11 lib/embulk/file_input.rb
embulk-0.6.10 lib/embulk/file_input.rb
embulk-0.6.9 lib/embulk/file_input.rb
embulk-0.6.8 lib/embulk/file_input.rb
embulk-0.6.7 lib/embulk/file_input.rb
embulk-0.6.6 lib/embulk/file_input.rb
embulk-0.6.5 lib/embulk/file_input.rb
embulk-0.6.4 lib/embulk/file_input.rb
embulk-0.6.3 lib/embulk/file_input.rb
embulk-0.6.2 lib/embulk/file_input.rb
embulk-0.6.1 lib/embulk/file_input.rb
embulk-0.6.0 lib/embulk/file_input.rb
embulk-0.5.5 lib/embulk/file_input.rb
embulk-0.5.4 lib/embulk/file_input.rb
embulk-0.5.3 lib/embulk/file_input.rb