Skip to content

Commit

Permalink
Implemented a Bzip2Encoder
Browse files Browse the repository at this point in the history
Motivation:

Bzip2Encoder provides sending data compressed in bzip2 format.

Modifications:

Added classes:
- Bzip2Encoder
- Bzip2BitWriter
- Bzip2BlockCompressor
- Bzip2DivSufSort
- Bzip2HuffmanAllocator
- Bzip2HuffmanStageEncoder
- Bzip2MTFAndRLE2StageEncoder
- Bzip2EncoderTest

Modified classes:
- Bzip2Constants (splited BLOCK_HEADER_MAGIC and END_OF_STREAM_MAGIC)
- Bzip2Decoder (use splited magic numbers)

Added integration tests for Bzip2Encoder/Decoder

Result:

Implemented new encoder which can compress outgoing data in bzip2 format.
  • Loading branch information
idelpivnitskiy authored and Norman Maurer committed Jul 17, 2014
1 parent 2b37b69 commit 401a6db
Show file tree
Hide file tree
Showing 16 changed files with 3,920 additions and 31 deletions.
9 changes: 9 additions & 0 deletions NOTICE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ and decompression library written by Matthew J. Francis. It can be obtained at:
* HOMEPAGE:
* https://code.google.com/p/jbzip2/

This product contains a modified portion of 'libdivsufsort', a C API library to construct
the suffix array and the Burrows-Wheeler transformed string for any input string of
a constant-size alphabet written by Yuta Mori. It can be obtained at:

* LICENSE:
* license/LICENSE.libdivsufsort.txt (MIT License)
* HOMEPAGE:
* https://code.google.com/p/libdivsufsort/

This product optionally depends on 'JZlib', a re-implementation of zlib in
pure Java, which can be obtained at:

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Copyright 2014 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.handler.codec.compression;

import io.netty.buffer.ByteBuf;

/**
* A bit writer that allows the writing of single bit booleans, unary numbers, bit strings
* of arbitrary length (up to 24 bits), and bit aligned 32-bit integers. A single byte at a
* time is written to the {@link ByteBuf} when sufficient bits have been accumulated.
*/
final class Bzip2BitWriter {
/**
* A buffer of bits waiting to be written to the output stream.
*/
private int bitBuffer;

/**
* The number of bits currently buffered in {@link #bitBuffer}.
*/
private int bitCount;

/**
* Writes up to 24 bits to the output {@link ByteBuf}.
* @param count The number of bits to write (maximum {@code 24}, because the {@link #bitBuffer}
* is {@code int} and it can store up to {@code 8} bits before calling)
* @param value The bits to write
*/
void writeBits(ByteBuf out, final int count, final int value) {
if (count < 0 || count > 24) {
throw new IllegalArgumentException("count: " + count + " (expected: 0-24)");
}
int bitCount = this.bitCount;
int bitBuffer = this.bitBuffer | (value << (32 - count)) >>> bitCount;
bitCount += count;

while (bitCount >= 8) {
out.writeByte(bitBuffer >>> 24);
bitBuffer <<= 8;
bitCount -= 8;
}
this.bitBuffer = bitBuffer;
this.bitCount = bitCount;
}

/**
* Writes a single bit to the output {@link ByteBuf}.
* @param value The bit to write
*/
void writeBoolean(ByteBuf out, final boolean value) {
int bitCount = this.bitCount + 1;
int bitBuffer = this.bitBuffer | (value ? 1 : 0) << (32 - bitCount);

if (bitCount == 8) {
out.writeByte(bitBuffer >>> 24);
bitBuffer = 0;
bitCount = 0;
}
this.bitBuffer = bitBuffer;
this.bitCount = bitCount;
}

/**
* Writes a zero-terminated unary number to the output {@link ByteBuf}.
* Example of the output for value = 6: {@code 1111110}
* @param value The number of {@code 1} to write
*/
void writeUnary(ByteBuf out, int value) {
if (value < 0) {
throw new IllegalArgumentException("value: " + value + " (expected 0 or more)");
}
while (value-- > 0) {
writeBoolean(out, true);
}
writeBoolean(out, false);
}

/**
* Writes an integer as 32 bits to the output {@link ByteBuf}.
* @param value The integer to write
*/
void writeInt(ByteBuf out, final int value) {
writeBits(out, 16, (value >>> 16) & 0xffff);
writeBits(out, 16, value & 0xffff);
}

/**
* Writes any remaining bits to the output {@link ByteBuf},
* zero padding to a whole byte as required.
*/
void flush(ByteBuf out) {
if (bitCount > 0) {
writeBits(out, 8 - bitCount, 0);
}
}
}
Loading

0 comments on commit 401a6db

Please sign in to comment.