class

Compress::LZ4::Writer

Inherits IO / Reference / Object

A write-only IO object to compress data in the LZ4 format.

Instances of this class wrap another IO object. When you write to this instance, it compresses the data and writes it to the underlying IO.

NOTE: unless created with a block, close must be invoked after all data has been written to a LZ4::Writer instance.

Example: compress a file

require "lz4"

File.write("file.txt", "abcd")

File.open("./file.txt", "r") do |input_file|
  File.open("./file.lz4", "w") do |output_file|
    Compress::LZ4::Writer.open(output_file) do |lz4|
      IO.copy(input_file, lz4)
    end
  end
end

Constructors

new(output : IO, options = CompressOptions.new, sync_close : Bool = false)
Source
new(filename : String, options = CompressOptions.new)

Creates a new writer to the given filename.

Source

Class methods

open(io : IO, options = CompressOptions.new, sync_close = false, &)

Creates a new writer for the given io, yields it to the given block, and closes it at its end.

Source
open(filename : String, options = CompressOptions.new, &)

Creates a new writer to the given filename, yields it to the given block, and closes it at the end.

Source

Instance methods

close

Ends the current LZ4 frame, the stream can still be written to, unless @sync_close

Source
closed?

Returns true if this IO is closed.

IO defines returns false, but including types may override.

Source
compressed_bytes
Source
compression_ratio

Uncompressed bytes read / compressed bytes outputted so far in the stream

Source
finalize
Source
flush

Flush LZ4 lib buffers even if a block isn't full

Source
read(slice : Bytes)

Reads at most slice.size bytes from this IO into slice. Returns the number of bytes read, which is 0 if and only if there is no more data to read (so checking for 0 is the way to detect end of file).

io = IO::Memory.new "hello"
slice = Bytes.new(4)
io.read(slice) # => 4
slice          # => Bytes[104, 101, 108, 108]
io.read(slice) # => 1
slice          # => Bytes[111, 101, 108, 108]
io.read(slice) # => 0
Source
sync_close=(sync_close : Bool)
Source
sync_close?
Source
uncompressed_bytes
Source
write(slice : Bytes) : Nil

Writes the contents of slice into this IO.

io = IO::Memory.new
slice = Bytes.new(4) { |i| ('a'.ord + i).to_u8 }
io.write(slice)
io.to_s # => "abcd"
Source