本文整理汇总了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;
}