Class: ChConnect::BodyReader Private

Inherits:
Object
  • Object
show all
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

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.

Parameters:

  • body (#read, #bytesize, #close)

    HTTP response body



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

#closevoid

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.

Returns:

  • (Boolean)


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

#getbyteInteger?

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.

Returns:

  • (Integer, nil)

    byte value or 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.

Parameters:

  • n (Integer)

    number of bytes to read

Returns:

  • (String)

    binary string of n bytes



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