本文整理了Java中java.nio.ByteBuffer.compact()
方法的一些代码示例,展示了ByteBuffer.compact()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ByteBuffer.compact()
方法的具体详情如下:
包路径:java.nio.ByteBuffer
类名称:ByteBuffer
方法名:compact
[英]Compacts this byte buffer.
The remaining bytes will be moved to the head of the buffer, starting from position zero. Then the position is set to remaining(); the limit is set to capacity; the mark is cleared.
[中]压缩这个字节缓冲区。
剩余的字节将从位置0开始移动到缓冲区的头部。然后将位置设置为剩余();限制设置为容量;标记已清除。
代码示例来源:origin: google/guava
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
}
}
代码示例来源:origin: commons-io/commons-io
/**
* 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();
}
代码示例来源:origin: apache/kafka
/**
* 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;
}
代码示例来源:origin: libgdx/libgdx
@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;
}
代码示例来源:origin: Alluxio/alluxio
/**
* 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);
}
}
代码示例来源:origin: apache/kafka
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);
代码示例来源:origin: apache/kafka
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");
代码示例来源:origin: apache/ignite
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();
代码示例来源:origin: apache/geode
public void doneReading(ByteBuffer unwrappedBuffer) {
if (unwrappedBuffer.position() != 0) {
unwrappedBuffer.compact();
} else {
unwrappedBuffer.position(unwrappedBuffer.limit());
unwrappedBuffer.limit(unwrappedBuffer.capacity());
}
}
代码示例来源:origin: jersey/jersey
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();
代码示例来源:origin: EsotericSoftware/kryonet
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;
}
代码示例来源:origin: libgdx/libgdx
@Override
public IntBuffer compact () {
if (byteBuffer.isReadOnly()) {
throw new ReadOnlyBufferException();
}
byteBuffer.limit(limit << 2);
byteBuffer.position(position << 2);
byteBuffer.compact();
byteBuffer.clear();
position = limit - position;
limit = capacity;
mark = UNSET_MARK;
return this;
}
代码示例来源:origin: wildfly/wildfly
/**
* 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);
}
代码示例来源:origin: prestodb/presto
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
}
}
代码示例来源:origin: apache/kafka
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()) {
代码示例来源:origin: TooTallNate/Java-WebSocket
private synchronized ByteBuffer wrap( ByteBuffer b ) throws SSLException {
outCrypt.compact();
writeEngineResult = sslEngine.wrap( b, outCrypt );
outCrypt.flip();
return outCrypt;
}
代码示例来源:origin: apache/geode
/**
* 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());
}
}
代码示例来源:origin: jersey/jersey
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();
}
代码示例来源:origin: libgdx/libgdx
@Override
public CharBuffer compact () {
if (byteBuffer.isReadOnly()) {
throw new ReadOnlyBufferException();
}
byteBuffer.limit(limit << 1);
byteBuffer.position(position << 1);
byteBuffer.compact();
byteBuffer.clear();
position = limit - position;
limit = capacity;
mark = UNSET_MARK;
return this;
}
代码示例来源:origin: AsyncHttpClient/async-http-client
@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;
}
内容来源于网络,如有侵权,请联系作者删除!