Java Code Examples for io.netty.buffer.ByteBuf.isDirect()

The following are Jave code examples for showing how to use isDirect() of the io.netty.buffer.ByteBuf class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: simulacron   File: ByteBufCodec.java   Source Code and License Vote up 6 votes
@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);
}
 
Example 2
Project: BedrockProxy   File: Compression.java   Source Code and License Vote up 5 votes
/**
 * 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();
        }
    }
}
 
Example 3
Project: BedrockProxy   File: Compression.java   Source Code and License Vote up 5 votes
/**
 * 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();
        }
    }
}
 
Example 4
Project: ss-java   File: AbstractCrypto.java   Source Code and License Vote up 5 votes
@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);
}
 
Example 5
Project: kcp-netty   File: UkcpClientUdpChannel.java   Source Code and License Vote up 2 votes
/**
 * 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;
}
 
Example 6
Project: kcp-netty   File: UkcpServerChannel.java   Source Code and License Vote up 2 votes
/**
 * 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;
}