/** * Prepare the contents for sending again */ public final void rewind() { finishWriting(); this.size = 0; if (this.chunks != null) { for (ByteBuffer bb: this.chunks) { bb.rewind(); size += bb.remaining(); } } this.buffer.rewind(); size += this.buffer.remaining(); }
public HDInputStream() { finishWriting(); if (HeapDataOutputStream.this.chunks != null) { this.chunkIt = HeapDataOutputStream.this.chunks.iterator(); nextChunk(); } else { this.chunkIt = null; this.bb = HeapDataOutputStream.this.buffer; } } private void nextChunk() {
/** * Free up any unused memory */ public final void trim() { finishWriting(); if (this.buffer.limit() < this.buffer.capacity()) { // buffer is less than half full so allocate a new one and copy it in ByteBuffer bb = ByteBuffer.allocate(this.buffer.limit()); bb.put(this.buffer); bb.flip(); // now ready for reading this.buffer = bb; } }
/** * Prepare the contents for sending again */ public final void rewind() { finishWriting(); this.size = 0; if (this.chunks != null) { for (ByteBuffer bb: this.chunks) { bb.rewind(); size += bb.remaining(); } } this.buffer.rewind(); size += this.buffer.remaining(); }
/** * Free up any unused memory */ public final void trim() { finishWriting(); if (this.buffer.limit() < this.buffer.capacity()) { // buffer is less than half full so allocate a new one and copy it in ByteBuffer bb = ByteBuffer.allocate(this.buffer.limit()); bb.put(this.buffer); bb.flip(); // now ready for reading this.buffer = bb; } this.reuseChunks = null; }
/** gets the contents of this stream as s ByteBuffer, ready for reading. * The stream should not be written to past this point until it has been reset. */ public final ByteBuffer toByteBuffer() { finishWriting(); consolidateChunks(); return this.buffer; } /** gets the contents of this stream as a byte[].
/** gets the contents of this stream as s ByteBuffer, ready for reading. * The stream should not be written to past this point until it has been reset. */ public final ByteBuffer toByteBuffer() { finishWriting(); consolidateChunks(); return this.buffer; } /** gets the contents of this stream as a byte[].
public HDInputStream() { finishWriting(); if (HeapDataOutputStream.this.chunks != null) { this.chunkIt = HeapDataOutputStream.this.chunks.iterator(); nextChunk(); } else { this.chunkIt = null; this.bb = HeapDataOutputStream.this.buffer; } } private void nextChunk() {
/** * Returns a ByteBuffer of the unused buffer; returns null if the buffer was completely used. */ public ByteBuffer finishWritingAndReturnUnusedBuffer() { finishWriting(); ByteBuffer result = this.buffer.duplicate(); if (result.remaining() == 0) { // buffer was never used. result.limit(result.capacity()); return result; } int newPos = result.limit(); if ((result.capacity() - newPos) > 0) { result.limit(result.capacity()); result.position(newPos); return result.slice(); } else { return null; } }
/** * Write this stream to the specified channel. Call multiple times until size returns zero to make sure all bytes in the stream have been written. * @return the number of bytes written, possibly zero. * @throws IOException if channel is closed, not yet connected, or some other I/O error occurs. */ public final int sendTo(SocketChannel chan) throws IOException { finishWriting(); if (size() == 0) { return 0; } int result; if (this.chunks != null) { ByteBuffer[] bufs = new ByteBuffer[this.chunks.size()+1]; bufs = this.chunks.toArray(bufs); bufs[this.chunks.size()] = this.buffer; result = (int)chan.write(bufs); } else { result = chan.write(this.buffer); } this.size -= result; return result; }
/** * Write this stream to the specified channel. Call multiple times until size returns zero to make sure all bytes in the stream have been written. * @return the number of bytes written, possibly zero. * @throws IOException if channel is closed, not yet connected, or some other I/O error occurs. */ public final int sendTo(SocketChannel chan) throws IOException { finishWriting(); if (size() == 0) { return 0; } int result; if (this.chunks != null) { ByteBuffer[] bufs = new ByteBuffer[this.chunks.size()+1]; bufs = this.chunks.toArray(bufs); bufs[this.chunks.size()] = this.buffer; result = (int)chan.write(bufs); } else { result = chan.write(this.buffer); } this.size -= result; return result; }
finishWriting(); if (this.chunks != null) { for (ByteBuffer bb: this.chunks) {
/** * Write the contents of this stream to the specified stream. */ public final void sendTo(ByteBufferWriter out) { finishWriting(); if (this.chunks != null) { for (ByteBuffer bb: this.chunks) { basicSendTo(out, bb); } } basicSendTo(out, this.buffer); }
/** * Write the contents of this stream to the byte buffer. * @throws BufferOverflowException if out is not large enough to contain all of * our data. */ public final void sendTo(ByteBuffer out) { finishWriting(); if (out.remaining() < size()) { throw new BufferOverflowException(); } if (this.chunks != null) { for (ByteBuffer bb: this.chunks) { int bytesToWrite = bb.remaining(); if (bytesToWrite > 0) { out.put(bb); this.size -= bytesToWrite; } } } { ByteBuffer bb = this.buffer; int bytesToWrite = bb.remaining(); if (bytesToWrite > 0) { out.put(bb); this.size -= bytesToWrite; } } }
/** * Write the contents of this stream to the byte buffer. * @throws BufferOverflowException if out is not large enough to contain all of * our data. */ public final void sendTo(ByteBuffer out) { finishWriting(); if (out.remaining() < size()) { throw new BufferOverflowException(); } if (this.chunks != null) { for (ByteBuffer bb: this.chunks) { int bytesToWrite = bb.remaining(); if (bytesToWrite > 0) { out.put(bb); this.size -= bytesToWrite; } } } { ByteBuffer bb = this.buffer; int bytesToWrite = bb.remaining(); if (bytesToWrite > 0) { out.put(bb); this.size -= bytesToWrite; } } }
finishWriting(); int bytesWritten = 0; if (buffer.length < (offset + size())) {
public final void sendTo(SocketChannel chan, ByteBuffer out) throws IOException { finishWriting(); if (size() == 0) { return; } if (this.chunks != null) { for (ByteBuffer bb: this.chunks) { sendChunkTo(bb, chan, out); } } sendChunkTo(this.buffer, chan, out); }
/** * Write the contents of this stream to the specified stream using * outBuf if a buffer is needed. */ public final void sendTo(OutputStream out, ByteBuffer outBuf) throws IOException { finishWriting(); if (this.chunks != null) { for (ByteBuffer bb: this.chunks) { sendTo(out, outBuf, bb); } } sendTo(out, outBuf, this.buffer); flushStream(out, outBuf); }
public final void sendTo(SocketChannel chan, ByteBuffer out) throws IOException { finishWriting(); if (size() == 0) { return; } // TODO OFFHEAP: this code end up calling chan.write at least once for each DirectByteBuffer in this HDOS. // It will combine consecutive heap ByteBuffers into a write call. // It would be better to do one chan.write call with an array of ByteBuffers like sendTo(SocketChannel) does. out.clear(); if (this.chunks != null) { for (ByteBuffer bb: this.chunks) { sendChunkTo(bb, chan, out); } } sendChunkTo(this.buffer, chan, out); flushBuffer(chan, out); }
if (v instanceof HeapDataOutputStream) { HeapDataOutputStream other = (HeapDataOutputStream)v; other.finishWriting(); InternalDataSerializer.writeArrayLength(other.size(), this); if (this.doNotCopy) {