List of usage examples for io.netty.buffer Unpooled copiedBuffer
public static ByteBuf copiedBuffer(ByteBuffer... buffers)
From source file:com.corundumstudio.socketio.protocol.PacketDecoder.java
License:Apache License
private Packet parseBinary(ClientHead head, ByteBuf frame) throws IOException { if (frame.getByte(0) == 1) { frame.readByte();//from w ww . j av a2 s. c o m int headEndIndex = frame.bytesBefore((byte) -1); int len = (int) readLong(frame, headEndIndex); ByteBuf oldFrame = frame; frame = frame.slice(oldFrame.readerIndex() + 1, len); oldFrame.readerIndex(oldFrame.readerIndex() + 1 + len); } if (frame.getByte(0) == 'b' && frame.getByte(1) == '4') { frame.readShort(); } else if (frame.getByte(0) == 4) { frame.readByte(); } Packet binaryPacket = head.getLastBinaryPacket(); if (binaryPacket != null) { ByteBuf attachBuf; if (frame.getByte(0) == 'b' && frame.getByte(1) == '4') { attachBuf = frame; } else { attachBuf = Base64.encode(frame); } binaryPacket.addAttachment(Unpooled.copiedBuffer(attachBuf)); frame.readerIndex(frame.readerIndex() + frame.readableBytes()); if (binaryPacket.isAttachmentsLoaded()) { LinkedList<ByteBuf> slices = new LinkedList<ByteBuf>(); ByteBuf source = binaryPacket.getDataSource(); for (int i = 0; i < binaryPacket.getAttachments().size(); i++) { ByteBuf attachment = binaryPacket.getAttachments().get(i); ByteBuf scanValue = Unpooled.copiedBuffer("{\"_placeholder\":true,\"num\":" + i + "}", CharsetUtil.UTF_8); int pos = PacketEncoder.find(source, scanValue); if (pos == -1) { throw new IllegalStateException( "Can't find attachment by index: " + i + " in packet source"); } ByteBuf prefixBuf = source.slice(source.readerIndex(), pos - source.readerIndex()); slices.add(prefixBuf); slices.add(QUOTES); slices.add(attachment); slices.add(QUOTES); source.readerIndex(pos + scanValue.readableBytes()); } slices.add(source.slice()); ByteBuf compositeBuf = Unpooled.wrappedBuffer(slices.toArray(new ByteBuf[slices.size()])); parseBody(head, compositeBuf, binaryPacket); head.setLastBinaryPacket(null); return binaryPacket; } } return new Packet(PacketType.MESSAGE); }
From source file:com.corundumstudio.socketio.protocol.PacketDecoder.java
License:Apache License
private void parseBody(ClientHead head, ByteBuf frame, Packet packet) throws IOException { if (packet.getType() == PacketType.MESSAGE) { if (packet.getSubType() == PacketType.CONNECT || packet.getSubType() == PacketType.DISCONNECT) { packet.setNsp(readString(frame)); }/*w ww .j av a2 s .c o m*/ if (packet.getSubType() == PacketType.ACK) { ByteBufInputStream in = new ByteBufInputStream(frame); AckCallback<?> callback = ackManager.getCallback(head.getSessionId(), packet.getAckId()); AckArgs args = jsonSupport.readAckArgs(in, callback); packet.setData(args.getArgs()); } if (packet.getSubType() == PacketType.EVENT || packet.getSubType() == PacketType.BINARY_EVENT) { if (packet.hasAttachments() && !packet.isAttachmentsLoaded()) { packet.setDataSource(Unpooled.copiedBuffer(frame)); frame.readerIndex(frame.readableBytes()); head.setLastBinaryPacket(packet); } else { ByteBufInputStream in = new ByteBufInputStream(frame); Event event = jsonSupport.readValue(packet.getNsp(), in, Event.class); packet.setName(event.getName()); packet.setData(event.getArgs()); } } } }
From source file:com.corundumstudio.socketio.transport.FlashPolicyHandler.java
License:Apache License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { if (msg instanceof ByteBuf) { ByteBuf message = (ByteBuf) msg; ByteBuf data = message.slice(0, requestBuffer.readableBytes()); if (data.equals(requestBuffer)) { message.release();//from w w w . j a v a 2 s . c om ChannelFuture f = ctx.writeAndFlush(Unpooled.copiedBuffer(responseBuffer)); f.addListener(ChannelFutureListener.CLOSE); return; } ctx.pipeline().remove(this); } ctx.fireChannelRead(msg); }
From source file:com.corundumstudio.socketio.transport.FlashUrlLoaderPolicyHandler.java
License:Apache License
/** * Method to write back crossdomain xml/*from w w w .j a v a 2 s. com*/ * * @param request */ private void writeCrossdomainDotXml(final ChannelHandlerContext ctx, final HttpRequest request) { boolean keepAlive = isKeepAlive(request); HttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.copiedBuffer(crossdomain)); response.headers().set(CONTENT_TYPE, "text/xml"); response.headers().set(ACCESS_CONTROL_ALLOW_ORIGIN, "*"); if (keepAlive) { // - http://www.w3.org/Protocols/HTTP/1.1/draft-ietf-http-v11-spec-01.html#Connection response.headers().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE); response.headers().set(HttpHeaders.Values.KEEP_ALIVE, "timeout=2,max=5"); } // Write the response. ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); }
From source file:com.dianping.cat.consumer.dump.TreeHelper.java
License:Open Source License
public static DefaultMessageTree cacheTree(MessageCodec codec, MessageId id) { DefaultMessageTree tree = new DefaultMessageTree(); tree.setDomain(id.getDomain());//from ww w . j av a2 s . c om tree.setHostName("mock-host"); tree.setIpAddress(id.getIpAddress()); tree.setThreadGroupName("test"); tree.setThreadId("test"); tree.setThreadName("test"); tree.setMessage(m_message); tree.setMessageId(id.toString()); // test for rpc index tree.setSessionToken(id.toString()); if (codec != null) { ByteBuf buf = Unpooled.copiedBuffer(m_data); tree.setBuffer(buf); } return tree; }
From source file:com.friz.owari.network.event.impl.RecycleEvent.java
License:Open Source License
public final Object getObj() { if (obj instanceof ByteBuf) { return Unpooled.copiedBuffer((ByteBuf) obj); }// www.ja v a2s . co m return obj; }
From source file:com.github.ambry.rest.NettyResponseChannelTest.java
License:Open Source License
/** * Creates {@link HttpContent} wrapping the {@code content}. * @param content the content to wrap.//from w w w .j a v a 2s . c om * @param isLast {@code true} if this is the last piece of content. {@code false} otherwise. * @return a {@link HttpContent} wrapping the {@code content}. */ private HttpContent createContent(String content, boolean isLast) { ByteBuf buf = Unpooled.copiedBuffer(content.getBytes()); if (isLast) { return new DefaultLastHttpContent(buf); } else { return new DefaultHttpContent(buf); } }
From source file:com.github.mrstampy.kitchensync.netty.channel.payload.ByteArrayByteBufCreator.java
License:Open Source License
@Override public <MSG> ByteBuf createByteBuf(MSG message, InetSocketAddress recipient) { return Unpooled.copiedBuffer((byte[]) message); }
From source file:com.github.mrstampy.kitchensync.stream.header.SequenceHeader.java
License:Open Source License
private void setSequence(byte[] seqBytes) { ByteBuf seq = Unpooled.copiedBuffer(seqBytes); sequence = seq.getLong(0); }
From source file:com.github.mrstampy.pprspray.core.streamer.footer.MediaFooterChunk.java
License:Open Source License
private int extractInt(byte[] message, int start, int end) { byte[] hash = Arrays.copyOfRange(message, start, end); ByteBuf buf = Unpooled.copiedBuffer(hash); return buf.getInt(0); }