Class: BinData::IO::Transform
- Inherits:
-
Object
- Object
- BinData::IO::Transform
- Defined in:
- lib/bindata/io.rb
Overview
An IO stream may be transformed before processing. e.g. encoding, compression, buffered.
Multiple transforms can be chained together.
To create a new transform layer, subclass Transform
. Override the public methods #read
and #write
at a minimum. Additionally the hook, #before_transform
, #after_read_transform
and #after_write_transform
are available as well.
IMPORTANT! If your transform changes the size of the underlying data stream (e.g. compression), then call ::transform_changes_stream_length!
in your subclass.
Direct Known Subclasses
Buffer::BufferIO, Skip::SkipUntilValidPlugin::ReadaheadIO, Transform::Brotli, Transform::LZ4, Transform::Lzma, Transform::XZ, Transform::Xor, Transform::Zlib, Transform::Zstd
Class Method Summary collapse
-
.transform_changes_stream_length! ⇒ Object
Indicates that this transform changes the length of the underlying data.
Instance Method Summary collapse
-
#after_read_transform ⇒ Object
Flushes the input stream.
-
#after_write_transform ⇒ Object
Flushes the output stream.
-
#before_transform ⇒ Object
Initialises this transform.
-
#initialize ⇒ Transform
constructor
A new instance of Transform.
-
#num_bytes_remaining ⇒ Object
How many bytes are available for reading?.
-
#offset ⇒ Object
The current offset within the stream.
-
#prepend_to_chain(chain) ⇒ Object
Prepends this transform to the given
chain
. -
#read(n) ⇒ Object
Reads
n
bytes from the stream. -
#seek_abs(n) ⇒ Object
Seeks to the given absolute position.
-
#seekable? ⇒ Boolean
Is the IO seekable?.
-
#skip(n) ⇒ Object
Skips forward
n
bytes in the input stream. -
#write(data) ⇒ Object
Writes
data
to the stream.
Constructor Details
#initialize ⇒ Transform
Returns a new instance of Transform.
392 393 394 |
# File 'lib/bindata/io.rb', line 392 def initialize @chain_io = nil end |
Class Method Details
.transform_changes_stream_length! ⇒ Object
Indicates that this transform changes the length of the underlying data. e.g. performs compression or error correction
387 388 389 |
# File 'lib/bindata/io.rb', line 387 def transform_changes_stream_length! prepend(UnSeekableIO) end |
Instance Method Details
#after_read_transform ⇒ Object
Flushes the input stream.
Called after the final read operation.
404 |
# File 'lib/bindata/io.rb', line 404 def after_read_transform; end |
#after_write_transform ⇒ Object
Flushes the output stream.
Called after the final write operation.
409 |
# File 'lib/bindata/io.rb', line 409 def after_write_transform; end |
#before_transform ⇒ Object
Initialises this transform.
Called before any IO operations.
399 |
# File 'lib/bindata/io.rb', line 399 def before_transform; end |
#num_bytes_remaining ⇒ Object
How many bytes are available for reading?
426 427 428 |
# File 'lib/bindata/io.rb', line 426 def num_bytes_remaining chain_num_bytes_remaining end |
#offset ⇒ Object
The current offset within the stream.
431 432 433 |
# File 'lib/bindata/io.rb', line 431 def offset chain_offset end |
#prepend_to_chain(chain) ⇒ Object
Prepends this transform to the given chain
.
Returns self (the new head of chain).
414 415 416 417 418 |
# File 'lib/bindata/io.rb', line 414 def prepend_to_chain(chain) @chain_io = chain before_transform self end |
#read(n) ⇒ Object
Reads n
bytes from the stream.
446 447 448 |
# File 'lib/bindata/io.rb', line 446 def read(n) chain_read(n) end |
#seek_abs(n) ⇒ Object
Seeks to the given absolute position.
441 442 443 |
# File 'lib/bindata/io.rb', line 441 def seek_abs(n) chain_seek_abs(n) end |
#seekable? ⇒ Boolean
Is the IO seekable?
421 422 423 |
# File 'lib/bindata/io.rb', line 421 def seekable? @chain_io.seekable? end |
#skip(n) ⇒ Object
Skips forward n
bytes in the input stream.
436 437 438 |
# File 'lib/bindata/io.rb', line 436 def skip(n) chain_skip(n) end |
#write(data) ⇒ Object
Writes data
to the stream.
451 452 453 |
# File 'lib/bindata/io.rb', line 451 def write(data) chain_write(data) end |