org.jboss.netty.buffer.ChannelBufferOutputStream Java Examples

The following examples show how to use org.jboss.netty.buffer.ChannelBufferOutputStream. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ThriftFrameEncoder.java    From floodlight_with_topoguard with Apache License 2.0 6 votes vote down vote up
@Override
protected Object encode(ChannelHandlerContext ctx, Channel channel,
                        Object message) throws Exception {
    if (message instanceof SyncMessage) {
        ChannelBuffer buf = new DynamicChannelBuffer(512);
        ChannelBufferOutputStream os = new ChannelBufferOutputStream(buf);
        TCompactProtocol thriftProtocol =
                new TCompactProtocol(new TIOStreamTransport(os));
        ((SyncMessage) message).write(thriftProtocol);

        ChannelBuffer len = ChannelBuffers.buffer(4);
        len.writeInt(buf.readableBytes());
        return ChannelBuffers.wrappedBuffer(len, buf);
    }
    return message;
}
 
Example #2
Source File: MessageBatch.java    From jstorm with Apache License 2.0 6 votes vote down vote up
/**
 * create a buffer containing the encoding of this batch
 */
@Override
public ChannelBuffer buffer() throws Exception {
    ChannelBufferOutputStream bout = new ChannelBufferOutputStream(ChannelBuffers.directBuffer(encodedLength));

    for (Object msg : msgs)
        if (msg instanceof TaskMessage)
            writeTaskMessage(bout, (TaskMessage) msg);
        else {
            // LOG.debug("Write one non-TaskMessage {}", msg );
            ((ControlMessage) msg).write(bout);
        }

    // add a END_OF_BATCH indicator
    ControlMessage.EOB_MESSAGE.write(bout);
    // LOG.debug("ControlMessage.EOB_MESSAGE " );

    bout.close();

    return bout.buffer();
}
 
Example #3
Source File: MessageBatch.java    From jstorm with Apache License 2.0 6 votes vote down vote up
/**
 * write a TaskMessage into a stream
 *
 * Each TaskMessage is encoded as: task ... short(2) len ... int(4) payload ... byte[] *
 */
private void writeTaskMessage(ChannelBufferOutputStream bout, TaskMessage message) throws Exception {
    int payload_len = 0;
    if (message.message() != null)
        payload_len = message.message().length;

    short type = message.get_type();
    bout.writeShort(type);

    int task_id = message.task();
    if (task_id > Short.MAX_VALUE)
        throw new RuntimeException("Task ID should not exceed " + Short.MAX_VALUE);

    bout.writeShort((short) task_id);
    bout.writeInt(payload_len);
    if (payload_len > 0)
        bout.write(message.message());

    // LOG.info("Write one message taskid:{}, len:{}, data:{}", taskId
    // , payload_len, JStormUtils.toPrintableString(message.message()) );
}
 
Example #4
Source File: TaskMessage.java    From jstorm with Apache License 2.0 6 votes vote down vote up
/**
 * create a buffer containing the encoding of this batch
 */
@Override
public ChannelBuffer buffer() throws Exception {
    int payloadLen = 0;
    if (_message != null)
        payloadLen = _message.length;

    int totalLen = 8 + payloadLen;
    ChannelBufferOutputStream bout = new ChannelBufferOutputStream(ChannelBuffers.directBuffer(totalLen));
    bout.writeShort(_type);

    if (_task > Short.MAX_VALUE)
        throw new RuntimeException("Task ID should not exceed " + Short.MAX_VALUE);

    bout.writeShort((short) _task);
    bout.writeInt(payloadLen);
    if (payloadLen > 0)
        bout.write(_message);

    bout.close();
    return bout.buffer();
}
 
Example #5
Source File: MessagePackEncoder.java    From msgpack-rpc-java with Apache License 2.0 6 votes vote down vote up
@Override
protected Object encode(ChannelHandlerContext ctx, Channel channel,
        Object msg) throws Exception {
    if (msg instanceof ChannelBuffer) {
        return msg;
    }

    ChannelBufferOutputStream out = new ChannelBufferOutputStream(
            ChannelBuffers.dynamicBuffer(estimatedLength, ctx.getChannel()
                    .getConfig().getBufferFactory()));

    // MessagePack.pack(out, msg);
    messagePack.write(out, msg);

    ChannelBuffer result = out.buffer();
    return result;
}
 
Example #6
Source File: EmptyMapGenerator.java    From tangobot with Apache License 2.0 5 votes vote down vote up
@Override
public ChannelBuffer generateData() {
    ChannelBufferOutputStream output = new ChannelBufferOutputStream(MessageBuffers.dynamicBuffer());
    try {
        output.write(new byte[WIDTH * HEIGHT]);
    } catch (Exception e) {
        throw new RuntimeException("Empty map generator generateData error: " + e.getMessage());
    }
    return output.buffer();
}
 
Example #7
Source File: CompressedImagePublisher.java    From rosjava_android_template with Apache License 2.0 5 votes vote down vote up
public CompressedImagePublisher(ConnectedNode connectedNode) {
  this.connectedNode = connectedNode;
  NameResolver resolver = connectedNode.getResolver().newChild("camera");
  imagePublisher =
      connectedNode.newPublisher(resolver.resolve("image/compressed"),
          sensor_msgs.CompressedImage._TYPE);
  cameraInfoPublisher =
      connectedNode.newPublisher(resolver.resolve("camera_info"), sensor_msgs.CameraInfo._TYPE);
  stream = new ChannelBufferOutputStream(MessageBuffers.dynamicBuffer());
}
 
Example #8
Source File: NettyHandlerContainer.java    From recipes-rss with Apache License 2.0 5 votes vote down vote up
public OutputStream writeStatusAndHeaders(long contentLength, ContainerResponse cResponse) throws IOException {

			response = new DefaultHttpResponse(HttpVersion.HTTP_1_0, HttpResponseStatus.valueOf(cResponse.getStatus()));
			for (Map.Entry<String, List<Object>> e : cResponse.getHttpHeaders().entrySet()) {
				List<String> values = new ArrayList<String>();
				for (Object v : e.getValue())
					values.add(ContainerResponse.getHeaderValue(v));
				response.setHeader(e.getKey(), values);
			}
			ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
			response.setContent(buffer);
			return new ChannelBufferOutputStream(buffer);
		}
 
Example #9
Source File: NettyTcpClientTransport.java    From msgpack-rpc-java with Apache License 2.0 4 votes vote down vote up
@Override
protected ChannelBufferOutputStream newPendingBuffer() {
    return new ChannelBufferOutputStream(
            ChannelBuffers.dynamicBuffer(HeapChannelBufferFactory.getInstance()));
}
 
Example #10
Source File: NettyTcpClientTransport.java    From msgpack-rpc-java with Apache License 2.0 4 votes vote down vote up
@Override
protected void resetPendingBuffer(ChannelBufferOutputStream b) {
    b.buffer().clear();
}
 
Example #11
Source File: NettyTcpClientTransport.java    From msgpack-rpc-java with Apache License 2.0 4 votes vote down vote up
@Override
protected void flushPendingBuffer(ChannelBufferOutputStream b, Channel c) {
    Channels.write(c, b.buffer());
    b.buffer().clear();
}
 
Example #12
Source File: NettyTcpClientTransport.java    From msgpack-rpc-java with Apache License 2.0 4 votes vote down vote up
@Override
protected void closePendingBuffer(ChannelBufferOutputStream b) {
    b.buffer().clear();
}