Example usage for io.netty.buffer ByteBufOutputStream ByteBufOutputStream

List of usage examples for io.netty.buffer ByteBufOutputStream ByteBufOutputStream

Introduction

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

Prototype

public ByteBufOutputStream(ByteBuf buffer) 

Source Link

Document

Creates a new stream which writes data to the specified buffer .

Usage

From source file:ws.wamp.jawampa.transport.netty.WampSerializationHandler.java

License:Apache License

@Override
protected void encode(ChannelHandlerContext ctx, WampMessage msg, List<Object> out) throws Exception {
    ByteBuf msgBuffer = Unpooled.buffer();
    ByteBufOutputStream outStream = new ByteBufOutputStream(msgBuffer);
    ObjectMapper objectMapper = serialization.getObjectMapper();
    try {//from w w  w .j a  v  a  2s  . c o  m
        JsonNode node = msg.toObjectArray(objectMapper);
        objectMapper.writeValue(outStream, node);

        if (logger.isDebugEnabled()) {
            logger.debug("Serialized Wamp Message: {}", node.toString());
        }

    } catch (Exception e) {
        msgBuffer.release();
        return;
    }

    if (serialization.isText()) {
        TextWebSocketFrame frame = new TextWebSocketFrame(msgBuffer);
        out.add(frame);
    } else {
        BinaryWebSocketFrame frame = new BinaryWebSocketFrame(msgBuffer);
        out.add(frame);
    }
}

From source file:ws.wamp.jawampa.transport.WampSerializationHandler.java

License:Apache License

@Override
protected void encode(ChannelHandlerContext ctx, WampMessage msg, List<Object> out) throws Exception {
    if (serialization == Serialization.Json) {
        ByteBuf msgBuffer = Unpooled.buffer();
        ByteBufOutputStream outStream = new ByteBufOutputStream(msgBuffer);
        try {/*from   www. j  a v  a  2s  .  c  o m*/
            objectMapper.writeValue(outStream, msg.toObjectArray(objectMapper)); // TODO: Non JSON Mapping
        } catch (Exception e) {
            msgBuffer.release();
            return;
        }

        TextWebSocketFrame frame = new TextWebSocketFrame(msgBuffer);
        if (logger.isDebugEnabled()) {
            logger.debug("Serialized Wamp Message: {}", frame.text());
        }
        //System.out.println("Serialized Wamp message: " + frame.text());
        out.add(frame);
    }
}