Class: ChConnect::BodyReader Private
- Inherits:
-
Object
- Object
- ChConnect::BodyReader
- Defined in:
- lib/ch_connect/body_reader.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Wrapper for HTTP response body providing buffered reads. Reads data in chunks for efficient small reads.
Constant Summary collapse
- CHUNK_SIZE =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
64KB chunks
64 * 1024
Instance Method Summary collapse
-
#close ⇒ void
private
Closes the underlying body.
-
#eof? ⇒ Boolean
private
Returns true if at end of stream.
-
#getbyte ⇒ Integer?
private
Reads a single byte as integer, returns nil at EOF.
-
#initialize(body) ⇒ BodyReader
constructor
private
Creates a new body reader.
-
#read(n) ⇒ String
private
Reads exactly n bytes from the body.
Constructor Details
#initialize(body) ⇒ BodyReader
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Creates a new body reader.
13 14 15 16 17 18 19 |
# File 'lib/ch_connect/body_reader.rb', line 13 def initialize(body) @body = body @size = body.bytesize @buffer = "".b @buffer_pos = 0 @eof = false end |
Instance Method Details
#close ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Closes the underlying body.
24 25 26 |
# File 'lib/ch_connect/body_reader.rb', line 24 def close @body.close end |
#eof? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns true if at end of stream.
31 32 33 34 |
# File 'lib/ch_connect/body_reader.rb', line 31 def eof? fill_buffer(1) if @buffer_pos >= @buffer.bytesize && !@eof @eof && @buffer_pos >= @buffer.bytesize end |
#getbyte ⇒ Integer?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Reads a single byte as integer, returns nil at EOF.
51 52 53 54 55 56 57 58 59 |
# File 'lib/ch_connect/body_reader.rb', line 51 def getbyte fill_buffer(1) return nil if @buffer_pos >= @buffer.bytesize byte = @buffer.getbyte(@buffer_pos) @buffer_pos += 1 compact_buffer if @buffer_pos > CHUNK_SIZE byte end |
#read(n) ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Reads exactly n bytes from the body.
40 41 42 43 44 45 46 |
# File 'lib/ch_connect/body_reader.rb', line 40 def read(n) fill_buffer(n) result = @buffer.byteslice(@buffer_pos, n) @buffer_pos += n compact_buffer if @buffer_pos > CHUNK_SIZE result end |