class Compress::LZ4::Writer

Overview

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

Defined in:

lz4/writer.cr

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.new(output : IO, options = CompressOptions.new, sync_close : Bool = false) #

[View source]
def self.new(filename : String, options = CompressOptions.new) #

Creates a new writer to the given filename.


[View source]

Class Method Detail

def self.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.


[View source]
def self.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.


[View source]

Instance Method Detail

def close #

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


[View source]
def closed? : Bool #
Description copied from class IO

Returns true if this IO is closed.

IO defines returns false, but including types may override.


[View source]
def compressed_bytes : UInt64 #

[View source]
def compression_ratio : Float64 #

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


[View source]
def finalize #

[View source]
def flush : Nil #

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


[View source]
def read(slice : Bytes) #
Description copied from class IO

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

[View source]
def sync_close=(sync_close : Bool) #

[View source]
def sync_close? : Bool #

[View source]
def uncompressed_bytes : UInt64 #

[View source]
def write(slice : Bytes) : Nil #
Description copied from class IO

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"

[View source]