List of usage examples for io.netty.buffer ByteBuf writeByte
public abstract ByteBuf writeByte(int value);
From source file:com.corundumstudio.socketio.protocol.PacketEncoder.java
License:Apache License
public void encodePacket(Packet packet, ByteBuf buffer, ByteBufAllocator allocator, boolean binary) throws IOException { ByteBuf buf = buffer; if (!binary) { buf = allocateBuffer(allocator); }//from w w w. j ava2s .c om byte type = toChar(packet.getType().getValue()); buf.writeByte(type); try { switch (packet.getType()) { case PONG: { buf.writeBytes(packet.getData().toString().getBytes(CharsetUtil.UTF_8)); break; } case OPEN: { ByteBufOutputStream out = new ByteBufOutputStream(buf); jsonSupport.writeValue(out, packet.getData()); break; } case MESSAGE: { ByteBuf encBuf = null; if (packet.getSubType() == PacketType.ERROR) { encBuf = allocateBuffer(allocator); ByteBufOutputStream out = new ByteBufOutputStream(encBuf); jsonSupport.writeValue(out, packet.getData()); } if (packet.getSubType() == PacketType.EVENT || packet.getSubType() == PacketType.ACK) { List<Object> values = new ArrayList<Object>(); if (packet.getSubType() == PacketType.EVENT) { values.add(packet.getName()); } encBuf = allocateBuffer(allocator); List<Object> args = packet.getData(); values.addAll(args); ByteBufOutputStream out = new ByteBufOutputStream(encBuf); jsonSupport.writeValue(out, values); if (!jsonSupport.getArrays().isEmpty()) { packet.initAttachments(jsonSupport.getArrays().size()); for (byte[] array : jsonSupport.getArrays()) { packet.addAttachment(Unpooled.wrappedBuffer(array)); } packet.setSubType(PacketType.BINARY_EVENT); } } byte subType = toChar(packet.getSubType().getValue()); buf.writeByte(subType); if (packet.hasAttachments()) { byte[] ackId = toChars(packet.getAttachments().size()); buf.writeBytes(ackId); buf.writeByte('-'); } if (packet.getSubType() == PacketType.CONNECT) { if (!packet.getNsp().isEmpty()) { buf.writeBytes(packet.getNsp().getBytes(CharsetUtil.UTF_8)); } } else { if (!packet.getNsp().isEmpty()) { buf.writeBytes(packet.getNsp().getBytes(CharsetUtil.UTF_8)); buf.writeByte(','); } } if (packet.getAckId() != null) { byte[] ackId = toChars(packet.getAckId()); buf.writeBytes(ackId); } if (encBuf != null) { buf.writeBytes(encBuf); encBuf.release(); } break; } } } finally { // we need to write a buffer in any case if (!binary) { buffer.writeByte(0); int length = buf.writerIndex(); buffer.writeBytes(longToBytes(length)); buffer.writeByte(0xff); buffer.writeBytes(buf); buf.release(); } } }
From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandler.java
License:Apache License
private static BinaryMemcacheRequest handleSubdocumentRequest(ChannelHandlerContext ctx, BinarySubdocRequest msg) {// w w w .j a v a 2 s .c om byte[] key = msg.keyBytes(); short keyLength = (short) key.length; ByteBuf extras = ctx.alloc().buffer(3, 7); //extras can be 7 bytes if there is an expiry byte extrasLength = 3; //by default 2 bytes for pathLength + 1 byte for "command" flags extras.writeShort(msg.pathLength()); long cas = 0L; if (msg instanceof BinarySubdocMutationRequest) { BinarySubdocMutationRequest mut = (BinarySubdocMutationRequest) msg; //for now only possible command flag is MKDIR_P (and it makes sense in mutations only) if (mut.createIntermediaryPath()) { extras.writeByte(0 | SUBDOC_BITMASK_MKDIR_P); } else { extras.writeByte(0); } if (mut.expiration() != 0L) { extrasLength = 7; extras.writeInt(mut.expiration()); } cas = mut.cas(); } else { extras.writeByte(0); } FullBinaryMemcacheRequest request = new DefaultFullBinaryMemcacheRequest(key, extras, msg.content()); request.setOpcode(msg.opcode()).setKeyLength(keyLength).setExtrasLength(extrasLength) .setTotalBodyLength(keyLength + msg.content().readableBytes() + extrasLength).setCAS(cas); return request; }
From source file:com.couchbase.client.core.message.kv.subdoc.multi.SubMultiLookupRequest.java
License:Apache License
private static ByteBuf encode(List<LookupCommand> commands) { CompositeByteBuf compositeBuf = Unpooled.compositeBuffer(commands.size()); //FIXME pooled allocator? for (LookupCommand command : commands) { byte[] pathBytes = command.path().getBytes(CharsetUtil.UTF_8); short pathLength = (short) pathBytes.length; ByteBuf commandBuf = Unpooled.buffer(4 + pathLength); //FIXME a way of using the pooled allocator? commandBuf.writeByte(command.opCode()); commandBuf.writeByte(0); //no flags supported for lookup commandBuf.writeShort(pathLength); //no value length commandBuf.writeBytes(pathBytes); compositeBuf.addComponent(commandBuf); compositeBuf.writerIndex(compositeBuf.writerIndex() + commandBuf.readableBytes()); }// w ww .j ava 2 s . co m return compositeBuf; }
From source file:com.couchbase.client.core.message.kv.subdoc.multi.SubMultiMutationRequest.java
License:Apache License
private static ByteBuf encode(List<MutationCommand> commands) { //FIXME a way of using the pooled allocator? CompositeByteBuf compositeBuf = Unpooled.compositeBuffer(commands.size()); for (MutationCommand command : commands) { byte[] pathBytes = command.path().getBytes(CharsetUtil.UTF_8); short pathLength = (short) pathBytes.length; ByteBuf commandBuf = Unpooled.buffer(4 + pathLength + command.fragment().readableBytes()); commandBuf.writeByte(command.opCode()); if (command.createIntermediaryPath()) { commandBuf.writeByte(KeyValueHandler.SUBDOC_BITMASK_MKDIR_P); //0 | SUBDOC_BITMASK_MKDIR_P } else {//from w ww . jav a 2 s . co m commandBuf.writeByte(0); } commandBuf.writeShort(pathLength); commandBuf.writeInt(command.fragment().readableBytes()); commandBuf.writeBytes(pathBytes); //copy the fragment but don't move indexes (in case it is retained and reused) commandBuf.writeBytes(command.fragment(), command.fragment().readerIndex(), command.fragment().readableBytes()); //eagerly release the fragment once it's been copied command.fragment().release(); //add the command to the composite buffer compositeBuf.addComponent(commandBuf); compositeBuf.writerIndex(compositeBuf.writerIndex() + commandBuf.readableBytes()); } return compositeBuf; }
From source file:com.datastax.driver.core.CBUtil.java
License:Apache License
public static void writeInet(InetSocketAddress inet, ByteBuf cb) { byte[] address = inet.getAddress().getAddress(); cb.writeByte(address.length); cb.writeBytes(address);// w w w .j av a 2s .c o m cb.writeInt(inet.getPort()); }
From source file:com.Da_Technomancer.crossroads.API.packets.Message.java
License:Creative Commons License
private static void writeByte(byte b, ByteBuf buf) { buf.writeByte(b); }
From source file:com.dc.gameserver.ServerCore.Controller.AbstractController.AbstractController.java
License:Apache License
/** * 4/*from w w w . j ava 2 s .c o m*/ * * @param ID ?? * @param messageLite * @return */ public static ByteBuf wrappedBufferInt(int ID, MessageLite messageLite) { byte[] src = messageLite.toByteArray(); int length = 8 + src.length; ByteBuf buffer = PooledByteBufAllocator.DEFAULT.heapBuffer(length, length); buffer.setIndex(0, 0x4);//writeIndex? //4 buffer.writeByte(ID); // 4 buffer.writeBytes(messageLite.toByteArray()); buffer.setInt(0, buffer.writerIndex() - 0x4); messageLite = null; return buffer; }
From source file:com.dc.gameserver.ServerCore.Controller.AbstractController.AbstractController.java
License:Apache License
/** * ?? </br>/*from w w w .jav a2 s . co m*/ * Encoder buffer </br> * ?4 </br> * proto buffer </br> * </br> * * @param arg1 * @param arg2 * @param messageLite * @return + ID+protoBufferData */ public static ByteBuf wrappedBufferInt(int arg1, int arg2, MessageLite messageLite) { byte[] src = messageLite.toByteArray(); int length = 12 + src.length; ByteBuf buffer = PooledByteBufAllocator.DEFAULT.heapBuffer(length, length); buffer.setIndex(0, 0x4);//writeIndex? //4 buffer.writeByte(arg1); // 4 buffer.writeByte(arg2); // 4 buffer.writeBytes(messageLite.toByteArray()); buffer.setInt(0, buffer.writerIndex() - 0x4); messageLite = null; return buffer; }
From source file:com.dc.gameserver.ServerCore.Controller.AbstractController.AbstractController.java
License:Apache License
/** * ?? </br>/*from ww w.ja v a 2 s.c o m*/ * Encoder buffer </br> * ?2 </br> * proto buffer </br> * </br> * * @param arg1 * @param arg2 * @param messageLite * @return + ID+protoBufferData */ public static ByteBuf wrappedBufferShort(int arg1, int arg2, MessageLite messageLite) { byte[] src = messageLite.toByteArray(); int length = 10 + src.length; ByteBuf buffer = PooledByteBufAllocator.DEFAULT.heapBuffer(length, length); buffer.setIndex(0, 0x2);//writeIndex? buffer.writeByte(arg1); buffer.writeByte(arg2); buffer.writeBytes(messageLite.toByteArray()); /**?2*/ buffer.setShort(0, buffer.writerIndex() - 0x2); messageLite = null; return buffer; }
From source file:com.dempe.chat.common.mqtt.codec.ConnAckEncoder.java
License:Open Source License
@Override protected void encode(ChannelHandlerContext chc, ConnAckMessage message, ByteBuf out) { out.writeByte(AbstractMessage.CONNACK << 4); out.writeBytes(Utils.encodeRemainingLength(2)); out.writeByte(message.isSessionPresent() ? 0x01 : 0x00); out.writeByte(message.getReturnCode()); }