hm.binkley.xio.XByteBufferInputOutputStream Maven / Gradle / Ivy
The newest version!
package hm.binkley.xio;
import hm.binkley.util.Bug;
import javax.annotation.Nonnull;
import java.io.IOException;
import java.nio.ByteBuffer;
/**
* {@code XByteArrayInputOutputStream} is a blend of {@link XInputOutputStream}
* and {@link XSeekable} over a byte buffer.
*
* @author B. K. Oxley (binkley)
*/
public class XByteBufferInputOutputStream
implements XInputOutputStream, XSeekable {
private final ByteBuffer buf;
private int mark;
/**
* Constructs a new {@code XByteBufferInputOutputStream} for the given byte
* buffer.
*
* @param buf the byte buffer, never missing
*/
public XByteBufferInputOutputStream(@Nonnull final ByteBuffer buf) {
this.buf = buf;
}
@Override
public int read() {
return buf.get();
}
@Override
public void write(final int b)
throws IOException {
buf.put((byte) b);
}
@Override
public void write(@Nonnull final byte[] b, final int off, final int len)
throws IOException {
buf.put(b, off, len);
}
@Override
public long mark() {
return mark;
}
@Override
public void mark(final long mark) {
this.mark = (int) mark;
}
@Override
public long seek(final long offset, @Nonnull final Whence whence)
throws IOException {
try {
switch (whence) {
case SET:
buf.position((int) offset);
break;
case CUR:
buf.position((int) offset + buf.position());
break;
case END:
buf.position((int) offset + buf.limit());
break;
default:
throw new Bug("Missing case: " + whence);
}
return buf.position();
} catch (final IllegalArgumentException e) {
throw new IOException(e);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy