class Compress::LZ4::Writer
- Compress::LZ4::Writer
- IO
- Reference
- Object
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.crConstructors
- .new(output : IO, options = CompressOptions.new, sync_close : Bool = false)
-
.new(filename : String, options = CompressOptions.new)
Creates a new writer to the given filename.
Class Method Summary
-
.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.
-
.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.
Instance Method Summary
-
#close
Ends the current LZ4 frame, the stream can still be written to, unless @sync_close
-
#closed? : Bool
Returns
trueif thisIOis closed. - #compressed_bytes : UInt64
-
#compression_ratio : Float64
Uncompressed bytes read / compressed bytes outputted so far in the stream
- #finalize
-
#flush : Nil
Flush LZ4 lib buffers even if a block isn't full
-
#read(slice : Bytes)
Reads at most slice.size bytes from this
IOinto slice. - #sync_close=(sync_close : Bool)
- #sync_close? : Bool
- #uncompressed_bytes : UInt64
-
#write(slice : Bytes) : Nil
Writes the contents of slice into this
IO.
Constructor Detail
Creates a new writer to the given filename.
Class Method Detail
Creates a new writer for the given io, yields it to the given block, and closes it at its end.
Creates a new writer to the given filename, yields it to the given block, and closes it at the end.
Instance Method Detail
Returns true if this IO is closed.
IO defines returns false, but including types may override.
Uncompressed bytes read / compressed bytes outputted so far in the stream
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
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"