Class: Omnizip::IO::BufferedInput

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

Instance Method Summary collapse

Constructor Details

#initialize(source, buffer_size: DEFAULT_BUFFER_SIZE) ⇒ BufferedInput

Initialize buffered input.

Parameters:

  • source (IO, #read)

    Input source

  • buffer_size (Integer) (defaults to: DEFAULT_BUFFER_SIZE)

    Size of internal buffer



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_sizeObject (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

#positionObject (readonly)

Returns the value of attribute position.



28
29
30
# File 'lib/omnizip/io/buffered_input.rb', line 28

def position
  @position
end

#sourceObject (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

#closevoid

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.

Returns:

  • (Boolean)

    True if at EOF



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.

Parameters:

  • num_bytes (Integer)

    Number of bytes to read

Returns:

  • (String, nil)

    Read bytes or nil if EOF



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_byteInteger?

Read a single byte.

Returns:

  • (Integer, nil)

    Byte value (0-255) or nil if EOF



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