Sha256: 1ff6213cac510f0e606830eda7026bd9fd7fb1bef6ec5ff0120cb8e95f98ae9a

Contents?: true

Size: 1.21 KB

Versions: 2

Compression:

Stored size: 1.21 KB

Contents

module RackJetty
  class JavaInput
    def initialize(input)
      @input = input
    end
    
    # reads count bytes into into_buffer, returns nil at EOF, otherwise
    # returns into_buffer.
    def read_bytes(count, into_buffer)
      data = "\0" * count
      data = data.to_java_bytes
      count = @input.read(data, 0, count)
      if (count == -1)
        return nil
      end
      into_buffer << String.from_java_bytes(data[0,count])
      return into_buffer
    end
    
    # Reads the entire string into_buffer
    def read_all(into_buffer)
      while (read_bytes(4096, into_buffer))
        # work is in the loop condition.
      end
      return into_buffer
    end

    # If count is nil, reads the entire input into the buffer. Otherwise,
    # reads up to count bytes from the stream and puts them into buffer.
    # either way, returns buffer.
    def read(count = nil, buffer = "")
      if (count.nil?)
        read_all(buffer)
      else
        buffer = read_bytes(count, buffer)
      end
      return buffer
    end
    alias_method :gets, :read
    
    # Reads the input as chunked data and yields it piece by piece.
    def each
      while (s = read_bytes(4096, ""))
        yield s
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rack-jetty-0.2.0 lib/rack_jetty/java_input.rb
rack-jetty-0.1.0 lib/rack_jetty/java_input.rb