Class: CZTop::Z85::Pipe

Inherits:
Object
  • Object
show all
Defined in:
lib/cztop/z85/pipe.rb

Overview

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.

Defined Under Namespace

Classes: Strategy

Constant Summary collapse

ENCODE_READ_SZ =

Returns size of chunks read when encoding.

Returns:

  • (Integer)

    size of chunks read when encoding

(32 * (2**10))
DECODE_READ_SZ =

Returns size of chunks read when decoding.

Returns:

  • (Integer)

    size of chunks read when decoding

ENCODE_READ_SZ / 4 * 5

Instance Method Summary collapse

Constructor Details

#initialize(source, sink, strategy: Strategy::Parallel) ⇒ Pipe

Returns a new instance of Pipe.

Parameters:

  • source (IO)

    where to read data

  • sink (IO)

    where to write data

  • strategy (Strategy) (defaults to: Strategy::Parallel)

    algorithm to use (pass the class itself, not an instance)



11
12
13
14
15
16
# File 'lib/cztop/z85/pipe.rb', line 11

def initialize(source, sink, strategy: Strategy::Parallel)
  @source    = source
  @sink      = sink
  @strategy  = strategy
  @bin_bytes = 0 # processed binary data (non-Z85)
end

Instance Method Details

#decodeInteger

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

Returns:

  • (Integer)

    number of bytes written (binary data)



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/cztop/z85/pipe.rb', line 49

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
  @bin_bytes
end

#encodeInteger

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

Returns:

  • (Integer)

    number of bytes read (binary data)



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/cztop/z85/pipe.rb', line 28

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
  @bin_bytes
end