List of usage examples for io.netty.buffer Unpooled buffer
public static ByteBuf buffer()
From source file:com.soho.framework.server.netty.http.HttpServletHandler.java
License:Apache License
private void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) { String text = "Failure: " + status.toString() + "\r\n"; ByteBuf byteBuf = Unpooled.buffer(); byte[] bytes = null; try {/*from w w w. j a v a 2 s.c om*/ bytes = text.getBytes("utf-8"); byteBuf.writeBytes(bytes); } catch (UnsupportedEncodingException e) { } FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, byteBuf); HttpHeaders headers = response.headers(); headers.add(CONTENT_TYPE, "text/plain;charset=utf-8"); headers.add(CACHE_CONTROL, "no-cache"); headers.add(PRAGMA, "No-cache"); headers.add(SERVER, ServerConfig.getServerName()); headers.add(CONTENT_LENGTH, byteBuf.readableBytes()); ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); }
From source file:com.spotify.ffwd.riemann.RiemannResponder.java
License:Apache License
private ByteBuf ack(boolean ok) throws IOException { final Proto.Msg m = Proto.Msg.newBuilder().setOk(ok).build(); final ByteBuf b = Unpooled.buffer(); try (final ByteBufOutputStream output = new ByteBufOutputStream(b)) { m.writeTo(output);/* w w w . jav a 2 s. c o m*/ final ByteBuf frame = output.buffer(); final ByteBuf buffer = Unpooled.buffer(4 + frame.readableBytes()); buffer.writeInt(frame.readableBytes()); buffer.writeBytes(frame); return buffer; } finally { b.release(); } }
From source file:com.spotify.ffwd.riemann.RiemannSerialization.java
License:Apache License
public ByteBuf encodeAll0(Collection<Object> messages) throws IOException { final Proto.Msg.Builder builder = Proto.Msg.newBuilder(); for (final Object d : messages) { if (d instanceof Metric) { builder.addEvents(encodeMetric0((Metric) d)); } else if (d instanceof Event) { builder.addEvents(encodeEvent0((Event) d)); }// w ww . java 2 s .c o m } final Proto.Msg m = builder.build(); final ByteBuf work = Unpooled.buffer(); try (final ByteBufOutputStream output = new ByteBufOutputStream(work)) { m.writeTo(output); final ByteBuf result = Unpooled.buffer(); result.writeInt(work.writerIndex()); result.writeBytes(work); return result; } finally { work.release(); } }
From source file:com.spotify.netty.handler.codec.zmtp.ZMTPUtilsTests.java
License:Apache License
@Test public void frameSizeTest() { for (boolean more : asList(TRUE, FALSE)) { for (int size = 0; size < 1024; size++) { final ZMTPFrame frame = ZMTPFrame.create(new byte[size]); int estimatedSize = ZMTPUtils.frameSize(frame, 1); final ByteBuf buffer = Unpooled.buffer(); ZMTPUtils.writeFrame(frame, buffer, more, 1); int writtenSize = buffer.readableBytes(); assertEquals(writtenSize, estimatedSize); }// www.j a va 2s . c om } }
From source file:com.spotify.netty.handler.codec.zmtp.ZMTPUtilsTests.java
License:Apache License
@Test public void messageSizeTest() { final List<ZMTPFrame> EMPTY = new ArrayList<ZMTPFrame>(); final List<ZMTPFrame> manyFrameSizes = new ArrayList<ZMTPFrame>(); for (int i = 0; i < 1024; i++) { manyFrameSizes.add(ZMTPFrame.create(new byte[i])); }/*from ww w. ja v a 2s . co m*/ @SuppressWarnings("unchecked") final List<List<ZMTPFrame>> frameSets = asList(EMPTY, asList(ZMTPFrame.create("foo")), asList(ZMTPFrame.create("foo"), ZMTPFrame.create("bar")), manyFrameSizes); for (boolean enveloped : asList(TRUE, FALSE)) { for (List<ZMTPFrame> envelope : frameSets) { for (List<ZMTPFrame> payload : frameSets) { if (payload.isEmpty()) { continue; } final ZMTPMessage message = new ZMTPMessage(envelope, payload); int estimatedSize = ZMTPUtils.messageSize(message, enveloped, 1); final ByteBuf buffer = Unpooled.buffer(); ZMTPUtils.writeMessage(message, buffer, enveloped, 1); int writtenSize = buffer.readableBytes(); assertEquals(writtenSize, estimatedSize); } } } }
From source file:com.spotify.netty4.handler.codec.zmtp.ZMTPMessageTest.java
License:Apache License
@Test public void testWriteAndReadTwoMessages() throws ZMTPParsingException { final ZMTPMessage m1 = ZMTPMessage.fromUTF8(ALLOC, "hello", "world"); final ZMTPMessage m2 = ZMTPMessage.fromUTF8(ALLOC, "foo", "bar"); final ByteBuf buffer = Unpooled.buffer(); m1.write(buffer, version);/*from w ww .j a v a 2 s. c o m*/ m2.write(buffer, version); final ZMTPMessage r1 = ZMTPMessage.read(buffer, version); final ZMTPMessage r2 = ZMTPMessage.read(buffer, version); assertThat(r1, is(m1)); assertThat(r2, is(m2)); }
From source file:com.spotify.netty4.handler.codec.zmtp.ZMTPWriterTest.java
License:Apache License
@Test public void testOneFrame() throws Exception { final ZMTPWriter writer = ZMTPWriter.create(ZMTP10); final ByteBuf buf = Unpooled.buffer(); writer.reset(buf);//from ww w . j a v a 2s . c om ByteBuf frame = writer.frame(11, false); assertThat(frame, is(sameInstance(buf))); final ByteBuf content = copiedBuffer("hello world", UTF_8); frame.writeBytes(content.duplicate()); final ZMTPFramingDecoder decoder = new ZMTPFramingDecoder(wireFormat(ZMTP10), new RawDecoder()); decoder.decode(null, buf, out); assertThat(out, hasSize(1)); assertThat(out, contains((Object) singletonList(content))); }
From source file:com.spotify.netty4.handler.codec.zmtp.ZMTPWriterTest.java
License:Apache License
@Test public void testTwoFrames() throws Exception { final ZMTPWriter writer = ZMTPWriter.create(ZMTP10); final ByteBuf buf = Unpooled.buffer(); writer.reset(buf);/* ww w. j a v a2 s .c o m*/ final ByteBuf f0 = copiedBuffer("hello ", UTF_8); final ByteBuf f1 = copiedBuffer("hello ", UTF_8); writer.frame(f0.readableBytes(), true).writeBytes(f0.duplicate()); writer.frame(f1.readableBytes(), false).writeBytes(f1.duplicate()); final ZMTPFramingDecoder decoder = new ZMTPFramingDecoder(wireFormat(ZMTP10), new RawDecoder()); decoder.decode(null, buf, out); assertThat(out, hasSize(1)); assertThat(out, contains((Object) asList(f0, f1))); }
From source file:com.spotify.netty4.handler.codec.zmtp.ZMTPWriterTest.java
License:Apache License
@Test public void testReframe() throws Exception { final ZMTPFramingDecoder decoder = new ZMTPFramingDecoder(wireFormat(ZMTP10), new RawDecoder()); final ZMTPWriter writer = ZMTPWriter.create(ZMTP10); final ByteBuf buf = Unpooled.buffer(); writer.reset(buf);//from www. j a v a 2 s . c o m // Request a frame with margin in anticipation of a larger payload... // ... but write a smaller payload final ByteBuf content = copiedBuffer("hello world", UTF_8); writer.frame(content.readableBytes() * 2, true).writeBytes(content.duplicate()); // And rewrite the frame accordingly writer.reframe(content.readableBytes(), false); // Verify that the message can be parsed decoder.decode(null, buf, out); assertThat(out, hasSize(1)); assertThat(out, contains((Object) singletonList(content))); // Write and verify another message final ByteBuf next = copiedBuffer("next", UTF_8); writer.frame(next.readableBytes(), false).writeBytes(next.duplicate()); out.clear(); decoder.decode(null, buf, out); assertThat(out, hasSize(1)); assertThat(out, contains((Object) singletonList(next))); }
From source file:com.streamsets.pipeline.lib.parser.net.netflow.NetflowTestUtil.java
License:Apache License
public static void writeV5NetflowHeader(EmbeddedChannel channel, int count, long uptime, long seconds, long nanos, long flowSequence, int engineType, int engineId, int sampling) { final int version = 5; final ByteBuf buf = Unpooled.buffer(); buf.writeShort(version);/*from w w w . j av a2 s. c o m*/ buf.writeShort(count); buf.writeInt((int) uptime); buf.writeInt((int) seconds); buf.writeInt((int) nanos); buf.writeInt((int) flowSequence); buf.writeByte(engineType); buf.writeByte(engineId); buf.writeShort(sampling); channel.writeInbound(buf); }