Class: Omnizip::Formats::Cpio::BoundedIO

Inherits:
Object
  • Object
show all
Defined in:
lib/omnizip/formats/cpio/bounded_io.rb

Overview

IO wrapper that limits reading to a specific byte count

Used to read file content from CPIO archives where the file size is known in advance but the underlying IO stream continues.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, length) { ... } ⇒ BoundedIO

Initialize bounded IO

Parameters:

  • io (IO)

    Underlying IO stream

  • length (Integer)

    Maximum bytes to read

Yields:

  • Block called when EOF is reached (for reading padding)



18
19
20
21
22
23
24
# File 'lib/omnizip/formats/cpio/bounded_io.rb', line 18

def initialize(io, length, &eof_callback)
  @io = io
  @length = length
  @remaining = length
  @eof_callback = eof_callback
  @eof = false
end

Instance Attribute Details

#lengthObject (readonly)

Returns the value of attribute length.



11
12
13
# File 'lib/omnizip/formats/cpio/bounded_io.rb', line 11

def length
  @length
end

#remainingObject (readonly)

Returns the value of attribute remaining.



11
12
13
# File 'lib/omnizip/formats/cpio/bounded_io.rb', line 11

def remaining
  @remaining
end

Instance Method Details

#eof?Boolean

Check if at end of bounded region

Returns:

  • (Boolean)

    True if no more bytes to read



56
57
58
59
60
61
62
# File 'lib/omnizip/formats/cpio/bounded_io.rb', line 56

def eof?
  return false if @remaining.positive?
  return @eof if @eof

  @eof_callback&.call
  @eof = true
end

#read(size = nil) ⇒ String?

Read bytes from the IO

Parameters:

  • size (Integer, nil) (defaults to: nil)

    Number of bytes to read (nil = remaining)

Returns:

  • (String, nil)

    Data read or nil at EOF



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/omnizip/formats/cpio/bounded_io.rb', line 30

def read(size = nil)
  return nil if eof?

  size = @remaining if size.nil?
  data = @io.read(size)
  return nil if data.nil?

  @remaining -= data.bytesize
  eof?
  data
end

#sysread(size) ⇒ String

System read (raises on EOF)

Parameters:

  • size (Integer)

    Number of bytes to read

Returns:

  • (String)

    Data read

Raises:

  • (EOFError)

    If at end of bounded region



47
48
49
50
51
# File 'lib/omnizip/formats/cpio/bounded_io.rb', line 47

def sysread(size)
  raise EOFError, "end of file reached" if eof?

  read(size)
end