List of usage examples for io.netty.buffer ByteBuf writeBytes
public abstract ByteBuf writeBytes(ByteBuffer src);
From source file:com.flysoloing.learning.network.netty.http2.tiles.Http2RequestHandler.java
License:Apache License
private void handlePage(ChannelHandlerContext ctx, String streamId, int latency, FullHttpRequest request) { byte[] body = Html.body(latency); ByteBuf content = ctx.alloc().buffer(Html.HEADER.length + body.length + Html.FOOTER.length); content.writeBytes(Html.HEADER); content.writeBytes(body);/*from w w w .ja v a 2s. c om*/ 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.foilen.smalltools.net.commander.channel.CommanderEncoder.java
License:Open Source License
@Override protected void encode(ChannelHandlerContext ctx, CommandRequest msg, ByteBuf out) throws Exception { String className = msg.commandImplementationClass(); logger.debug("Encoding message of type {} to call the implementation {}", msg.getClass().getName(), className);//from w ww .j a v a2 s .co m try { byte[] classNameBytes = className.getBytes(CharsetTools.UTF_8); String jsonContent = JsonTools.writeToString(msg); byte[] jsonContentBytes = jsonContent.getBytes(CharsetTools.UTF_8); out.writeInt(classNameBytes.length); out.writeBytes(classNameBytes); out.writeInt(jsonContentBytes.length); out.writeBytes(jsonContentBytes); } catch (Exception e) { logger.warn("Problem encoding the message", e); } }
From source file:com.forgetutorials.lib.network.SubPacketTileEntityCustom.java
License:LGPL
@Override public void writeData(ByteBuf data) throws IOException { data.writeInt(this.custom.writerIndex()); data.writeBytes(this.custom); }
From source file:com.friz.network.utility.BufferUtils.java
License:Open Source License
/** * Writes a string/*from w w w . j av a2 s. c o m*/ * @param buffer The ChannelBuffer * @param string The string being wrote. */ public static void putJagString(ByteBuf buffer, String string) { buffer.writeByte(0); buffer.writeBytes(string.getBytes()); buffer.writeByte(0); }
From source file:com.friz.network.utility.BufferUtils.java
License:Open Source License
/** * Encodes a {@link String} with CESU8./*from www. j ava2 s. c om*/ * @param buf The {@link ByteBuf} to put the encoded string. * @param string The {@link String} to encode. */ public static void putCESU8(ByteBuf buf, String string) { int length = string.length(); // Calculate the amount of bytes for the buffer. int bytes = 0; for (int index = 0; index < length; index++) { int character = string.charAt(index); if (character >= 2048) bytes += 3; else if (character >= 128) bytes += 2; else bytes++; } // Allocate a new buffer for appending data. ByteBuffer buffer = ByteBuffer.allocate(bytes); for (int index = 0; index < length; index++) { // Get the character at the current index. int character = string.charAt(index); // A character that is represented more than 1 bytes. if (character >= 128) { // A character that is represented by 3 bytes. if (character >= 2048) { buffer.put((byte) ((character >> 0xC) | 0xE0)); buffer.put((byte) (((character >> 0x6) & 0x3F) | 0x80)); buffer.put((byte) ((character & 0x3F) | 0x80)); // A character that is represented by 2 bytes. } else { buffer.put((byte) ((character >> 0x6) | 0x3015)); buffer.put((byte) ((character & 0x3F) | 0x80)); } } else { // A character in which is represented by a single byte. buffer.put((byte) character); } } buffer.flip(); buf.writeBytes(buffer); }
From source file:com.friz.owari.network.codec.ExchangeEncoder.java
License:Open Source License
@Override protected void encode(ChannelHandlerContext ctx, ExchangeTransferEvent msg, ByteBuf out) throws Exception { if (!out.isWritable()) return;//from w ww . j ava2 s .co m final PublicKey key = msg.getPublicKey(); byte[] enc = key.getEncoded(); out.writeInt(enc.length); out.writeBytes(enc); }
From source file:com.friz.update.network.codec.UpdateEncoder.java
License:Open Source License
@Override protected void encode(ChannelHandlerContext ctx, FileResponseEvent msg, ByteBuf buffer) throws Exception { ByteBuf container = msg.getContainer(); int type = msg.getType(); int file = msg.getFile(); if (!msg.isPriority()) file |= 0x80000000;// w w w .ja v a2 s. c o m buffer.writeByte(type); buffer.writeInt(file); int bytes = container.readableBytes(); if (bytes > BYTES_AFTER_HEADER) bytes = BYTES_AFTER_HEADER; buffer.writeBytes(container.readBytes(bytes)); for (;;) { bytes = container.readableBytes(); if (bytes == 0) break; else if (bytes > BYTES_AFTER_HEADER) bytes = BYTES_AFTER_HEADER; buffer.writeByte(type); buffer.writeInt(file); buffer.writeBytes(container.readBytes(bytes)); } }
From source file:com.gemstone.gemfire.internal.redis.Coder.java
License:Apache License
public static final ByteBuf getBulkStringResponse(ByteBufAllocator alloc, byte[] value) { ByteBuf response = alloc.buffer(value.length + 20); response.writeByte(BULK_STRING_ID);/*from w ww . jav a 2 s . com*/ response.writeBytes(intToBytes(value.length)); response.writeBytes(CRLFar); response.writeBytes(value); response.writeBytes(CRLFar); return response; }
From source file:com.gemstone.gemfire.internal.redis.Coder.java
License:Apache License
public static final ByteBuf getBulkStringResponse(ByteBufAllocator alloc, double value) { ByteBuf response = alloc.buffer(); byte[] doub = doubleToBytes(value); response.writeByte(BULK_STRING_ID);/*w w w . ja va2 s . co m*/ response.writeBytes(intToBytes(doub.length)); response.writeBytes(CRLFar); response.writeBytes(doub); response.writeBytes(CRLFar); return response; }
From source file:com.gemstone.gemfire.internal.redis.Coder.java
License:Apache License
public static final ByteBuf getBulkStringResponse(ByteBufAllocator alloc, String value) { byte[] valueAr = stringToBytes(value); int length = valueAr == null ? 0 : valueAr.length; ByteBuf response = alloc.buffer(length + 20); response.writeByte(BULK_STRING_ID);//from w ww . j av a 2 s. c om response.writeBytes(intToBytes(length)); response.writeBytes(CRLFar); response.writeBytes(valueAr); response.writeBytes(CRLFar); return response; }