class CZTop::Z85::Pipe

Can be used if you want to encode or decode data from one IO to another. It'll do so until it hits EOF in the source IO.

Constants

DECODE_READ_SZ

@return [Integer] size of chunks read when decoding

ENCODE_READ_SZ

@return [Integer] size of chunks read when encoding

Public Class Methods

new(source, sink, strategy: Strategy::Parallel) click to toggle source

@param source [IO] where to read data @param sink [IO] where to write data @param strategy [Strategy] algorithm to use (pass the class itself,

not an instance)
# File lib/cztop/z85/pipe.rb, line 8
def initialize(source, sink, strategy: Strategy::Parallel)
  @source, @sink = source, sink
  @strategy = strategy
  @bin_bytes = 0 # processed binary data (non-Z85)
end

Public Instance Methods

decode() click to toggle source

Decodes Z85 data from source and writes decoded data to sink. This is done until EOF is hit on the source.

@return [Integer] number of bytes written (binary data)

# File lib/cztop/z85/pipe.rb, line 44
def decode
  @strategy.new(@source, @sink, DECODE_READ_SZ) do |chunk, prev_chunk|
    if prev_chunk && chunk
      CZTop::Z85.decode(prev_chunk)
    elsif prev_chunk && chunk.nil?
      CZTop::Z85::Padded.decode(prev_chunk)
    elsif prev_chunk.nil? && chunk.nil?
      CZTop::Z85.decode("") # empty input
    else
      "" # very first chunk. don't decode anything yet...
    end
  end.execute
  return @bin_bytes
end
encode() click to toggle source

Encodes data from source and writes Z85 data to sink. This is done until EOF is hit on the source.

@return [Integer] number of bytes read (binary data)

# File lib/cztop/z85/pipe.rb, line 24
def encode
  @strategy.new(@source, @sink, ENCODE_READ_SZ) do |chunk, prev_chunk|
    @bin_bytes += chunk.bytesize if chunk
    if prev_chunk && chunk
      CZTop::Z85.encode(prev_chunk)
    elsif prev_chunk && chunk.nil? # last chunk
      CZTop::Z85::Padded.encode(prev_chunk)
    elsif prev_chunk.nil? && chunk.nil?
      CZTop::Z85.encode("") # empty input
    else
      "" # very first chunk. don't encode anything yet...
    end
  end.execute
  return @bin_bytes
end