void release() { synchronized (readLock) { if (allocatedBuffer != null) { allocatedBuffer.release(); } } }
@Override public AllocatedBuffer allocateDirectBuffer(int capacity) { return AllocatedBuffer.wrap(ByteBuffer.allocateDirect(capacity)); } };
private int readEncryptedDataHeap(ByteBuffer dst, int len) throws IOException { AllocatedBuffer allocatedBuffer = null; try { final ByteBuffer buffer; if (bufferAllocator != null) { allocatedBuffer = bufferAllocator.allocateDirectBuffer(len); buffer = allocatedBuffer.nioBuffer(); } else { // We don't have a buffer allocator, but we don't want to send a heap // buffer to JNI. So lazy-create a direct buffer that we will use from now // on to copy encrypted packets. buffer = getOrCreateLazyDirectBuffer(); } int bytesToRead = min(len, buffer.remaining()); int bytesRead = readEncryptedDataDirect(buffer, 0, bytesToRead); if (bytesRead > 0) { buffer.position(bytesRead); buffer.flip(); dst.put(buffer); } return bytesRead; } finally { if (allocatedBuffer != null) { // Release the buffer back to the pool. allocatedBuffer.release(); } } }
SSLInputStream() { if (bufferAllocator != null) { allocatedBuffer = bufferAllocator.allocateDirectBuffer( engine.getSession().getApplicationBufferSize()); fromEngine = allocatedBuffer.nioBuffer(); } else { allocatedBuffer = null; fromEngine = ByteBuffer.allocateDirect(engine.getSession().getApplicationBufferSize()); } // Initially fromEngine.remaining() == 0. fromEngine.flip(); fromSocket = ByteBuffer.allocate(engine.getSession().getPacketBufferSize()); fromSocketArrayOffset = fromSocket.arrayOffset(); }
private int readEncryptedDataHeap(ByteBuffer dst, int len) throws IOException { AllocatedBuffer allocatedBuffer = null; try { final ByteBuffer buffer; if (bufferAllocator != null) { allocatedBuffer = bufferAllocator.allocateDirectBuffer(len); buffer = allocatedBuffer.nioBuffer(); } else { // We don't have a buffer allocator, but we don't want to send a heap // buffer to JNI. So lazy-create a direct buffer that we will use from now // on to copy encrypted packets. buffer = getOrCreateLazyDirectBuffer(); } int bytesToRead = min(len, buffer.remaining()); int bytesRead = readEncryptedDataDirect(buffer, 0, bytesToRead); if (bytesRead > 0) { buffer.position(bytesRead); buffer.flip(); dst.put(buffer); } return bytesRead; } finally { if (allocatedBuffer != null) { // Release the buffer back to the pool. allocatedBuffer.release(); } } }
SSLInputStream() { if (bufferAllocator != null) { allocatedBuffer = bufferAllocator.allocateDirectBuffer( engine.getSession().getApplicationBufferSize()); fromEngine = allocatedBuffer.nioBuffer(); } else { allocatedBuffer = null; fromEngine = ByteBuffer.allocateDirect(engine.getSession().getApplicationBufferSize()); } // Initially fromEngine.remaining() == 0. fromEngine.flip(); fromSocket = ByteBuffer.allocate(engine.getSession().getPacketBufferSize()); fromSocketArrayOffset = fromSocket.arrayOffset(); }
private int writePlaintextDataHeap(ByteBuffer src, int pos, int len) throws IOException { AllocatedBuffer allocatedBuffer = null; try { final ByteBuffer buffer; if (bufferAllocator != null) { allocatedBuffer = bufferAllocator.allocateDirectBuffer(len); buffer = allocatedBuffer.nioBuffer(); } else { // We don't have a buffer allocator, but we don't want to send a heap // buffer to JNI. So lazy-create a direct buffer that we will use from now // on to copy plaintext data. buffer = getOrCreateLazyDirectBuffer(); } // Copy the data to the direct buffer. int limit = src.limit(); int bytesToWrite = min(len, buffer.remaining()); src.limit(pos + bytesToWrite); buffer.put(src); buffer.flip(); // Restore the original position and limit. src.limit(limit); src.position(pos); return writePlaintextDataDirect(buffer, 0, bytesToWrite); } finally { if (allocatedBuffer != null) { // Release the buffer back to the pool. allocatedBuffer.release(); } } }
@Override public AllocatedBuffer allocateDirectBuffer(int capacity) { return AllocatedBuffer.wrap(ByteBuffer.allocateDirect(capacity)); } };
void release() { synchronized (readLock) { if (allocatedBuffer != null) { allocatedBuffer.release(); } } }
private int writePlaintextDataHeap(ByteBuffer src, int pos, int len) throws IOException { AllocatedBuffer allocatedBuffer = null; try { final ByteBuffer buffer; if (bufferAllocator != null) { allocatedBuffer = bufferAllocator.allocateDirectBuffer(len); buffer = allocatedBuffer.nioBuffer(); } else { // We don't have a buffer allocator, but we don't want to send a heap // buffer to JNI. So lazy-create a direct buffer that we will use from now // on to copy plaintext data. buffer = getOrCreateLazyDirectBuffer(); } // Copy the data to the direct buffer. int limit = src.limit(); int bytesToWrite = min(len, buffer.remaining()); src.limit(pos + bytesToWrite); buffer.put(src); buffer.flip(); // Restore the original position and limit. src.limit(limit); src.position(pos); return writePlaintextDataDirect(buffer, 0, bytesToWrite); } finally { if (allocatedBuffer != null) { // Release the buffer back to the pool. allocatedBuffer.release(); } } }