本文整理匯總了Java中io.netty.buffer.ByteBuf.isDirect方法的典型用法代碼示例。如果您正苦於以下問題:Java ByteBuf.isDirect方法的具體用法?Java ByteBuf.isDirect怎麽用?Java ByteBuf.isDirect使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類io.netty.buffer.ByteBuf
的用法示例。
在下文中一共展示了ByteBuf.isDirect方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: readBytes
import io.netty.buffer.ByteBuf; //導入方法依賴的package包/類
@Override
public ByteBuffer readBytes(ByteBuf source) {
int len = readInt(source);
if (len < 0) {
return null;
}
ByteBuf slice = source.readSlice(len);
// if direct byte buffer, return underlying nioBuffer.
if (slice.isDirect()) {
return slice.nioBuffer();
}
// otherwise copy to a byte array and wrap it.
final byte[] out = new byte[slice.readableBytes()];
source.getBytes(source.readerIndex(), out, 0, len);
return ByteBuffer.wrap(out);
}
示例2: inflate
import io.netty.buffer.ByteBuf; //導入方法依賴的package包/類
/**
* Decompresses a buffer.
*
* @param buffer the buffer to decompress
* @return the decompressed buffer
* @throws DataFormatException if data could not be inflated
*/
public static ByteBuf inflate(ByteBuf buffer) throws DataFormatException {
// Ensure that this buffer is direct.
ByteBuf source = null;
ByteBuf decompressed = PooledByteBufAllocator.DEFAULT.directBuffer();
try {
if (!buffer.isDirect()) {
// We don't have a direct buffer. Create one.
ByteBuf temporary = PooledByteBufAllocator.DEFAULT.directBuffer();
temporary.writeBytes(buffer);
source = temporary;
} else {
source = buffer;
}
inflaterLocal.get().process(source, decompressed);
decompressed.resetReaderIndex();
return decompressed;
} catch (DataFormatException e) {
decompressed.release();
throw e;
} finally {
if (source != null && source != buffer) {
source.release();
}
}
}
示例3: deflate
import io.netty.buffer.ByteBuf; //導入方法依賴的package包/類
/**
* Compresses a {@link ByteBuf}.
*
* @param toCompress the buffer to compress
* @param into the buffer to compress into
* @throws DataFormatException if data could not be deflated
*/
public static void deflate(ByteBuf toCompress, ByteBuf into) throws DataFormatException {
ByteBuf destination = null;
ByteBuf source = null;
try {
if (!toCompress.isDirect()) {
// Source is not a direct buffer. Work on a temporary direct buffer and then write the contents out.
source = PooledByteBufAllocator.DEFAULT.directBuffer();
source.writeBytes(toCompress);
} else {
source = toCompress;
}
if (!into.isDirect()) {
// Destination is not a direct buffer. Work on a temporary direct buffer and then write the contents out.
destination = PooledByteBufAllocator.DEFAULT.directBuffer();
} else {
destination = into;
}
deflaterLocal.get().process(source, destination);
if (destination != into) {
into.writeBytes(destination);
}
} finally {
if (source != null && source != toCompress) {
source.release();
}
if (destination != null && destination != into) {
destination.release();
destination.resetReaderIndex();
}
}
}
示例4: encrypt
import io.netty.buffer.ByteBuf; //導入方法依賴的package包/類
@Override
public ByteBuf encrypt(ByteBuf data) {
if (encCipher == null) {
byte[] iv = EncryptUtils.randomBytes(getIvLength());
encCipher = createCipher(iv, true);
CompositeByteBuf bufs = new CompositeByteBuf(data.alloc(), data.isDirect(), 2);
bufs.addComponents(true, Unpooled.wrappedBuffer(iv), process(encCipher, data));
return bufs;
}
return process(encCipher, data);
}
示例5: isSingleDirectBuffer
import io.netty.buffer.ByteBuf; //導入方法依賴的package包/類
/**
* Checks if the specified buffer is a direct buffer and is composed of a single NIO buffer.
* (We check this because otherwise we need to make it a non-composite buffer.)
*/
private static boolean isSingleDirectBuffer(ByteBuf buf) {
return buf.isDirect() && buf.nioBufferCount() == 1;
}