Skip to content
Snippets Groups Projects
Commit 562b35ac authored by Vojtech Moravec's avatar Vojtech Moravec
Browse files

InputBitStream.

parent 8aeb9e04
No related branches found
No related tags found
No related merge requests found
import cli.CliConstants; import cli.CliConstants;
import cli.ParsedCliOptions; import cli.ParsedCliOptions;
import compression.io.InBitStream;
import compression.io.OutBitStream; import compression.io.OutBitStream;
import org.apache.commons.cli.*; import org.apache.commons.cli.*;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
public class DataCompressor { public class DataCompressor {
...@@ -11,7 +14,8 @@ public class DataCompressor { ...@@ -11,7 +14,8 @@ public class DataCompressor {
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
OutBitStream bitStream = new OutBitStream(null, 3, 64); ByteArrayOutputStream outStream = new ByteArrayOutputStream();
OutBitStream bitStream = new OutBitStream(outStream, 3, 64);
bitStream.write(0); bitStream.write(0);
bitStream.write(1); bitStream.write(1);
bitStream.write(2); bitStream.write(2);
...@@ -20,9 +24,27 @@ public class DataCompressor { ...@@ -20,9 +24,27 @@ public class DataCompressor {
bitStream.write(5); bitStream.write(5);
bitStream.write(6); bitStream.write(6);
bitStream.write(7); bitStream.write(7);
bitStream.write(7);
bitStream.write(6);
bitStream.write(5);
bitStream.forceFlush(); bitStream.forceFlush();
final byte[] data = outStream.toByteArray();
ByteArrayInputStream inStream = new ByteArrayInputStream(data);
InBitStream inBitStream = new InBitStream(inStream, 3, 64);
final int i0 = inBitStream.readValue();
final int i1 = inBitStream.readValue();
final int i2 = inBitStream.readValue();
final int i3 = inBitStream.readValue();
final int i4 = inBitStream.readValue();
final int i5 = inBitStream.readValue();
final int i6 = inBitStream.readValue();
final int i7 = inBitStream.readValue();
final int i8 = inBitStream.readValue();
final int i9 = inBitStream.readValue();
final int i10 = inBitStream.readValue();
Options options = getOptions(); Options options = getOptions();
HelpFormatter formatter = new HelpFormatter(); HelpFormatter formatter = new HelpFormatter();
......
package compression.io;
import java.io.IOException;
import java.io.InputStream;
public class InBitStream {
private InputStream inputStream;
private byte[] buffer;
private int bufferPosition;
private int bytesAvailable;
private byte bitBuffer;
private byte bitBufferSize;
private final int bitsPerValue;
public InBitStream(InputStream inputStream, final int bitsPerValue, final int bufferSize) {
this.inputStream = inputStream;
this.bitsPerValue = bitsPerValue;
buffer = new byte[bufferSize];
bufferPosition = 0;
bytesAvailable = 0;
bitBuffer = 0;
bitBufferSize = 0;
}
private void readByteToBitBuffer() throws IOException {
if (!(bufferPosition < bytesAvailable)) {
bytesAvailable = inputStream.read(buffer, 0, buffer.length);
bufferPosition = 0;
}
if (bufferPosition < bytesAvailable) {
bitBuffer = buffer[bufferPosition++];
bitBufferSize = 8;
} else {
assert (false) : "Underlying buffer is empty.";
}
}
private int readBit() throws IOException {
if (bitBufferSize == 0) {
readByteToBitBuffer();
}
--bitBufferSize;
int bit = bitBuffer & (1 << bitBufferSize);
return (bit > 0 ? 1 : 0);
}
// boolean canRead() {
// birb
// }
public int readValue() throws IOException {
int result = 0;
int bit;
//writing => bit = (value & (1 << shift));
for (int shift = 0; shift < bitsPerValue; shift++) {
bit = readBit();
// FIXME
result |= (bit << shift);
}
return result;
}
/**/
}
...@@ -15,6 +15,8 @@ public class OutBitStream { ...@@ -15,6 +15,8 @@ public class OutBitStream {
private final int bitsPerValue; private final int bitsPerValue;
public OutBitStream(OutputStream outputStream, final int bitsPerValue, final int bufferSize) { public OutBitStream(OutputStream outputStream, final int bitsPerValue, final int bufferSize) {
outStream = outputStream;
this.bitsPerValue = bitsPerValue; this.bitsPerValue = bitsPerValue;
buffer = new byte[bufferSize]; buffer = new byte[bufferSize];
...@@ -22,8 +24,6 @@ public class OutBitStream { ...@@ -22,8 +24,6 @@ public class OutBitStream {
bitBuffer = 0; bitBuffer = 0;
bitBufferSize = 0; bitBufferSize = 0;
outStream = outputStream;
} }
/** /**
...@@ -62,12 +62,15 @@ public class OutBitStream { ...@@ -62,12 +62,15 @@ public class OutBitStream {
* @param bit True for 1 * @param bit True for 1
*/ */
private void writeBit(final int bit) throws IOException { private void writeBit(final int bit) throws IOException {
// ++bitBufferSize; ++bitBufferSize;
if (bit > 0) { if (bit > 0) {
// bitBuffer |= (1 << (8 - bitBufferSize)); bitBuffer |= (1 << (8 - bitBufferSize));
bitBuffer |= (1 << bitBufferSize);
} }
++bitBufferSize;
// if (bit > 0) {
// bitBuffer |= (1 << bitBufferSize);
// }
// ++bitBufferSize;
if (bitBufferSize == 8) { if (bitBufferSize == 8) {
flushBitBuffer(); flushBitBuffer();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment