Example usage for io.netty.buffer ByteBuf nioBuffer

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

Introduction

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

Prototype

public abstract ByteBuffer nioBuffer();

Source Link

Document

Exposes this buffer's readable bytes as an NIO ByteBuffer .

Usage

From source file:org.traccar.protocol.NavisProtocolDecoder.java

License:Apache License

private void sendNtcbReply(Channel channel, ByteBuf data) {
    if (channel != null) {
        ByteBuf header = Unpooled.buffer(16);
        header.writeCharSequence(prefix, StandardCharsets.US_ASCII);
        header.writeIntLE((int) deviceUniqueId);
        header.writeIntLE((int) serverId);
        header.writeShortLE(data.readableBytes());
        header.writeByte(Checksum.xor(data.nioBuffer()));
        header.writeByte(Checksum.xor(header.nioBuffer()));

        channel.writeAndFlush(/*w w w.j a  va2 s  . co m*/
                new NetworkMessage(Unpooled.wrappedBuffer(header, data), channel.remoteAddress()));
    }
}

From source file:org.traccar.protocol.NavisProtocolDecoder.java

License:Apache License

private void sendFlexReply(Channel channel, ByteBuf data) {
    if (channel != null) {
        ByteBuf cs = Unpooled.buffer(1);
        cs.writeByte(Checksum.crc8(new Algorithm(8, 0x31, 0xFF, false, false, 0x00), data.nioBuffer()));

        channel.writeAndFlush(new NetworkMessage(Unpooled.wrappedBuffer(data, cs), channel.remoteAddress()));
    }//from w w  w.j  a  v  a2  s.  c o  m
}

From source file:org.wso2.ballerinalang.compiler.bir.writer.BIRBinaryWriter.java

License:Open Source License

public byte[] write(BIRNode.BIRPackage birPackage) {
    ByteBuf birbuf = Unpooled.buffer();

    // Write the package details in the form of constant pool entry
    int orgCPIndex = addStringCPEntry(birPackage.org.value);
    int nameCPIndex = addStringCPEntry(birPackage.name.value);
    int versionCPIndex = addStringCPEntry(birPackage.version.value);
    int pkgIndex = cp.addCPEntry(new PackageCPEntry(orgCPIndex, nameCPIndex, versionCPIndex));
    birbuf.writeInt(pkgIndex);/* ww w . ja va  2 s . c  o  m*/

    // Write functions
    writeFunctions(birbuf, birPackage.functions);

    // Write the constant pool entries.
    // TODO Only one constant pool is available for now. This will change in future releases
    // TODO e.g., strtab, shstrtab, rodata.
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try (DataOutputStream dataOut = new DataOutputStream(baos)) {
        dataOut.write(BIR_MAGIC);
        dataOut.writeInt(BIR_VERSION);
        writeCP(dataOut);
        dataOut.write(birbuf.nioBuffer().array(), 0, birbuf.nioBuffer().limit());
        return baos.toByteArray();
    } catch (IOException e) {
        throw new BLangCompilerException("failed to serialize the bir", e);
    }
}

From source file:org.wso2.carbon.transport.http.netty.listener.HTTPTraceLoggingHandler.java

License:Open Source License

private static void appendPayload(StringBuilder stringBuilder, ByteBuf content)
        throws CharacterCodingException {
    CharsetDecoder decoder = Charset.forName("UTF8").newDecoder();
    CharBuffer buffer = decoder.decode(content.nioBuffer());
    stringBuilder.append(buffer);/*from  ww w  . j a va  2s. com*/
}

From source file:org.wso2.carbon.transport.http.netty.listener.WebSocketSourceHandler.java

License:Open Source License

private void notifyBinaryMessage(BinaryWebSocketFrame binaryWebSocketFrame) throws ServerConnectorException {
    ByteBuf byteBuf = binaryWebSocketFrame.content();
    boolean finalFragment = binaryWebSocketFrame.isFinalFragment();
    ByteBuffer byteBuffer = byteBuf.nioBuffer();
    WebSocketMessageImpl webSocketBinaryMessage = new WebSocketBinaryMessageImpl(byteBuffer, finalFragment);
    webSocketBinaryMessage = setupCommonProperties(webSocketBinaryMessage);
    connectorFuture.notifyWSListener((WebSocketBinaryMessage) webSocketBinaryMessage);
}

From source file:org.wso2.carbon.transport.http.netty.listener.WebSocketSourceHandler.java

