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);
}
}
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));
}
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);
}
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();
}
}
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);
}
}
Aggregations