private void munch() { buffer.flip(); while (buffer.remaining() >= chunkSize) { // we could limit the buffer to ensure process() does not read more than // chunkSize number of bytes, but we trust the implementations process(buffer); } buffer.compact(); // preserve any remaining data that do not make a full chunk } }
/** * Fills the byte output buffer from the input char buffer. * * @throws CharacterCodingException * an error encoding data */ private void fillBuffer() throws CharacterCodingException { this.bbuf.compact(); final CoderResult result = this.encoder.encode(this.cbuf, this.bbuf, true); if (result.isError()) { result.throwException(); } this.bbuf.flip(); }
/** * transfers appReadBuffer contents (decrypted data) into dst bytebuffer * @param dst ByteBuffer */ private int readFromAppBuffer(ByteBuffer dst) { appReadBuffer.flip(); int remaining = Math.min(appReadBuffer.remaining(), dst.remaining()); if (remaining > 0) { int limit = appReadBuffer.limit(); appReadBuffer.limit(appReadBuffer.position() + remaining); dst.put(appReadBuffer); appReadBuffer.limit(limit); } appReadBuffer.compact(); return remaining; }
@Override public DoubleBuffer compact () { if (byteBuffer.isReadOnly()) { throw new ReadOnlyBufferException(); } byteBuffer.limit(limit << 3); byteBuffer.position(position << 3); byteBuffer.compact(); byteBuffer.clear(); position = limit - position; limit = capacity; mark = UNSET_MARK; return this; }
/** * An efficient copy between two channels with a fixed-size buffer. * * @param src the source channel * @param dest the destination channel */ public static void fastCopy(final ReadableByteChannel src, final WritableByteChannel dest) throws IOException { // TODO(yupeng): make the buffer size configurable final ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024); while (src.read(buffer) != -1) { buffer.flip(); dest.write(buffer); buffer.compact(); } buffer.flip(); while (buffer.hasRemaining()) { dest.write(buffer); } }
do { int position = netReadBuffer.position(); netReadBuffer.flip(); result = sslEngine.unwrap(netReadBuffer, appReadBuffer); netReadBuffer.compact(); handshakeStatus = result.getHandshakeStatus(); if (result.getStatus() == SSLEngineResult.Status.OK && (ignoreHandshakeStatus && netReadBuffer.position() != position); log.trace("SSLHandshake handshakeUnwrap: handshakeStatus {} status {}", handshakeStatus, result.getStatus()); } while (netReadBuffer.position() != 0 && cont);
return written; netWriteBuffer.clear(); SSLEngineResult wrapResult = sslEngine.wrap(src, netWriteBuffer); netWriteBuffer.flip(); } else if (wrapResult.getStatus() == Status.BUFFER_OVERFLOW) { int currentNetWriteBufferSize = netWriteBufferSize(); netWriteBuffer.compact(); netWriteBuffer = Utils.ensureCapacity(netWriteBuffer, currentNetWriteBufferSize); netWriteBuffer.flip(); if (netWriteBuffer.limit() >= currentNetWriteBufferSize) throw new IllegalStateException("SSL BUFFER_OVERFLOW when available data size (" + netWriteBuffer.limit() + ") >= network buffer size (" + currentNetWriteBufferSize + ")"); } else if (wrapResult.getStatus() == Status.BUFFER_UNDERFLOW) { throw new IllegalStateException("SSL BUFFER_UNDERFLOW during write");
if(!inNetBuf.hasRemaining()) readFromNet(); inNetBuf.flip(); if (handshakeStatus == FINISHED && res.getStatus() == OK && inNetBuf.hasRemaining()) { res = unwrap0(); inNetBuf.compact(); inNetBuf.compact(); if(inNetBuf.capacity() == inNetBuf.limit()) inNetBuf = expandBuffer(inNetBuf, inNetBuf.capacity() * 2); inNetBuf.compact();
private boolean handleRead(ByteBuffer networkData) { try { applicationInputBuffer.clear(); SSLEngineResult result = sslEngine.unwrap(networkData, applicationInputBuffer); case OK: { if (result.bytesProduced() > 0) { applicationInputBuffer.flip(); upstreamFilter.onRead(applicationInputBuffer); applicationInputBuffer.compact();
private boolean writeToSocket () throws IOException { SocketChannel socketChannel = this.socketChannel; if (socketChannel == null) throw new SocketException("Connection is closed."); ByteBuffer buffer = writeBuffer; buffer.flip(); while (buffer.hasRemaining()) { if (bufferPositionFix) { buffer.compact(); buffer.flip(); } if (socketChannel.write(buffer) == 0) break; } buffer.compact(); return buffer.position() == 0; }
/** * Invoke inner SSL engine to unwrap. */ private SSLEngineResult engineUnwrap(final ByteBuffer buffer, final ByteBuffer unwrappedBuffer) throws IOException { assert Thread.holdsLock(getUnwrapLock()); if (!buffer.hasRemaining()) { buffer.compact(); sourceConduit.read(buffer); buffer.flip(); } log.logf(FQCN, Logger.Level.TRACE, null, "Unwrapping %s into %s", buffer, unwrappedBuffer); return engine.unwrap(buffer, unwrappedBuffer); }
private void munch() { buffer.flip(); while (buffer.remaining() >= chunkSize) { // we could limit the buffer to ensure process() does not read more than // chunkSize number of bytes, but we trust the implementations process(buffer); } buffer.compact(); // preserve any remaining data that do not make a full chunk } }
case OK: break; case BUFFER_OVERFLOW: netBuffer.compact(); netBuffer = Utils.ensureCapacity(netBuffer, sslEngine.getSession().getPacketBufferSize()); netBuffer.flip(); break; case BUFFER_UNDERFLOW: if (peerEngine.netBuffer.position() == 0) // no data to unwrap, return to process peer return; peerEngine.netBuffer.flip(); // unwrap the data from peer handshakeResult = sslEngine.unwrap(peerEngine.netBuffer, appBuffer); peerEngine.netBuffer.compact(); handshakeStatus = handshakeResult.getHandshakeStatus(); switch (handshakeResult.getStatus()) {
/** * You must invoke this when done reading from the unwrapped buffer */ default void doneReading(ByteBuffer unwrappedBuffer) { if (unwrappedBuffer.position() != 0) { unwrappedBuffer.compact(); } else { unwrappedBuffer.position(unwrappedBuffer.limit()); unwrappedBuffer.limit(unwrappedBuffer.capacity()); } }
void resize() { int increment = sslEngine.getSession().getPacketBufferSize(); int newSize = buffer.position() + increment; ByteBuffer newBuffer = ByteBuffer.allocate(newSize); buffer.flip(); newBuffer.flip(); buffer = Utils.appendBuffers(newBuffer, buffer, newBuffer.limit(), 50); buffer.compact(); }
@Override protected long transferContentTo(WritableByteChannel target) throws IOException { ReadableByteChannel channel = getChannel(); ByteBuffer buffer = getBuffer(); int transferred = 0; int read = channel.read(buffer); if (read > 0) { buffer.flip(); while (buffer.hasRemaining()) { transferred += target.write(buffer); } buffer.compact(); position += transferred; } if (position == getContentLength() || read < 0) { state = MultipartState.POST_CONTENT; if (channel.isOpen()) { channel.close(); } } return transferred; }