Example usage for io.netty.buffer ByteBuf isDirect

List of usage examples for io.netty.buffer ByteBuf isDirect

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf isDirect.

Prototype

public abstract boolean isDirect();

Source Link

Document

Returns true if and only if this buffer is backed by an NIO direct buffer.

Usage

From source file:com.datastax.driver.core.LZ4Compressor.java

License:Apache License

@Override
Frame compress(Frame frame) throws IOException {
    ByteBuf input = frame.body;

    // TODO: JAVA-1306: Use the same API calls for direct and heap buffers when LZ4 updated.
    ByteBuf frameBody = input.isDirect() ? compressDirect(input) : compressHeap(input);
    return frame.with(frameBody);
}

From source file:com.datastax.driver.core.LZ4Compressor.java

License:Apache License

@Override
Frame decompress(Frame frame) throws IOException {
    ByteBuf input = frame.body;

    // TODO: JAVA-1306: Use the same API calls for direct and heap buffers when LZ4 updated.
    ByteBuf frameBody = input.isDirect() ? decompressDirect(input) : decompressHeap(input);
    return frame.with(frameBody);
}

From source file:com.datastax.driver.core.SnappyCompressor.java

License:Apache License

@Override
Frame compress(Frame frame) throws IOException {
    ByteBuf input = frame.body;
    ByteBuf frameBody = input.isDirect() ? compressDirect(input) : compressHeap(input);
    return frame.with(frameBody);
}

From source file:com.datastax.driver.core.SnappyCompressor.java

License:Apache License

@Override
Frame decompress(Frame frame) throws IOException {
    ByteBuf input = frame.body;
    ByteBuf frameBody = input.isDirect() ? decompressDirect(input) : decompressHeap(input);
    return frame.with(frameBody);
}

From source file:io.advantageous.conekt.datagram.impl.DatagramServerHandler.java

License:Open Source License

@Override
protected Object safeObject(Object msg, ByteBufAllocator allocator) throws Exception {
    if (msg instanceof DatagramPacket) {
        DatagramPacket packet = (DatagramPacket) msg;
        ByteBuf content = packet.content();
        if (content.isDirect()) {
            content = safeBuffer(content, allocator);
        }// w w  w  .jav  a 2s  . c om
        return new DatagramPacketImpl(packet.sender(), Buffer.buffer(content));
    }
    return msg;
}

From source file:io.advantageous.conekt.http.impl.ConektHttpHandler.java

License:Open Source License

@Override
protected Object safeObject(Object msg, ByteBufAllocator allocator) throws Exception {
    if (msg instanceof HttpContent) {
        HttpContent content = (HttpContent) msg;
        ByteBuf buf = content.content();
        if (buf != Unpooled.EMPTY_BUFFER && buf.isDirect()) {
            ByteBuf newBuf = safeBuffer(content, allocator);
            if (msg instanceof LastHttpContent) {
                LastHttpContent last = (LastHttpContent) msg;
                return new AssembledLastHttpContent(newBuf, last.trailingHeaders(), last.getDecoderResult());
            } else {
                return new DefaultHttpContent(newBuf);
            }/*from ww  w  .j a v  a 2 s.c  o m*/
        }
    } else if (msg instanceof WebSocketFrame) {
        ByteBuf payload = safeBuffer((WebSocketFrame) msg, allocator);
        boolean isFinal = ((WebSocketFrame) msg).isFinalFragment();
        FrameType frameType;
        if (msg instanceof BinaryWebSocketFrame) {
            frameType = FrameType.BINARY;
        } else if (msg instanceof CloseWebSocketFrame) {
            frameType = FrameType.CLOSE;
        } else if (msg instanceof PingWebSocketFrame) {
            frameType = FrameType.PING;
        } else if (msg instanceof PongWebSocketFrame) {
            frameType = FrameType.PONG;
        } else if (msg instanceof TextWebSocketFrame) {
            frameType = FrameType.TEXT;
        } else if (msg instanceof ContinuationWebSocketFrame) {
            frameType = FrameType.CONTINUATION;
        } else {
            throw new IllegalStateException("Unsupported websocket msg " + msg);
        }
        return new WebSocketFrameImpl(frameType, payload, isFinal);
    }
    return msg;
}

From source file:io.advantageous.conekt.net.impl.ConektHandler.java

License:Open Source License

protected static ByteBuf safeBuffer(ByteBuf buf, ByteBufAllocator allocator) {
    if (buf == Unpooled.EMPTY_BUFFER) {
        return buf;
    }//from w w w  .ja va 2s  .  com
    if (buf.isDirect() || buf instanceof CompositeByteBuf) {
        try {
            if (buf.isReadable()) {
                ByteBuf buffer = allocator.heapBuffer(buf.readableBytes());
                buffer.writeBytes(buf);
                return buffer;
            } else {
                return Unpooled.EMPTY_BUFFER;
            }
        } finally {
            buf.release();
        }
    }
    return buf;
}

From source file:io.atomix.cluster.messaging.impl.MessageDecoder.java

License:Apache License

static String readString(ByteBuf buffer, int length, Charset charset) {
    if (buffer.isDirect()) {
        final String result = buffer.toString(buffer.readerIndex(), length, charset);
        buffer.skipBytes(length);//from   w  w w  . j  a  v a2  s. c om
        return result;
    } else if (buffer.hasArray()) {
        final String result = new String(buffer.array(), buffer.arrayOffset() + buffer.readerIndex(), length,
                charset);
        buffer.skipBytes(length);
        return result;
    } else {
        final byte[] array = new byte[length];
        buffer.readBytes(array);
        return new String(array, charset);
    }
}

From source file:io.gatling.netty.util.ahc.Utf8ByteBufCharsetDecoder.java

License:Apache License

public String decode(ByteBuf buf) {
    if (buf.isDirect()) {
        return ByteBufUtils.decodeString(UTF_8, buf);
    }//from   w w w.  j  av a2  s  .  c om
    decodeHeap0(buf);
    return charBuffer.toString();
}

From source file:io.gatling.netty.util.ahc.Utf8ByteBufCharsetDecoder.java

License:Apache License

public char[] decodeChars(ByteBuf buf) {
    if (buf.isDirect()) {
        return ByteBufUtils.decodeChars(UTF_8, buf);
    }//from  w w  w .  j av a2  s.c  o  m
    decodeHeap0(buf);
    return toCharArray(charBuffer);
}