/** Read a buffer from the input without copying, if possible. * @throws EOFException if EOF is reached before reading all the bytes. */ public ByteBuffer readBuffer(int length) throws IOException { if (length == 0) return ByteBuffer.allocate(0); ByteBuffer buffer = getBuffer(); if (buffer.remaining() == length) { // can return current as-is? current++; return buffer; // return w/o copying } // punt: allocate a new buffer & copy into it ByteBuffer result = ByteBuffer.allocate(length); int start = 0; while (start < length) start += read(result.array(), start, length-start); return result; }
/** Read a buffer from the input without copying, if possible. * @throws EOFException if EOF is reached before reading all the bytes. */ public ByteBuffer readBuffer(int length) throws IOException { if (length == 0) return ByteBuffer.allocate(0); ByteBuffer buffer = getBuffer(); if (buffer.remaining() == length) { // can return current as-is? current++; return buffer; // return w/o copying } // punt: allocate a new buffer & copy into it ByteBuffer result = ByteBuffer.allocate(length); int start = 0; while (start < length) start += read(result.array(), start, length-start); return result; }
/** Read a buffer from the input without copying, if possible. * @throws EOFException if EOF is reached before reading all the bytes. */ public ByteBuffer readBuffer(int length) throws IOException { if (length == 0) return ByteBuffer.allocate(0); ByteBuffer buffer = getBuffer(); if (buffer.remaining() == length) { // can return current as-is? current++; return buffer; // return w/o copying } // punt: allocate a new buffer & copy into it ByteBuffer result = ByteBuffer.allocate(length); int start = 0; while (start < length) start += read(result.array(), start, length-start); return result; }
/** Read a buffer from the input without copying, if possible. * @throws EOFException if EOF is reached before reading all the bytes. */ public ByteBuffer readBuffer(int length) throws IOException { if (length == 0) return ByteBuffer.allocate(0); ByteBuffer buffer = getBuffer(); if (buffer.remaining() == length) { // can return current as-is? current++; return buffer; // return w/o copying } // punt: allocate a new buffer & copy into it ByteBuffer result = ByteBuffer.allocate(length); int start = 0; while (start < length) start += read(result.array(), start, length-start); return result; }