Class: Omnizip::Formats::Cpio::BoundedIO
- Inherits:
-
Object
- Object
- Omnizip::Formats::Cpio::BoundedIO
- 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
-
#length ⇒ Object
readonly
Returns the value of attribute length.
-
#remaining ⇒ Object
readonly
Returns the value of attribute remaining.
Instance Method Summary collapse
-
#eof? ⇒ Boolean
Check if at end of bounded region.
-
#initialize(io, length) { ... } ⇒ BoundedIO
constructor
Initialize bounded IO.
-
#read(size = nil) ⇒ String?
Read bytes from the IO.
-
#sysread(size) ⇒ String
System read (raises on EOF).
Constructor Details
#initialize(io, length) { ... } ⇒ BoundedIO
Initialize bounded IO
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
#length ⇒ Object (readonly)
Returns the value of attribute length.
11 12 13 |
# File 'lib/omnizip/formats/cpio/bounded_io.rb', line 11 def length @length end |
#remaining ⇒ Object (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
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
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)
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 |