Skip to content

Commit

Permalink
Updated for Crystal 0.35.0
Browse files Browse the repository at this point in the history
  • Loading branch information
naqvis committed Jun 11, 2020
1 parent ace2a59 commit 5323ae6
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 20 deletions.
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ 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
#
```crystal
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
Expand All @@ -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

Expand Down
4 changes: 2 additions & 2 deletions shard.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
name: lz4
version: 0.1.0
version: 0.1.1

authors:
- Ali Naqvi <syed.alinaqvi@gmail.com>
description: |
Crystal bindings to the LZ4 compression library.
crystal: 0.34.0
crystal: 0.35.0

license: MIT
2 changes: 1 addition & 1 deletion spec/lz4_spec.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require "./spec_helper"

describe LZ4 do
describe Compress::LZ4 do
end
2 changes: 1 addition & 1 deletion src/lz4.cr
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/lz4/lib.cr
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/lz4/reader.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
15 changes: 8 additions & 7 deletions src/lz4/writer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -81,24 +81,25 @@ 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
@buffer.to_unsafe.clear(@buffer.size)

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`.
Expand Down Expand Up @@ -137,7 +138,7 @@ class LZ4::Writer < IO
end
end

struct LZ4::CompressOptions
struct Compress::LZ4::CompressOptions
enum CompressionLevel
FAST = 0
MIN = 3
Expand Down

0 comments on commit 5323ae6

Please sign in to comment.