InputStreamを受け取り、そこからデータを読み取るメソッドがあります。このメソッドをByteBufferでも使用したいと思います。ByteBufferをラップして、ストリームとしてアクセスできるようにする方法はありますか?
6 に答える
Thiloが参照している実装にはいくつかのバグがあるようです。また、他のサイトにそのままコピーして貼り付けます。
ByteBufferBackedInputStream.read()
読み取ったバイトの符号拡張int表現を返しますが、これは誤りです(値は[-1..255]の範囲内にある必要があります)ByteBufferBackedInputStream.read(byte[], int, int)
API仕様に従って、バッファにバイトが残っていない場合は-1を返しません
ByteBufferBackedOutputStreamは比較的健全なようです。
以下に「修正済み」バージョンを示します。さらにバグを見つけた場合(または誰かが指摘した場合)、ここで更新します。
更新:synchronized
読み取り/書き込みメソッドからキーワードを削除
InputStream
public class ByteBufferBackedInputStream extends InputStream {
ByteBuffer buf;
public ByteBufferBackedInputStream(ByteBuffer buf) {
this.buf = buf;
}
public int read() throws IOException {
if (!buf.hasRemaining()) {
return -1;
}
return buf.get() & 0xFF;
}
public int read(byte[] bytes, int off, int len)
throws IOException {
if (!buf.hasRemaining()) {
return -1;
}
len = Math.min(len, buf.remaining());
buf.get(bytes, off, len);
return len;
}
}
OutputStream
public class ByteBufferBackedOutputStream extends OutputStream {
ByteBuffer buf;
public ByteBufferBackedOutputStream(ByteBuffer buf) {
this.buf = buf;
}
public void write(int b) throws IOException {
buf.put((byte) b);
}
public void write(byte[] bytes, int off, int len)
throws IOException {
buf.put(bytes, off, len);
}
}
JDKには何もありませんが、ByteBufferInputStreamのグーグルで実装がたくさんあります。基本的に、それらは1つ以上のByteBufferをラップし、すでに読み取られた量を記録するインデックスを追跡します。このようなものがたくさん出てきますが、明らかにバグがあります。改善されたバージョンについては、@ Mike Houstonの回答を参照してください)。
バイト配列に裏打ちされている場合は、を使用して、ByteArrayInputStream
を介してバイト配列を取得できますByteBuffer.array()
。ネイティブのByteBufferで試行している場合、これは例外をスローします。
可能な場合はヒープバッファ(バイト配列)を直接使用し、そうでない場合はラップされたバイトバッファを使用します(回答Mike Houstonを参照)
public static InputStream asInputStream(ByteBuffer buffer) {
if (buffer.hasArray()) {
// use heap buffer; no array is created; only the reference is used
return new ByteArrayInputStream(buffer.array());
}
return new ByteBufferInputStream(buffer);
}
また、ラップされたバッファは、マーク/リセットおよびスキップ操作を効率的にサポートできることに注意してください。
これは私のバージョンとInputStream
実装OutputStream
です:
ByteBufferBackedInputStream
:
public class ByteBufferBackedInputStream extends InputStream
{
private ByteBuffer backendBuffer;
public ByteBufferBackedInputStream(ByteBuffer backendBuffer) {
Objects.requireNonNull(backendBuffer, "Given backend buffer can not be null!");
this.backendBuffer = backendBuffer;
}
public void close() throws IOException {
this.backendBuffer = null;
}
private void ensureStreamAvailable() throws IOException {
if (this.backendBuffer == null) {
throw new IOException("read on a closed InputStream!");
}
}
@Override
public int read() throws IOException {
this.ensureStreamAvailable();
return this.backendBuffer.hasRemaining() ? this.backendBuffer.get() & 0xFF : -1;
}
@Override
public int read(@Nonnull byte[] buffer) throws IOException {
return this.read(buffer, 0, buffer.length);
}
@Override
public int read(@Nonnull byte[] buffer, int offset, int length) throws IOException {
this.ensureStreamAvailable();
Objects.requireNonNull(buffer, "Given buffer can not be null!");
if (offset >= 0 && length >= 0 && length <= buffer.length - offset) {
if (length == 0) {
return 0;
}
else {
int remainingSize = Math.min(this.backendBuffer.remaining(), length);
if (remainingSize == 0) {
return -1;
}
else {
this.backendBuffer.get(buffer, offset, remainingSize);
return remainingSize;
}
}
}
else {
throw new IndexOutOfBoundsException();
}
}
public long skip(long n) throws IOException {
this.ensureStreamAvailable();
if (n <= 0L) {
return 0L;
}
int length = (int) n;
int remainingSize = Math.min(this.backendBuffer.remaining(), length);
this.backendBuffer.position(this.backendBuffer.position() + remainingSize);
return (long) length;
}
public int available() throws IOException {
this.ensureStreamAvailable();
return this.backendBuffer.remaining();
}
public synchronized void mark(int var1) {
}
public synchronized void reset() throws IOException {
throw new IOException("mark/reset not supported");
}
public boolean markSupported() {
return false;
}
}
ByteBufferBackedOutputStream
:
public class ByteBufferBackedOutputStream extends OutputStream
{
private ByteBuffer backendBuffer;
public ByteBufferBackedOutputStream(ByteBuffer backendBuffer) {
Objects.requireNonNull(backendBuffer, "Given backend buffer can not be null!");
this.backendBuffer = backendBuffer;
}
public void close() throws IOException {
this.backendBuffer = null;
}
private void ensureStreamAvailable() throws IOException {
if (this.backendBuffer == null) {
throw new IOException("write on a closed OutputStream");
}
}
@Override
public void write(int b) throws IOException {
this.ensureStreamAvailable();
backendBuffer.put((byte) b);
}
@Override
public void write(@Nonnull byte[] bytes) throws IOException {
this.write(bytes, 0, bytes.length);
}
@Override
public void write(@Nonnull byte[] bytes, int off, int len) throws IOException {
this.ensureStreamAvailable();
Objects.requireNonNull(bytes, "Given buffer can not be null!");
if ((off < 0) || (off > bytes.length) || (len < 0) ||
((off + len) > bytes.length) || ((off + len) < 0))
{
throw new IndexOutOfBoundsException();
}
else if (len == 0) {
return;
}
backendBuffer.put(bytes, off, len);
}
}
ByteArrayInputStreamコードの派生物に基づく...提供されたByteBufferには、必要に応じて事前に位置と制限を正しく設定する必要があります。
public class ByteBufferInputStream extends InputStream
{
/**
* The input ByteBuffer that was provided.
* The ByteBuffer should be supplied with position and limit correctly set as appropriate
*/
protected ByteBuffer buf;
public ByteBufferInputStream(ByteBuffer buf)
{
this.buf = buf;
buf.mark(); // to prevent java.nio.InvalidMarkException on InputStream.reset() if mark had not been set
}
/**
* Reads the next byte of data from this ByteBuffer. The value byte is returned as an int in the range 0-255.
* If no byte is available because the end of the buffer has been reached, the value -1 is returned.
* @return the next byte of data, or -1 if the limit/end of the buffer has been reached.
*/
public int read()
{
return buf.hasRemaining()
? (buf.get() & 0xff)
: -1;
}
/**
* Reads up to len bytes of data into an array of bytes from this ByteBuffer.
* If the buffer has no remaining bytes, then -1 is returned to indicate end of file.
* Otherwise, the number k of bytes read is equal to the smaller of len and buffer remaining.
* @param b the buffer into which the data is read.
* @param off the start offset in the destination array b
* @param len the maximum number of bytes read.
* @return the total number of bytes read into the buffer, or -1 if there is no more data because the limit/end of
* the ByteBuffer has been reached.
* @exception NullPointerException If b is null.
* @exception IndexOutOfBoundsException If off is negative, len is negative, or len is greater than b.length - off
*/
public int read(byte b[], int off, int len)
{
if (b == null)
{
throw new NullPointerException();
}
else if (off < 0 || len < 0 || len > b.length - off)
{
throw new IndexOutOfBoundsException();
}
if (!buf.hasRemaining())
{
return -1;
}
int remaining = buf.remaining();
if (len > remaining)
{
len = remaining;
}
if (len <= 0)
{
return 0;
}
buf.get(b, off, len);
return len;
}
/**
* Skips n bytes of input from this ByteBuffer. Fewer bytes might be skipped if the limit is reached.
*
* @param n the number of bytes to be skipped.
* @return the actual number of bytes skipped.
*/
public long skip(long n)
{
int skipAmount = (n < 0)
? 0
: ((n > Integer.MAX_VALUE)
? Integer.MAX_VALUE
: (int) n);
if (skipAmount > buf.remaining())
{
skipAmount = buf.remaining();
}
int newPos = buf.position() + skipAmount;
buf.position(newPos);
return skipAmount;
}
/**
* Returns remaining bytes available in this ByteBuffer
* @return the number of remaining bytes that can be read (or skipped over) from this ByteBuffer.
*/
public int available()
{
return buf.remaining();
}
public boolean markSupported()
{
return true;
}
/**
* Set the current marked position in the ByteBuffer.
* <p> Note: The readAheadLimit for this class has no meaning.
*/
public void mark(int readAheadLimit)
{
buf.mark();
}
/**
* Resets the ByteBuffer to the marked position.
*/
public void reset()
{
buf.reset();
}
/**
* Closing a ByteBuffer has no effect.
* The methods in this class can be called after the stream has been closed without generating an IOException.
*/
public void close() throws IOException
{
}
}