License:Open Source License

private void notifyPingMessage(PingWebSocketFrame pingWebSocketFrame) throws ServerConnectorException {
    //Control message for WebSocket is Ping Message
    ByteBuf byteBuf = pingWebSocketFrame.content();
    ByteBuffer byteBuffer = byteBuf.nioBuffer();
    WebSocketMessageImpl webSocketControlMessage = new WebSocketControlMessageImpl(WebSocketControlSignal.PING,
            byteBuffer);//from   www  . j a  v a2s  .co m
    webSocketControlMessage = setupCommonProperties(webSocketControlMessage);
    connectorFuture.notifyWSListener((WebSocketControlMessage) webSocketControlMessage);
}

From source file:org.wso2.carbon.transport.http.netty.listener.WebSocketSourceHandler.java

License:Open Source License

private void notifyPongMessage(PongWebSocketFrame pongWebSocketFrame) throws ServerConnectorException {
    //Control message for WebSocket is Pong Message
    ByteBuf byteBuf = pongWebSocketFrame.content();
    ByteBuffer byteBuffer = byteBuf.nioBuffer();
    WebSocketMessageImpl webSocketControlMessage = new WebSocketControlMessageImpl(WebSocketControlSignal.PONG,
            byteBuffer);// ww  w  .jav a2s.c om
    webSocketControlMessage = setupCommonProperties(webSocketControlMessage);
    connectorFuture.notifyWSListener((WebSocketControlMessage) webSocketControlMessage);
}

From source file:org.wso2.carbon.transport.http.netty.message.BlockingEntityCollector.java

License:Open Source License

@Deprecated
public List<ByteBuffer> getFullMessageBody() {
    List<ByteBuffer> byteBufferList = new ArrayList<>();

    if (!isConsumed.get()) {
        boolean isEndOfMessageProcessed = false;
        while (!isEndOfMessageProcessed) {
            try {
                HttpContent httpContent = httpContentQueue.poll(soTimeOut, TimeUnit.SECONDS);
                // This check is to make sure we add the last http content after getClone and avoid adding
                // empty content to bytebuf list again and again
                if (httpContent instanceof EmptyLastHttpContent) {
                    break;
                }/*  ww  w. j  a v a  2  s  . com*/

                if (httpContent instanceof LastHttpContent) {
                    isEndOfMessageProcessed = true;
                    isConsumed.set(true);
                    httpContentQueue.clear();
                }
                ByteBuf buf = httpContent.content();
                byteBufferList.add(buf.nioBuffer());
            } catch (InterruptedException e) {
                LOG.error("Error while getting full message body", e);
            }
        }
    }

    return byteBufferList;
}

From source file:org.wso2.carbon.transport.http.netty.sender.websocket.WebSocketTargetHandler.java

License:Open Source License

private void notifyBinaryMessage(BinaryWebSocketFrame binaryWebSocketFrame, ChannelHandlerContext ctx)
        throws ServerConnectorException {
    ByteBuf byteBuf = binaryWebSocketFrame.content();
    boolean finalFragment = binaryWebSocketFrame.isFinalFragment();
    ByteBuffer byteBuffer = byteBuf.nioBuffer();
    WebSocketMessageImpl webSocketBinaryMessage = new WebSocketBinaryMessageImpl(byteBuffer, finalFragment);
    webSocketBinaryMessage = setupCommonProperties(webSocketBinaryMessage, ctx);
    connectorListener.onMessage((WebSocketBinaryMessage) webSocketBinaryMessage);
}

From source file:org.wso2.carbon.transport.http.netty.sender.websocket.WebSocketTargetHandler.java

License:Open Source License

private void notifyPingMessage(PingWebSocketFrame pingWebSocketFrame, ChannelHandlerContext ctx)
        throws ServerConnectorException {
    //Control message for WebSocket is Ping Message
    ByteBuf byteBuf = pingWebSocketFrame.content();
    ByteBuffer byteBuffer = byteBuf.nioBuffer();
    WebSocketMessageImpl webSocketControlMessage = new WebSocketControlMessageImpl(WebSocketControlSignal.PING,
            byteBuffer);//from   ww  w. j  av a2 s. c om
    webSocketControlMessage = setupCommonProperties(webSocketControlMessage, ctx);
    connectorListener.onMessage((WebSocketControlMessage) webSocketControlMessage);
}