Class: BinData::IO::Transform

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTransform

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_transformObject

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_transformObject

Flushes the output stream.

Called after the final write operation.



409
# File 'lib/bindata/io.rb', line 409

def after_write_transform; end

#before_transformObject

Initialises this transform.

Called before any IO operations.



399
# File 'lib/bindata/io.rb', line 399

def before_transform; end

#num_bytes_remainingObject

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

#offsetObject

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?

Returns:

  • (Boolean)


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