Skip to content

Commit

Permalink
Add ChannelBufferByte(Input|Output)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jin Kwon committed May 13, 2020
1 parent c6948bc commit 2fd0f13
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

A library for reading/writing non octet aligned values such as `1-bit boolean` or `17-bit unsigned int`.

See [bit-io2](https://github.com/jinahya/bit-io2) for Java8+ flavored version.

## Specifications

#### boolean
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/github/jinahya/bit/io/BitInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ public interface BitInput {
* Skips specified number of bits by discarding bits.
*
* @param bits the number of bit to skip; must be positive.
* @throws IOException if an I/O error occurs.
* @throws IllegalArgumentException if {@code bits} is not positive.
* @throws IOException if an I/O error occurs.
*/
void skip(int bits) throws IOException;

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/github/jinahya/bit/io/BitOutput.java
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ public interface BitOutput {
* Skips specified number of bits by padding zero bits.
*
* @param bits the number of bit to skip; must be positive.
* @throws IOException if an I/O error occurs.
* @throws IllegalArgumentException if {@code bits} is not positive.
* @throws IOException if an I/O error occurs.
*/
void skip(int bits) throws IOException;

Expand Down
37 changes: 37 additions & 0 deletions src/main/java/com/github/jinahya/bit/io/BufferByteInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@
* #L%
*/

import java.io.EOFException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;

import static java.nio.ByteBuffer.allocate;

/**
* A byte input reads bytes from a {@link ByteBuffer}.
Expand All @@ -31,6 +35,39 @@
*/
public class BufferByteInput extends AbstractByteInput<ByteBuffer> {

private static class ChannelBufferByteInput extends BufferByteInput {

private ChannelBufferByteInput(final ByteBuffer source, final ReadableByteChannel channel) {
super(source);
if (channel == null) {
throw new NullPointerException("channel is null");
}
this.channel = channel;
}

@Override
public int read() throws IOException {
final ByteBuffer source = getSource();
if (!source.hasRemaining()) {
source.clear(); // position -> zero, limit -> capacity
}
while (source.hasRemaining()) {
source.clear();
if (channel.read(source) == -1) {
throw new EOFException("reached to an end");
}
source.flip();
}
return super.read();
}

private final ReadableByteChannel channel;
}

public static BufferByteInput from(final ReadableByteChannel channel) {
return new ChannelBufferByteInput((ByteBuffer) allocate(1).position(1), channel);
}

// -----------------------------------------------------------------------------------------------------------------

/**
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/com/github/jinahya/bit/io/BufferByteOutput.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.WritableByteChannel;

import static java.nio.ByteBuffer.allocate;

/**
* A byte output writes bytes to a {@link ByteBuffer}.
Expand All @@ -31,6 +34,35 @@
*/
public class BufferByteOutput extends AbstractByteOutput<ByteBuffer> {

// -----------------------------------------------------------------------------------------------------------------
private static class ChannelBufferByteOutput extends BufferByteOutput {

private ChannelBufferByteOutput(final ByteBuffer target, final WritableByteChannel channel) {
super(target);
if (channel == null) {
throw new NullPointerException("channel is null");
}
this.channel = channel;
}

@Override
public void write(final int value) throws IOException {
super.write(value);
final ByteBuffer target = getTarget();
while (!target.hasRemaining()) {
target.flip();
channel.write(target);
target.compact();
}
}

private final WritableByteChannel channel;
}

public static BufferByteOutput from(final WritableByteChannel channel) {
return new ChannelBufferByteOutput(allocate(1), channel);
}

// -----------------------------------------------------------------------------------------------------------------

/**
Expand Down

0 comments on commit 2fd0f13

Please sign in to comment.