List of usage examples for io.netty.buffer ByteBuf writeBytes
public abstract ByteBuf writeBytes(ByteBuffer src);
From source file:com.hop.hhxx.example.http2.helloworld.server.HelloWorldHttp2Handler.java
License:Apache License
@Override public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int padding, boolean endOfStream) { if (endOfStream) { ByteBuf content = ctx.alloc().buffer(); content.writeBytes(RESPONSE_BYTES.duplicate()); ByteBufUtil.writeAscii(content, " - via HTTP/2"); sendResponse(ctx, streamId, content); }/*from ww w . j a va 2 s . c om*/ }
From source file:com.hop.hhxx.example.http2.tiles.Http2RequestHandler.java
License:Apache License
private void handlePage(ChannelHandlerContext ctx, String streamId, int latency, FullHttpRequest request) { byte[] body = io.netty.example.http2.tiles.Html.body(latency); ByteBuf content = ctx.alloc().buffer(io.netty.example.http2.tiles.Html.HEADER.length + body.length + io.netty.example.http2.tiles.Html.FOOTER.length); content.writeBytes(io.netty.example.http2.tiles.Html.HEADER); content.writeBytes(body);/*from ww w . ja v a2 s. co m*/ content.writeBytes(Html.FOOTER); FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, content); response.headers().set(CONTENT_TYPE, "text/html; charset=UTF-8"); sendResponse(ctx, streamId, latency, response, request); }
From source file:com.hxr.javatone.concurrency.netty.official.factorial.NumberEncoder.java
License:Apache License
@Override protected void encode(ChannelHandlerContext ctx, Number msg, ByteBuf out) throws Exception { // Convert to a BigInteger first for easier implementation. BigInteger v;/*from www. j av a 2 s .c o m*/ if (msg instanceof BigInteger) { v = (BigInteger) msg; } else { v = new BigInteger(String.valueOf(msg)); } // Convert the number into a byte array. byte[] data = v.toByteArray(); int dataLength = data.length; // Write a message. out.writeByte((byte) 'F'); // magic number out.writeInt(dataLength); // data length out.writeBytes(data); // data }
From source file:com.ibasco.agql.protocols.valve.source.query.handlers.SourceQueryPacketAssembler.java
License:Open Source License
/** * Re-assemble's the packets from the container. * * @param splitPackets The {@link SplitPacketContainer} to be re-assembled * * @return Returns true if the re-assembly process has completed successfully *///from w w w . j a v a2s . c om private boolean reassembleSplitPackets(SplitPacketContainer splitPackets, ByteBuf packetBuffer, boolean isCompressed, int decompressedSize, int packetChecksum) { log.trace("reassembleSplitPackets : START"); if (packetBuffer == null) throw new IllegalArgumentException("Packet Buffer is not initialized"); splitPackets.forEachEntry(packetEntry -> { log.debug("--> Packet #{} : {}", packetEntry.getKey(), packetEntry.getValue()); //Throw exception if compression is set. Not yet supported. if (isCompressed) throw new IllegalStateException("Compression is not yet supported at this time sorry"); //TODO: Is this still needed? /*if(isCompressed) { //From Steam Condenser (thanks Koraktor) try { ByteArrayInputStream stream = new ByteArrayInputStream(packetData); stream.read(); stream.read(); BZip2CompressorInputStream bzip2 = new BZip2CompressorInputStream(stream); byte[] uncompressedPacketData = new byte[uncompressedSize]; bzip2.read(uncompressedPacketData, 0, uncompressedSize); CRC32 crc32 = new CRC32(); crc32.update(uncompressedPacketData); int crc32checksum = (int) crc32.getValue(); if (crc32checksum != packetChecksum) { throw new PacketFormatException( "CRC32 checksum mismatch of uncompressed packet data."); } packetData = uncompressedPacketData; } catch(IOException e) { throw new SteamCondenserException(e.getMessage(), e); } }*/ packetBuffer.writeBytes(packetEntry.getValue()); }); log.trace("reassembleSplitPackets : END"); return packetBuffer.readableBytes() > 0; }
From source file:com.ibasco.agql.protocols.valve.source.query.SourcePacketBuilder.java
License:Open Source License
/** * Convert a source packet instance to it's byte representation * * @param packet The {@link SourceServerPacket} to convert * * @return Returns the deconstructed packet in byte array form *//*from ww w. j ava2s. co m*/ @Override public byte[] deconstruct(SourceServerPacket packet) { if (packet == null) throw new IllegalArgumentException("Invalid packet specified in the arguments."); byte[] payload = packet.getPayload(); byte[] protocolHeader = packet.getProtocolHeader(); byte[] packetHeader = packet.getPacketHeader(); int payloadSize = payload != null ? payload.length : 0; int protocolHeaderSize = protocolHeader != null ? protocolHeader.length : 0; int packetHeaderSize = packetHeader != null ? packetHeader.length : 0; int packetSize = protocolHeaderSize + packetHeaderSize + payloadSize; //Allocate our buffer final ByteBuf buf = createBuffer(packetSize); byte[] data; try { //Include Protocol Header if available if (protocolHeaderSize > 0) buf.writeBytes(protocolHeader); //Include Packet Header if (packetHeaderSize > 0) buf.writeBytes(packetHeader); //Include Payload (if available) if (payloadSize > 0) buf.writeBytes(payload); //Store the buffer into a byte array data = new byte[buf.readableBytes()]; if (data.length > 0) { buf.readBytes(data); } } finally { buf.release(); } log.debug("Constructing '{}' with total of {} bytes of data", packet.getClass().getSimpleName(), data.length); //Return the backing array representation return data; }
From source file:com.ibasco.agql.protocols.valve.source.query.SourceRconPacketBuilder.java
License:Open Source License
@Override public byte[] deconstruct(SourceRconPacket packet) { //1) size = int (4 bytes) //2) id = int (4 bytes) //3) type = int (4 bytes) //4) body = string (length + 1 null byte) //5) trailer = null byte int id = packet.getId(); int type = packet.getType(); final String body = StringUtils.defaultString(packet.getBody()); int packetSize = 10 + body.length(); final ByteBuf buf = createBuffer(packetSize); byte[] data;//from ww w . j a va 2s. co m try { buf.writeIntLE(packetSize); buf.writeIntLE(id); buf.writeIntLE(type); buf.writeBytes(body.getBytes()); buf.writeByte(0); buf.writeByte(0); data = new byte[buf.readableBytes()]; buf.readBytes(data); } finally { buf.release(); } return data; }
From source file:com.ibasco.agql.protocols.valve.steam.master.MasterServerPacketBuilder.java
License:Open Source License
@Override public byte[] deconstruct(MasterServerPacket packet) { if (packet == null) throw new IllegalArgumentException("Invalid packet specified in the arguments."); byte[] payload = packet.getPayload(); byte[] packetHeader = packet.getPacketHeader(); int payloadSize = payload != null ? payload.length : 0; int packetHeaderSize = packetHeader != null ? packetHeader.length : 0; int packetSize = packetHeaderSize + payloadSize; //Allocate our buffer final ByteBuf buf = createBuffer(packetSize); byte[] data;//from w ww.j av a2 s.co m try { //Include Packet Header if (packetHeaderSize > 0) buf.writeBytes(packetHeader); //Include Payload (if available) if (payloadSize > 0) buf.writeBytes(payload); //Store the buffer into a byte array data = new byte[buf.readableBytes()]; if (data.length > 0) { buf.readBytes(data); } } finally { buf.release(); } log.debug("Constructing '{}' with total of {} bytes of data", packet.getClass().getSimpleName(), data.length); //Return the backing array representation return data; }
From source file:com.ibasco.agql.protocols.valve.steam.master.packets.MasterServerRequestPacket.java
License:Open Source License
@Override public byte[] getPayload() { String filterString = this.filter.toString(); int payloadSize = (3 + filterString.length() + (this.startIp.length())); final ByteBuf payload = PooledByteBufAllocator.DEFAULT.buffer(payloadSize); try {/*from w w w. j av a 2 s .c o m*/ payload.writeByte(getRegion()); payload.writeBytes(getStartIp().getBytes()); payload.writeByte(0); //terminating byte payload.writeBytes(filterString.getBytes()); byte[] payloadBytes = new byte[payload.readableBytes()]; payload.readBytes(payloadBytes); return payloadBytes; } finally { payload.release(); } }
From source file:com.ibm.crail.namenode.rpc.netty.common.NettyRequest.java
License:Apache License
public int write(ByteBuf buffer) throws IOException { buffer.writeLong(cookie); //8 buffer.writeShort(cmd); //2 buffer.writeShort(type); //2 int written = 12; nioBuffer.clear();/*from w w w . ja v a 2 s.c om*/ switch (type) { case NameNodeProtocol.REQ_CREATE_FILE: written += createFileReq.write(nioBuffer); break; case NameNodeProtocol.REQ_GET_FILE: written += fileReq.write(nioBuffer); break; case NameNodeProtocol.REQ_SET_FILE: written += setFileReq.write(nioBuffer); break; case NameNodeProtocol.REQ_REMOVE_FILE: written += removeReq.write(nioBuffer); break; case NameNodeProtocol.REQ_RENAME_FILE: written += renameFileReq.write(nioBuffer); break; case NameNodeProtocol.REQ_GET_BLOCK: written += getBlockReq.write(nioBuffer); break; case NameNodeProtocol.REQ_GET_LOCATION: written += getLocationReq.write(nioBuffer); break; case NameNodeProtocol.REQ_SET_BLOCK: written += setBlockReq.write(nioBuffer); break; case NameNodeProtocol.REQ_GET_DATANODE: written += getDataNodeReq.write(nioBuffer); break; case NameNodeProtocol.REQ_DUMP_NAMENODE: written += dumpNameNodeReq.write(nioBuffer); break; case NameNodeProtocol.REQ_PING_NAMENODE: written += pingNameNodeReq.write(nioBuffer); break; } /* instead of flip you want to clear it */ nioBuffer.clear(); buffer.writeBytes(nioBuffer); return written; }
From source file:com.ibm.crail.namenode.rpc.netty.common.NettyResponse.java
License:Apache License
public int write(ByteBuf buffer) { buffer.writeLong(cookie);//from w w w . j av a2 s. com buffer.writeShort(type); buffer.writeShort(error); int written = 12; nioBuffer.clear(); switch (type) { case NameNodeProtocol.RES_VOID: written += voidRes.write(nioBuffer); break; case NameNodeProtocol.RES_CREATE_FILE: written += createFileRes.write(nioBuffer); break; case NameNodeProtocol.RES_GET_FILE: written += getFileRes.write(nioBuffer); break; case NameNodeProtocol.RES_DELETE_FILE: written += delFileRes.write(nioBuffer); break; case NameNodeProtocol.RES_RENAME_FILE: written += renameRes.write(nioBuffer); break; case NameNodeProtocol.RES_GET_BLOCK: written += getBlockRes.write(nioBuffer); break; case NameNodeProtocol.RES_GET_LOCATION: written += getLocationRes.write(nioBuffer); break; case NameNodeProtocol.RES_GET_DATANODE: written += getDataNodeRes.write(nioBuffer); break; case NameNodeProtocol.RES_PING_NAMENODE: written += pingNameNodeRes.write(nioBuffer); break; } /* reset and copy becuase we want to copy the whole capacity of the nio buffer */ nioBuffer.clear(); buffer.writeBytes(nioBuffer); return written; }