ByteBufferListOutputStream bbos = new ByteBufferListOutputStream(pool); encodeCellsTo(bbos, cellScanner, codec, compressor); if (bbos.size() == 0) { bbos.releaseResources(); return null;
public ByteBufferListOutputStream(ByteBufferPool pool) { this.pool = pool; allocateNewBuffer(); }
/** * Release the resources it uses (The ByteBuffers) which are obtained from pool. Call this only * when all the data is fully used. And it must be called at the end of usage else we will leak * ByteBuffers from pool. */ public void releaseResources() { try { close(); } catch (IOException e) { LOG.debug(e.toString(), e); } // Return back all the BBs to pool if (this.bufsFromPool != null) { for (int i = 0; i < this.bufsFromPool.size(); i++) { this.pool.putbackBuffer(this.bufsFromPool.get(i)); } this.bufsFromPool = null; } this.allBufs = null; this.curBuf = null; }
@Test public void testWrites() throws Exception { ByteBufferPool pool = new ByteBufferPool(10, 3); ByteBufferListOutputStream bbos = new ByteBufferListOutputStream(pool); bbos.write(2);// Write a byte bbos.writeInt(100);// Write an int byte[] b = Bytes.toBytes("row123");// 6 bytes bbos.write(b); bbos.write(bb, 0, bb.capacity()); pool.putbackBuffer(bb1); bbos.writeInt(123); bbos.writeInt(124); assertEquals(0, pool.getQueueSize()); List<ByteBuffer> allBufs = bbos.getByteBuffers(); assertEquals(4, allBufs.size()); assertEquals(3, bbos.bufsFromPool.size()); assertEquals(4, b4.remaining()); assertEquals(124, b4.getInt()); bbos.releaseResources(); assertEquals(3, pool.getQueueSize());
/** * Call is done. Execution happened and we returned results to client. It is * now safe to cleanup. */ @edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "IS2_INCONSISTENT_SYNC", justification = "Presume the lock on processing request held by caller is protection enough") @Override public void done() { if (this.cellBlockStream != null) { // This will return back the BBs which we got from pool. this.cellBlockStream.releaseResources(); this.cellBlockStream = null; } // If the call was run successfuly, we might have already returned the BB // back to pool. No worries..Then inputCellBlock will be null cleanup(); }
this.connection.compressionCodec, cells, this.reservoir); if (this.cellBlockStream != null) { cellBlock = this.cellBlockStream.getByteBuffers(); cellBlockSize = this.cellBlockStream.size();
@Test public void testWrites() throws Exception { ByteBufferPool pool = new ByteBufferPool(10, 3); ByteBufferListOutputStream bbos = new ByteBufferListOutputStream(pool); bbos.write(2);// Write a byte bbos.writeInt(100);// Write an int byte[] b = Bytes.toBytes("row123");// 6 bytes bbos.write(b); bbos.write(bb, 0, bb.capacity()); pool.putbackBuffer(bb1); bbos.writeInt(123); bbos.writeInt(124); assertEquals(0, pool.getQueueSize()); List<ByteBuffer> allBufs = bbos.getByteBuffers(); assertEquals(4, allBufs.size()); assertEquals(3, bbos.bufsFromPool.size()); assertEquals(4, b4.remaining()); assertEquals(124, b4.getInt()); bbos.releaseResources(); assertEquals(3, pool.getQueueSize());
@Test public void testWrites() throws Exception { ByteBufferPool pool = new ByteBufferPool(10, 3); ByteBufferListOutputStream bbos = new ByteBufferListOutputStream(pool); bbos.write(2);// Write a byte bbos.writeInt(100);// Write an int byte[] b = Bytes.toBytes("row123");// 6 bytes bbos.write(b); bbos.write(bb, 0, bb.capacity()); pool.putbackBuffer(bb1); bbos.writeInt(123); bbos.writeInt(124); assertEquals(0, pool.getQueueSize()); List<ByteBuffer> allBufs = bbos.getByteBuffers(); assertEquals(4, allBufs.size()); assertEquals(3, bbos.bufsFromPool.size()); assertEquals(4, b4.remaining()); assertEquals(124, b4.getInt()); bbos.releaseResources(); assertEquals(3, pool.getQueueSize());
ByteBufferListOutputStream bbos = new ByteBufferListOutputStream(pool); encodeCellsTo(bbos, cellScanner, codec, compressor); if (bbos.size() == 0) { bbos.releaseResources(); return null;
@Override protected void checkSizeAndGrow(int extra) { long capacityNeeded = curBuf.position() + (long) extra; if (capacityNeeded > curBuf.limit()) { allocateNewBuffer(); } }
/** * Release the resources it uses (The ByteBuffers) which are obtained from pool. Call this only * when all the data is fully used. And it must be called at the end of usage else we will leak * ByteBuffers from pool. */ public void releaseResources() { try { close(); } catch (IOException e) { LOG.debug(e.toString(), e); } // Return back all the BBs to pool if (this.bufsFromPool != null) { for (int i = 0; i < this.bufsFromPool.size(); i++) { this.pool.putbackBuffer(this.bufsFromPool.get(i)); } this.bufsFromPool = null; } this.allBufs = null; this.curBuf = null; }
ByteBufferListOutputStream bbos = new ByteBufferListOutputStream(pool); encodeCellsTo(bbos, cellScanner, codec, compressor); if (bbos.size() == 0) { bbos.releaseResources(); return null;
@Override public void write(byte[] b, int off, int len) throws IOException { int toWrite = 0; while (len > 0) { toWrite = Math.min(len, this.curBuf.remaining()); ByteBufferUtils.copyFromArrayToBuffer(this.curBuf, b, off, toWrite); off += toWrite; len -= toWrite; if (len > 0) { allocateNewBuffer();// The curBuf is over. Let us move to the next one } } }
/** * Release the resources it uses (The ByteBuffers) which are obtained from pool. Call this only * when all the data is fully used. And it must be called at the end of usage else we will leak * ByteBuffers from pool. */ public void releaseResources() { try { close(); } catch (IOException e) { LOG.debug(e.toString(), e); } // Return back all the BBs to pool if (this.bufsFromPool != null) { for (int i = 0; i < this.bufsFromPool.size(); i++) { this.pool.putbackBuffer(this.bufsFromPool.get(i)); } this.bufsFromPool = null; } this.allBufs = null; this.curBuf = null; }
@Override public void write(ByteBuffer b, int off, int len) throws IOException { int toWrite = 0; while (len > 0) { toWrite = Math.min(len, this.curBuf.remaining()); ByteBufferUtils.copyFromBufferToBuffer(b, this.curBuf, off, toWrite); off += toWrite; len -= toWrite; if (len > 0) { allocateNewBuffer();// The curBuf is over. Let us move to the next one } } } }
public ByteBufferListOutputStream(ByteBufferPool pool) { this.pool = pool; allocateNewBuffer(); }
public ByteBufferListOutputStream(ByteBufferPool pool) { this.pool = pool; allocateNewBuffer(); }
@Override protected void checkSizeAndGrow(int extra) { long capacityNeeded = curBuf.position() + (long) extra; if (capacityNeeded > curBuf.limit()) { allocateNewBuffer(); } }
@Override protected void checkSizeAndGrow(int extra) { long capacityNeeded = curBuf.position() + (long) extra; if (capacityNeeded > curBuf.limit()) { allocateNewBuffer(); } }
@Override public void write(byte[] b, int off, int len) throws IOException { int toWrite = 0; while (len > 0) { toWrite = Math.min(len, this.curBuf.remaining()); ByteBufferUtils.copyFromArrayToBuffer(this.curBuf, b, off, toWrite); off += toWrite; len -= toWrite; if (len > 0) { allocateNewBuffer();// The curBuf is over. Let us move to the next one } } }