diff --git a/README.md b/README.md index 4798c98..4408b00 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ LZ4 is lossless compression algorithm, providing compression speed > 500 MB/s pe require "lz4" ``` -`LZ4` shard provides both `LZ4::Reader` and `LZ4::Writer` as well as `LZ4#decode` and `LZ4#encode` methods for quick usage. +`LZ4` shard provides both `Compress::LZ4::Reader` and `Compress::LZ4::Writer` as well as `Compress::LZ4#decode` and `Compress::LZ4#encode` methods for quick usage. ## Example: decompress an lz4 file # @@ -31,7 +31,7 @@ require "lz4" require "lz4" string = File.open("file.xz") do |file| - LZ4::Reader.open(file) do |lz4| + Compress::LZ4::Reader.open(file) do |lz4| lz4.gets_to_end end end @@ -47,16 +47,13 @@ File.write("file.txt", "abcd") File.open("./file.txt", "r") do |input_file| File.open("./file.xz", "w") do |output_file| - LZ4::Writer.open(output_file) do |lz4| + Compress::LZ4::Writer.open(output_file) do |lz4| IO.copy(input_file, lz4) end end end ``` -## Development - -TODO: Write development instructions here ## Contributing diff --git a/shard.yml b/shard.yml index fcab4b6..1afd840 100644 --- a/shard.yml +++ b/shard.yml @@ -1,11 +1,11 @@ name: lz4 -version: 0.1.0 +version: 0.1.1 authors: - Ali Naqvi description: | Crystal bindings to the LZ4 compression library. -crystal: 0.34.0 +crystal: 0.35.0 license: MIT diff --git a/spec/lz4_spec.cr b/spec/lz4_spec.cr index 6800f42..a64f174 100644 --- a/spec/lz4_spec.cr +++ b/spec/lz4_spec.cr @@ -1,4 +1,4 @@ require "./spec_helper" -describe LZ4 do +describe Compress::LZ4 do end diff --git a/src/lz4.cr b/src/lz4.cr index d175401..c666f40 100644 --- a/src/lz4.cr +++ b/src/lz4.cr @@ -1,7 +1,7 @@ # LZ4 Crystal Wrapper require "semantic_version" -module LZ4 +module Compress::LZ4 VERSION = "0.1.0" LZ4_VERSION = SemanticVersion.parse String.new(LibLZ4.version_string) diff --git a/src/lz4/lib.cr b/src/lz4/lib.cr index 2d43538..84e4a6d 100644 --- a/src/lz4/lib.cr +++ b/src/lz4/lib.cr @@ -1,4 +1,4 @@ -module LZ4 +module Compress::LZ4 @[Link(ldflags: "`command -v pkg-config > /dev/null && pkg-config --libs liblz4 2> /dev/null|| printf %s '--llz4'`")] lib LibLZ4 alias ErrorCodeT = LibC::SizeT diff --git a/src/lz4/reader.cr b/src/lz4/reader.cr index b87863f..e18bf36 100644 --- a/src/lz4/reader.cr +++ b/src/lz4/reader.cr @@ -8,13 +8,13 @@ # require "lz4" # string = File.open("file.lz4") do |file| -# LZ4::Reader.open(file) do |lz4| +# Compress::LZ4::Reader.open(file) do |lz4| # lz4.gets_to_end # end # end # pp string # ``` -class LZ4::Reader < IO +class Compress::LZ4::Reader < IO include IO::Buffered # If `#sync_close?` is `true`, closing this IO will close the underlying IO. diff --git a/src/lz4/writer.cr b/src/lz4/writer.cr index 5251e20..c178042 100644 --- a/src/lz4/writer.cr +++ b/src/lz4/writer.cr @@ -15,13 +15,13 @@ # # File.open("./file.txt", "r") do |input_file| # File.open("./file.lz4", "w") do |output_file| -# LZ4::Writer.open(output_file) do |lz4| +# Compress::LZ4::Writer.open(output_file) do |lz4| # IO.copy(input_file, lz4) # end # end # end # ``` -class LZ4::Writer < IO +class Compress::LZ4::Writer < IO # If `#sync_close?` is `true`, closing this IO will close the underlying IO. property? sync_close : Bool @context : LibLZ4::Cctx @@ -81,11 +81,11 @@ class LZ4::Writer < IO end # See `IO#write`. - def write(slice : Bytes) : Nil + def write(slice : Bytes) : Int64 check_open - return if slice.empty? + return 0i64 if slice.empty? write_header - + written = 0i64 while slice.size > 0 write_size = slice.size write_size = @buffer.size if write_size > @buffer.size @@ -93,12 +93,13 @@ class LZ4::Writer < IO comp_size = LibLZ4.compress_update(@context, @buffer.to_unsafe, @buffer.size, slice.to_unsafe, write_size, nil) raise LZ4Error.new("Compression failed: #{String.new(LibLZ4.get_error_name(comp_size))}") unless LibLZ4.is_error(comp_size) == 0 - @output.write(@buffer[...comp_size]) if comp_size > 0 + written += @output.write(@buffer[...comp_size]) if comp_size > 0 # 0 means data was buffered, to avoid buffer too small problem at end, # let's flush the data manually flush if comp_size == 0 slice = slice[write_size..] end + written end # See `IO#flush`. @@ -137,7 +138,7 @@ class LZ4::Writer < IO end end -struct LZ4::CompressOptions +struct Compress::LZ4::CompressOptions enum CompressionLevel FAST = 0 MIN = 3