Class: RackJetty::JavaInput

Inherits:
Object
  • Object
show all
Defined in:
lib/rack_jetty/java_input.rb

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ JavaInput

Returns a new instance of JavaInput.



3
4
5
# File 'lib/rack_jetty/java_input.rb', line 3

def initialize(input)
  @input = input
end

Instance Method Details

#eachObject

Reads the input as chunked data and yields it piece by piece.



42
43
44
45
46
# File 'lib/rack_jetty/java_input.rb', line 42

def each
  while (s = read_bytes(4096, ""))
    yield s
  end
end

#read(count = nil, buffer = "") ⇒ Object Also known as: gets

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.



31
32
33
34
35
36
37
38
# File 'lib/rack_jetty/java_input.rb', line 31

def read(count = nil, buffer = "")
  if (count.nil?)
    read_all(buffer)
  else
    buffer = read_bytes(count, buffer)
  end
  return buffer
end

#read_all(into_buffer) ⇒ Object

Reads the entire string into_buffer



21
22
23
24
25
26
# File 'lib/rack_jetty/java_input.rb', line 21

def read_all(into_buffer)
  while (read_bytes(4096, into_buffer))
    # work is in the loop condition.
  end
  return into_buffer
end

#read_bytes(count, into_buffer) ⇒ Object

reads count bytes into into_buffer, returns nil at EOF, otherwise returns into_buffer.



9
10
11
12
13
14
15
16
17
18
# File 'lib/rack_jetty/java_input.rb', line 9

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