Class: Omnizip::IO::BufferedInput
- Inherits:
-
Object
- Object
- Omnizip::IO::BufferedInput
- Defined in:
- lib/omnizip/io/buffered_input.rb
Overview
Buffered input stream for efficient reading.
This class provides buffered reading capabilities with methods for reading bytes, integers, and other data types efficiently.
Constant Summary collapse
- DEFAULT_BUFFER_SIZE =
65_536
Instance Attribute Summary collapse
-
#buffer_size ⇒ Object
readonly
Returns the value of attribute buffer_size.
-
#position ⇒ Object
readonly
Returns the value of attribute position.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Instance Method Summary collapse
-
#close ⇒ void
Close the underlying source.
-
#eof? ⇒ Boolean
Check if at end of stream.
-
#initialize(source, buffer_size: DEFAULT_BUFFER_SIZE) ⇒ BufferedInput
constructor
Initialize buffered input.
-
#read(num_bytes) ⇒ String?
Read bytes from the stream.
-
#read_byte ⇒ Integer?
Read a single byte.
Constructor Details
#initialize(source, buffer_size: DEFAULT_BUFFER_SIZE) ⇒ BufferedInput
Initialize buffered input.
34 35 36 37 38 39 40 41 42 |
# File 'lib/omnizip/io/buffered_input.rb', line 34 def initialize(source, buffer_size: DEFAULT_BUFFER_SIZE) @source = source @buffer_size = buffer_size @buffer = String.new(capacity: buffer_size) @buffer_pos = 0 @buffer_end = 0 @position = 0 @eof = false end |
Instance Attribute Details
#buffer_size ⇒ Object (readonly)
Returns the value of attribute buffer_size.
28 29 30 |
# File 'lib/omnizip/io/buffered_input.rb', line 28 def buffer_size @buffer_size end |
#position ⇒ Object (readonly)
Returns the value of attribute position.
28 29 30 |
# File 'lib/omnizip/io/buffered_input.rb', line 28 def position @position end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
28 29 30 |
# File 'lib/omnizip/io/buffered_input.rb', line 28 def source @source end |
Instance Method Details
#close ⇒ void
This method returns an undefined value.
Close the underlying source.
78 79 80 |
# File 'lib/omnizip/io/buffered_input.rb', line 78 def close @source.close if @source.respond_to?(:close) end |
#eof? ⇒ Boolean
Check if at end of stream.
71 72 73 |
# File 'lib/omnizip/io/buffered_input.rb', line 71 def eof? @eof && @buffer_pos >= @buffer_end end |
#read(num_bytes) ⇒ String?
Read bytes from the stream.
48 49 50 51 52 53 54 |
# File 'lib/omnizip/io/buffered_input.rb', line 48 def read(num_bytes) return nil if @eof && @buffer_pos >= @buffer_end result = String.new(capacity: num_bytes) read_into_result(result, num_bytes) result.empty? ? nil : result end |
#read_byte ⇒ Integer?
Read a single byte.
59 60 61 62 63 64 65 66 |
# File 'lib/omnizip/io/buffered_input.rb', line 59 def read_byte return nil if @eof && @buffer_pos >= @buffer_end ensure_buffer_filled return nil if @buffer_pos >= @buffer_end consume_byte end |