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 02f02d6 commit 1c374c0
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 30 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Crystal bindings to the [Brotli](https://github.com/google/brotli) compression l
require "brotli"
```

`brotli` shard provides both `Brotli::Reader` and `Brotli::Writer` , as well as `Brotli#decode` and `Brotli#encode` methods for quick usage.
`brotli` shard provides both `Compress::Brotli::Reader` and `Compress::Brotli::Writer` , as well as `Compress::Brotli#decode` and `Compress::Brotli#encode` methods for quick usage.

Refer to `specs` for sample usage.

Expand All @@ -30,7 +30,7 @@ Refer to `specs` for sample usage.
require "brotli"
string = File.open("file.br") do |file|
Brotli::Reader.open(file) do |brotli|
Compress::Brotli::Reader.open(file) do |brotli|
brotli.gets_to_end
end
end
Expand All @@ -46,7 +46,7 @@ File.write("file.txt", "abcd")
File.open("./file.txt", "r") do |input_file|
File.open("./file.br", "w") do |output_file|
Brotli::Writer.open(output_file) do |brotli|
Compress::Brotli::Writer.open(output_file) do |brotli|
IO.copy(input_file, brotli)
end
end
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: brotli
version: 0.1.1
version: 0.1.2

authors:
- Ali Naqvi <[email protected]>
description: |
Crystal bindings to the Brotli compression library.
crystal: 0.34.0
crystal: 0.35.0

license: MIT
24 changes: 12 additions & 12 deletions spec/brotli_spec.cr
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
require "./spec_helper"

describe Brotli do
describe Compress::Brotli do
# TODO: Write tests

it "Test Encode No Write" do
buf = IO::Memory.new
br = Brotli::Writer.new(buf)
br = Compress::Brotli::Writer.new(buf)
br.close

# check write after close
Expand All @@ -16,7 +16,7 @@ describe Brotli do

it "Test Encode Empty Write" do
buf = IO::Memory.new
Brotli::Writer.open(buf, options: Brotli::WriterOptions.new(quality: 5_u32)) do |br|
Compress::Brotli::Writer.open(buf, options: Compress::Brotli::WriterOptions.new(quality: 5_u32)) do |br|
br.write Bytes.empty
end
end
Expand All @@ -26,7 +26,7 @@ describe Brotli do
input = "<html><body><H1>Hello world</H1></body></html>"
buf = IO::Memory.new
inp = IO::Memory.new(input)
enc = Brotli::Writer.new(buf, options: Brotli::WriterOptions.new(quality: 1_u32))
enc = Compress::Brotli::Writer.new(buf, options: Compress::Brotli::WriterOptions.new(quality: 1_u32))
IO.copy inp, enc
enc.close
buf.rewind
Expand All @@ -45,7 +45,7 @@ describe Brotli do
Random.new.random_bytes(input)
half_input = input[0, input.size//2]
buf = IO::Memory.new
Brotli::Writer.open(buf, options: Brotli::WriterOptions.new(lgwin: lgwin.to_u32)) do |br|
Compress::Brotli::Writer.open(buf, options: Compress::Brotli::WriterOptions.new(lgwin: lgwin.to_u32)) do |br|
br.write half_input
end
# We've fed more data than the sliding window size. Check that some
Expand All @@ -59,7 +59,7 @@ describe Brotli do
input = Bytes.new(1000000)
Random.new.random_bytes(input)
buf = IO::Memory.new
Brotli::Writer.open(buf, options: Brotli::WriterOptions.new(quality: 5_u32)) do |br|
Compress::Brotli::Writer.open(buf, options: Compress::Brotli::WriterOptions.new(quality: 5_u32)) do |br|
br.write input
end
buf.rewind
Expand All @@ -70,7 +70,7 @@ describe Brotli do
input = Bytes.new(1000)
Random.new.random_bytes(input)
buf = IO::Memory.new
Brotli::Writer.open(buf, options: Brotli::WriterOptions.new(quality: 5_u32)) do |br|
Compress::Brotli::Writer.open(buf, options: Compress::Brotli::WriterOptions.new(quality: 5_u32)) do |br|
br.write input
br.flush
fail "0 bytes written after flush" if buf.size == 0
Expand All @@ -81,19 +81,19 @@ describe Brotli do

it "Test Reader" do
data = "hello crystal!" * 10000
compressed = Brotli.encode(data, Brotli::WriterOptions.new(quality: 5_u32))
uncompressed = Brotli.decode(compressed)
compressed = Compress::Brotli.encode(data, Compress::Brotli::WriterOptions.new(quality: 5_u32))
uncompressed = Compress::Brotli.decode(compressed)

data.to_slice.should eq(uncompressed)
end

it "Test Decode Trailing Data" do
data = "hello crystal!" * 10000
compressed = Brotli.encode(data, Brotli::WriterOptions.new(quality: 5_u32))
compressed = Compress::Brotli.encode(data, Compress::Brotli::WriterOptions.new(quality: 5_u32))
corrupt = Bytes.new(compressed.size + 1)
corrupt.copy_from(compressed.to_unsafe, compressed.size)
expect_raises(Brotli::BrotliError, "excessive input") do
Brotli.decode(corrupt)
expect_raises(Compress::Brotli::BrotliError, "excessive input") do
Compress::Brotli.decode(corrupt)
end
end
end
2 changes: 1 addition & 1 deletion spec/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ require "spec"
require "../src/brotli"

def check_compressed_data(compressed_data : Slice, want : Slice)
uncompressed = Brotli.decode(compressed_data)
uncompressed = Compress::Brotli.decode(compressed_data)
if uncompressed != want
fail "Data doesn't uncompress to the original value \n" +
"Length of original: #{want.size}\n" +
Expand Down
4 changes: 2 additions & 2 deletions src/brotli.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# `Brotli` Crystal Wrapper
module Brotli
VERSION = "0.1.0"
module Compress::Brotli
VERSION = "0.1.2"

class BrotliError < Exception
end
Expand Down
2 changes: 1 addition & 1 deletion src/brotli/lib.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Brotli
module Compress::Brotli
@[Link(ldflags: "`command -v pkg-config > /dev/null && pkg-config --libs libbrotlicommon libbrotlidec libbrotlienc 2> /dev/null|| printf %s '--llbrotlicommon --llbrotlidec --llbrotlienc'`")]

lib LibBrotli
Expand Down
4 changes: 2 additions & 2 deletions src/brotli/reader.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
# require "brotli"

# string = File.open("file.br") do |file|
# Brotli::Reader.open(file) do |br|
# Compress::Brotli::Reader.open(file) do |br|
# br.gets_to_end
# end
# end
# pp string
# ```
class Brotli::Reader < IO
class Compress::Brotli::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/brotli/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.br", "w") do |output_file|
# Brotli::Writer.open(output_file) do |br|
# Compress::Brotli::Writer.open(output_file) do |br|
# IO.copy(input_file, br)
# end
# end
# end
# ```
class Brotli::Writer < IO
class Compress::Brotli::Writer < IO
# If `#sync_close?` is `true`, closing this IO will close the underlying IO.
property? sync_close : Bool

Expand Down Expand Up @@ -73,10 +73,10 @@ class Brotli::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_chunk slice, LibBrotli::EncoderOperation::OperationProcess
end

Expand Down Expand Up @@ -108,7 +108,7 @@ class Brotli::Writer < IO

private def write_chunk(chunk : Slice, op : LibBrotli::EncoderOperation)
raise BrotliError.new("Writer closed") if @closed || @state.nil?

written = 0i64
loop do
size = chunk.size
avail_in = size.to_u64
Expand All @@ -123,14 +123,15 @@ class Brotli::Writer < IO

chunk = chunk[bytes_consumed..]
if output_data_size != 0
@output.write output.to_slice(output_data_size)
written += @output.write output.to_slice(output_data_size)
end
break if chunk.size == 0 && !has_more
end
written
end
end

struct Brotli::WriterOptions
struct Compress::Brotli::WriterOptions
# compression mode
property mode : LibBrotli::EncoderMode
# controls the compression speed vs compression density tradeoff. Higher the quality,
Expand Down

0 comments on commit 1c374c0

Please sign in to comment.