Search in sources :

Example 6 with ChannelBuffer

use of org.jboss.netty.buffer.ChannelBuffer in project storm by apache.

the class ThriftEncoder method encodeNettySerializable.

private HBMessage encodeNettySerializable(INettySerializable netty_message, HBServerMessageType mType) {
    HBMessageData message_data = new HBMessageData();
    HBMessage m = new HBMessage();
    try {
        ChannelBuffer cbuffer = netty_message.buffer();
        if (cbuffer.hasArray()) {
            message_data.set_message_blob(cbuffer.array());
        } else {
            byte[] buff = new byte[netty_message.encodeLength()];
            cbuffer.readBytes(buff, 0, netty_message.encodeLength());
            message_data.set_message_blob(buff);
        }
        m.set_type(mType);
        m.set_data(message_data);
        return m;
    } catch (IOException e) {
        LOG.error("Failed to encode NettySerializable: ", e);
        throw new RuntimeException(e);
    }
}
Also used : HBMessageData(org.apache.storm.generated.HBMessageData) IOException(java.io.IOException) HBMessage(org.apache.storm.generated.HBMessage) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 7 with ChannelBuffer

use of org.jboss.netty.buffer.ChannelBuffer in project crate by crate.

the class BasePGTypeTest method assertBytesWritten.

void assertBytesWritten(Object value, byte[] expectedBytes, int expectedLength) {
    ChannelBuffer writeBuffer = ChannelBuffers.dynamicBuffer();
    int bytesWritten = pgType.writeAsBinary(writeBuffer, value);
    assertThat(bytesWritten, is(expectedLength));
    byte[] bytes = new byte[expectedLength];
    writeBuffer.getBytes(0, bytes);
    assertThat(bytes, is(expectedBytes));
}
Also used : ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 8 with ChannelBuffer

use of org.jboss.netty.buffer.ChannelBuffer in project crate by crate.

the class PGTypesTest method writeAndReadBinary.

private Object writeAndReadBinary(Entry entry, PGType pgType) {
    ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
    pgType.writeAsBinary(buffer, entry.value);
    int length = buffer.readInt();
    return pgType.readBinaryValue(buffer, length);
}
Also used : ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 9 with ChannelBuffer

use of org.jboss.netty.buffer.ChannelBuffer in project crate by crate.

the class DigestBlob method addToHead.

public void addToHead(BytesReference content) throws IOException {
    if (content == null) {
        return;
    }
    int written = 0;
    ChannelBuffer channelBuffer = Netty3Utils.toChannelBuffer(content);
    int readableBytes = channelBuffer.readableBytes();
    assert readableBytes + headSize.get() <= headLength : "Got too many bytes in addToHead()";
    ByteBuffer byteBuffer = channelBuffer.toByteBuffer();
    while (written < readableBytes) {
        updateDigest(byteBuffer);
        written += headFileChannel.write(byteBuffer);
    }
    headSize.addAndGet(written);
    if (headSize.get() == headLength) {
        headCatchedUpLatch.countDown();
    }
}
Also used : ByteBuffer(java.nio.ByteBuffer) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 10 with ChannelBuffer

use of org.jboss.netty.buffer.ChannelBuffer in project sockjs-netty by cgbystrom.

the class HtmlFileTransport method writeRequested.

@Override
public void writeRequested(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
    if (e.getMessage() instanceof Frame) {
        final Frame frame = (Frame) e.getMessage();
        if (headerSent.compareAndSet(false, true)) {
            HttpResponse response = createResponse(CONTENT_TYPE_HTML);
            response.setHeader(CACHE_CONTROL, "no-store, no-cache, must-revalidate, max-age=0");
            // Safari needs at least 1024 bytes to parse the website. Relevant:
            //   http://code.google.com/p/browsersec/wiki/Part2#Survey_of_content_sniffing_behaviors
            int spaces = 1024 - header.readableBytes();
            ChannelBuffer paddedHeader = ChannelBuffers.buffer(1024 + 50);
            paddedHeader.writeBytes(header);
            for (int i = 0; i < spaces + 20; i++) {
                paddedHeader.writeByte(' ');
            }
            paddedHeader.writeByte('\r');
            paddedHeader.writeByte('\n');
            // Opera needs one more new line at the start.
            paddedHeader.writeByte('\r');
            paddedHeader.writeByte('\n');
            ctx.sendDownstream(new DownstreamMessageEvent(e.getChannel(), e.getFuture(), response, e.getRemoteAddress()));
            ctx.sendDownstream(new DownstreamMessageEvent(e.getChannel(), e.getFuture(), new DefaultHttpChunk(paddedHeader), e.getRemoteAddress()));
        }
        final ChannelBuffer frameContent = Frame.encode(frame, false);
        final ChannelBuffer content = ChannelBuffers.dynamicBuffer(frameContent.readableBytes() + 10);
        Frame.escapeJson(frameContent, content);
        ChannelBuffer wrappedContent = ChannelBuffers.wrappedBuffer(PREFIX, content, POSTFIX);
        ctx.sendDownstream(new DownstreamMessageEvent(e.getChannel(), e.getFuture(), new DefaultHttpChunk(wrappedContent), e.getRemoteAddress()));
        logResponseSize(e.getChannel(), content);
    } else {
        super.writeRequested(ctx, e);
    }
}
Also used : Frame(com.cgbystrom.sockjs.Frame) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Aggregations

ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)312 Test (org.junit.Test)63 DefaultHttpResponse (org.jboss.netty.handler.codec.http.DefaultHttpResponse)59 HttpResponse (org.jboss.netty.handler.codec.http.HttpResponse)49 Test (org.testng.annotations.Test)49 HttpChunk (org.jboss.netty.handler.codec.http.HttpChunk)46 DefaultHttpChunk (org.jboss.netty.handler.codec.http.DefaultHttpChunk)43 HttpChunkTrailer (org.jboss.netty.handler.codec.http.HttpChunkTrailer)37 DefaultHttpChunkTrailer (org.jboss.netty.handler.codec.http.DefaultHttpChunkTrailer)34 Checkpoint (com.linkedin.databus.core.Checkpoint)27 ByteBuffer (java.nio.ByteBuffer)27 BootstrapDatabaseTooOldException (com.linkedin.databus2.core.container.request.BootstrapDatabaseTooOldException)25 IOException (java.io.IOException)23 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)23 ArrayList (java.util.ArrayList)21 Channel (org.jboss.netty.channel.Channel)19 ChannelFuture (org.jboss.netty.channel.ChannelFuture)18 ChannelPipeline (org.jboss.netty.channel.ChannelPipeline)17 BlockLogBuffer (org.neo4j.com.BlockLogBuffer)16 Map (java.util.Map)14