Skip to content

Commit

Permalink
Rename compressors and decompressors (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
bicycle1885 authored Oct 19, 2017
1 parent ab6e4cf commit b231e33
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 79 deletions.
19 changes: 1 addition & 18 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,9 @@ matrix:
- julia: nightly
notifications:
email: false
git:
depth: 99999999

## uncomment the following lines to allow failures on nightly julia
## (tests will run but not make your overall status red)
#matrix:
# allow_failures:
# - julia: nightly

## uncomment and modify the following lines to manually install system packages
#addons:
# apt: # apt-get for linux
# packages:
# - gfortran
#before_script: # homebrew for mac
# - if [ $TRAVIS_OS_NAME = osx ]; then brew install gcc; fi

## uncomment the following lines to override the default test script
#script:
# - julia -e 'Pkg.clone(pwd()); Pkg.build("CodecXz"); Pkg.test("CodecXz"; coverage=true)'
after_success:
# push coverage results to Codecov
- julia -e 'cd(Pkg.dir("CodecXz")); Pkg.add("Coverage"); using Coverage; Codecov.submit(Codecov.process_folder())'
- if [ $TRAVIS_JULIA_VERSION != "nightly" ]; then julia -e 'cd(Pkg.dir("CodecXz")); Pkg.add("Coverage"); using Coverage; Codecov.submit(Codecov.process_folder())'
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,24 @@ erat ex bibendum ipsum, sed varius ipsum ipsum vitae dui.
"""

# Streaming API.
stream = XzCompressionStream(IOBuffer(text))
for line in eachline(XzDecompressionStream(stream))
stream = XzCompressorStream(IOBuffer(text))
for line in eachline(XzDecompressorStream(stream))
println(line)
end
close(stream)

# Array API.
compressed = transcode(XzCompression(), text)
compressed = transcode(XzCompressor, text)
@assert sizeof(compressed) < sizeof(text)
@assert transcode(XzDecompression(), compressed) == Vector{UInt8}(text)
@assert transcode(XzDecompressor, compressed) == Vector{UInt8}(text)
```

This package exports following codecs and streams:

| Codec | Stream |
| ----------------- | ----------------------- |
| `XzCompression` | `XzCompressionStream` |
| `XzDecompression` | `XzDecompressionStream` |
| Codec | Stream |
| ---------------- | ---------------------- |
| `XzCompressor` | `XzCompressorStream` |
| `XzDecompressor` | `XzDecompressorStream` |

See docstrings and [TranscodingStreams.jl](https://github.com/bicycle1885/TranscodingStreams.jl) for details.

Expand Down
14 changes: 10 additions & 4 deletions src/CodecXz.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ __precompile__()
module CodecXz

export
XzCompression,
XzCompressionStream,
XzDecompression,
XzDecompressionStream
XzCompressor,
XzCompressorStream,
XzDecompressor,
XzDecompressorStream

import TranscodingStreams:
TranscodingStreams,
Expand All @@ -20,4 +20,10 @@ include("liblzma.jl")
include("compression.jl")
include("decompression.jl")

# Deprecations
@deprecate XzCompression XzCompressor
@deprecate XzCompressionStream XzCompressorStream
@deprecate XzDecompression XzDecompressor
@deprecate XzDecompressionStream XzDecompressorStream

end # module
32 changes: 16 additions & 16 deletions src/compression.jl
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
# Compression Codec
# =================
# Compressor Codec
# ================

struct XzCompression <: TranscodingStreams.Codec
struct XzCompressor <: TranscodingStreams.Codec
stream::LZMAStream
preset::UInt32
check::Cint
end

function Base.show(io::IO, codec::XzCompression)
function Base.show(io::IO, codec::XzCompressor)
print(io, summary(codec), "(level=$(codec.preset), check=$(codec.check))")
end

const DEFAULT_COMPRESSION_LEVEL = 6
const DEFAULT_CHECK = LZMA_CHECK_CRC64

"""
XzCompression(;level=$(DEFAULT_COMPRESSION_LEVEL), check=LZMA_CHECK_CRC64)
XzCompressor(;level=$(DEFAULT_COMPRESSION_LEVEL), check=LZMA_CHECK_CRC64)
Create an xz compression codec.
Expand All @@ -24,43 +24,43 @@ Arguments
- `level`: compression level (0..9)
- `check`: integrity check type (`LZMA_CHECK_{NONE,CRC32,CRC64,SHA256}`)
"""
function XzCompression(;level::Integer=DEFAULT_COMPRESSION_LEVEL, check::Cint=DEFAULT_CHECK)
function XzCompressor(;level::Integer=DEFAULT_COMPRESSION_LEVEL, check::Cint=DEFAULT_CHECK)
if !(0 level 9)
throw(ArgumentError("compression level must be within 0..9"))
elseif check (LZMA_CHECK_NONE, LZMA_CHECK_CRC32, LZMA_CHECK_CRC64, LZMA_CHECK_SHA256)
throw(ArgumentError("invalid integrity check"))
end
return XzCompression(LZMAStream(), level, check)
return XzCompressor(LZMAStream(), level, check)
end

const XzCompressionStream{S} = TranscodingStream{XzCompression,S} where S<:IO
const XzCompressorStream{S} = TranscodingStream{XzCompressor,S} where S<:IO

"""
XzCompressionStream(stream::IO; kwargs...)
XzCompressorStream(stream::IO; kwargs...)
Create an xz compression stream (see `XzCompression` for `kwargs`).
Create an xz compression stream (see `XzCompressor` for `kwargs`).
"""
function XzCompressionStream(stream::IO; kwargs...)
return TranscodingStream(XzCompression(;kwargs...), stream)
function XzCompressorStream(stream::IO; kwargs...)
return TranscodingStream(XzCompressor(;kwargs...), stream)
end


# Methods
# -------

function TranscodingStreams.initialize(codec::XzCompression)
function TranscodingStreams.initialize(codec::XzCompressor)
ret = easy_encoder(codec.stream, codec.preset, codec.check)
if ret != LZMA_OK
lzmaerror(codec.stream, ret)
end
return
end

function TranscodingStreams.finalize(codec::XzCompression)
function TranscodingStreams.finalize(codec::XzCompressor)
free(codec.stream)
end

function TranscodingStreams.startproc(codec::XzCompression, mode::Symbol, error::Error)
function TranscodingStreams.startproc(codec::XzCompressor, mode::Symbol, error::Error)
ret = easy_encoder(codec.stream, codec.preset, codec.check)
if ret != LZMA_OK
error[] = ErrorException("xz error")
Expand All @@ -69,7 +69,7 @@ function TranscodingStreams.startproc(codec::XzCompression, mode::Symbol, error:
return :ok
end

function TranscodingStreams.process(codec::XzCompression, input::Memory, output::Memory, error::Error)
function TranscodingStreams.process(codec::XzCompressor, input::Memory, output::Memory, error::Error)
stream = codec.stream
stream.next_in = input.ptr
stream.avail_in = input.size
Expand Down
32 changes: 16 additions & 16 deletions src/decompression.jl
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
# Decompression Codec
# ===================
# Decompressor Codec
# ==================

struct XzDecompression <: TranscodingStreams.Codec
struct XzDecompressor <: TranscodingStreams.Codec
stream::LZMAStream
memlimit::Integer
flags::UInt32
end

function Base.show(io::IO, codec::XzDecompression)
function Base.show(io::IO, codec::XzDecompressor)
print(io, summary(codec), "(memlimit=$(codec.memlimit), flags=$(codec.flags))")
end

const DEFAULT_MEM_LIMIT = typemax(UInt64)

"""
XzDecompression(;memlimit=$(DEFAULT_MEM_LIMIT), flags=LZMA_CONCATENATED)
XzDecompressor(;memlimit=$(DEFAULT_MEM_LIMIT), flags=LZMA_CONCATENATED)
Create an xz decompression codec.
Expand All @@ -23,42 +23,42 @@ Arguments
- `memlimit`: memory usage limit as bytes
- `flags`: decoder flags
"""
function XzDecompression(;memlimit::Integer=DEFAULT_MEM_LIMIT, flags::UInt32=LZMA_CONCATENATED)
function XzDecompressor(;memlimit::Integer=DEFAULT_MEM_LIMIT, flags::UInt32=LZMA_CONCATENATED)
if memlimit 0
throw(ArgumentError("memlimit must be positive"))
end
# NOTE: flags are checked in liblzma
return XzDecompression(LZMAStream(), memlimit, flags)
return XzDecompressor(LZMAStream(), memlimit, flags)
end

const XzDecompressionStream{S} = TranscodingStream{XzDecompression,S} where S<:IO
const XzDecompressorStream{S} = TranscodingStream{XzDecompressor,S} where S<:IO

"""
XzDecompressionStream(stream::IO; kwargs...)
XzDecompressorStream(stream::IO; kwargs...)
Create an xz decompression stream (see `XzDecompression` for `kwargs`).
Create an xz decompression stream (see `XzDecompressor` for `kwargs`).
"""
function XzDecompressionStream(stream::IO; kwargs...)
return TranscodingStream(XzDecompression(;kwargs...), stream)
function XzDecompressorStream(stream::IO; kwargs...)
return TranscodingStream(XzDecompressor(;kwargs...), stream)
end


# Methods
# -------

function TranscodingStreams.initialize(codec::XzDecompression)
function TranscodingStreams.initialize(codec::XzDecompressor)
ret = stream_decoder(codec.stream, codec.memlimit, codec.flags)
if ret != LZMA_OK
lzmaerror(codec.stream, ret)
end
return
end

function TranscodingStreams.finalize(codec::XzDecompression)
function TranscodingStreams.finalize(codec::XzDecompressor)
free(codec.stream)
end

function TranscodingStreams.startproc(codec::XzDecompression, mode::Symbol, error::Error)
function TranscodingStreams.startproc(codec::XzDecompressor, mode::Symbol, error::Error)
ret = stream_decoder(codec.stream, codec.memlimit, codec.flags)
if ret != LZMA_OK
error[] = ErrorException("xz error")
Expand All @@ -67,7 +67,7 @@ function TranscodingStreams.startproc(codec::XzDecompression, mode::Symbol, erro
return :ok
end

function TranscodingStreams.process(codec::XzDecompression, input::Memory, output::Memory, error::Error)
function TranscodingStreams.process(codec::XzDecompressor, input::Memory, output::Memory, error::Error)
stream = codec.stream
stream.next_in = input.ptr
stream.avail_in = input.size
Expand Down
34 changes: 17 additions & 17 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,34 @@ using Base.Test
import TranscodingStreams

@testset "Xz Codec" begin
codec = XzCompression()
@test codec isa XzCompression
@test ismatch(r"^CodecXz.XzCompression\(level=\d, check=\d+\)$", sprint(show, codec))
codec = XzCompressor()
@test codec isa XzCompressor
@test ismatch(r"^CodecXz.XzCompressor\(level=\d, check=\d+\)$", sprint(show, codec))
@test CodecXz.initialize(codec) === nothing
@test CodecXz.finalize(codec) === nothing

codec = XzDecompression()
@test codec isa XzDecompression
@test ismatch(r"^CodecXz.XzDecompression\(memlimit=\d+, flags=\d+\)$", sprint(show, codec))
codec = XzDecompressor()
@test codec isa XzDecompressor
@test ismatch(r"^CodecXz.XzDecompressor\(memlimit=\d+, flags=\d+\)$", sprint(show, codec))
@test CodecXz.initialize(codec) === nothing
@test CodecXz.finalize(codec) === nothing

# Generated by `lzma.compress(b"foo")` on CPython 3.5.2.
data = b"\xfd7zXZ\x00\x00\x04\xe6\xd6\xb4F\x02\x00!\x01\x16\x00\x00\x00t/\xe5\xa3\x01\x00\x02foo\x00\x00X\x15\xa9{,\xe6,\x98\x00\x01\x1b\x03\x0b/\xb9\x10\x1f\xb6\xf3}\x01\x00\x00\x00\x00\x04YZ"
@test read(XzDecompressionStream(IOBuffer(data))) == b"foo"
@test read(XzDecompressionStream(IOBuffer(vcat(data, data)))) == b"foofoo"
@test read(XzDecompressorStream(IOBuffer(data))) == b"foo"
@test read(XzDecompressorStream(IOBuffer(vcat(data, data)))) == b"foofoo"
# corrupt data
data[[1,3,5]] = b"bug"
@test_throws ErrorException read(XzDecompressionStream(IOBuffer(data)))
@test_throws ErrorException read(XzDecompressorStream(IOBuffer(data)))

@test XzCompressionStream <: TranscodingStreams.TranscodingStream
@test XzDecompressionStream <: TranscodingStreams.TranscodingStream
@test XzCompressorStream <: TranscodingStreams.TranscodingStream
@test XzDecompressorStream <: TranscodingStreams.TranscodingStream

TranscodingStreams.test_roundtrip_read(XzCompressionStream, XzDecompressionStream)
TranscodingStreams.test_roundtrip_write(XzCompressionStream, XzDecompressionStream)
TranscodingStreams.test_roundtrip_lines(XzCompressionStream, XzDecompressionStream)
TranscodingStreams.test_roundtrip_transcode(XzCompression, XzDecompression)
TranscodingStreams.test_roundtrip_read(XzCompressorStream, XzDecompressorStream)
TranscodingStreams.test_roundtrip_write(XzCompressorStream, XzDecompressorStream)
TranscodingStreams.test_roundtrip_lines(XzCompressorStream, XzDecompressorStream)
TranscodingStreams.test_roundtrip_transcode(XzCompressor, XzDecompressor)

@test_throws ArgumentError XzCompression(level=10)
@test_throws ArgumentError XzDecompression(memlimit=0)
@test_throws ArgumentError XzCompressor(level=10)
@test_throws ArgumentError XzDecompressor(memlimit=0)
end

0 comments on commit b231e33

Please sign in to comment.