List of usage examples for io.netty.buffer ByteBuf ensureWritable
public abstract ByteBuf ensureWritable(int minWritableBytes);
From source file:org.eclipse.neoscada.protocol.iec60870.apci.APDUEncoder.java
License:Open Source License
private void handleSFormat(final Supervisory msg, ByteBuf out) { out = out.order(ByteOrder.LITTLE_ENDIAN); out.ensureWritable(6); out.writeByte(Constants.START_BYTE); out.writeByte(4);//from w w w .ja v a 2s . c o m out.writeBytes(new byte[] { 0x01, 0x00 }); out.writeShort(msg.getReceiveSequenceNumber() << 1); }
From source file:org.eclipse.neoscada.protocol.iec60870.apci.APDUEncoder.java
License:Open Source License
private void handleUFormat(final UnnumberedControl msg, final ByteBuf out) { out.ensureWritable(6); out.writeByte(Constants.START_BYTE); out.writeByte(4);//ww w. j a v a2 s. c o m out.writeByte(msg.getFunction().getNumericValue() | 0x03 /* bits 1 and 2*/); out.writeZero(3); }
From source file:org.evilco.network.rcon.common.codec.FrameCodec.java
License:Apache License
/** * {@inheritDoc}/*from w w w. j a va 2 s . com*/ */ @Override protected void encode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, ByteBuf byteBuf2) throws Exception { // set order byteBuf2 = byteBuf2.order(ByteOrder.LITTLE_ENDIAN); // log getLogger().debug("Encoding frame for " + byteBuf.readableBytes() + " bytes of data."); // ensure output buffer is writable byteBuf2.ensureWritable((4 + byteBuf.readableBytes())); // write packet length byteBuf2.writeInt(byteBuf.readableBytes()); // write packet byteBuf2.writeBytes(byteBuf); }
From source file:qunar.tc.qmq.netty.EncodeHandler.java
License:Apache License
@Override protected void encode(ChannelHandlerContext ctx, Datagram msg, ByteBuf out) throws Exception { int start = out.writerIndex(); int headerStart = start + RemotingHeader.LENGTH_FIELD; out.ensureWritable(RemotingHeader.LENGTH_FIELD); out.writerIndex(headerStart);//w ww .java 2 s .c o m final RemotingHeader header = msg.getHeader(); encodeHeader(header, out); int headerSize = out.writerIndex() - headerStart; msg.writeBody(out); int end = out.writerIndex(); int total = end - start - RemotingHeader.TOTAL_SIZE_LEN; out.writerIndex(start); out.writeInt(total); out.writeShort((short) headerSize); out.writerIndex(end); }
From source file:qunar.tc.qmq.protocol.MessagesPayloadHolder.java
License:Apache License
private void serializeMessage(BaseMessage message, ByteBuf out) { int crcIndex = out.writerIndex(); // sizeof(bodyCrc<long>) out.ensureWritable(8); out.writerIndex(crcIndex + 8);//from w w w .j a v a 2 s.c o m final int messageStart = out.writerIndex(); // flag byte flag = 0; //?(1)?(0)?(1)?(0)?Tag flag = Flags.setDelay(flag, DelayUtil.isDelayMessage(message)); //in avoid add tag after sendMessage Set<String> tags = new HashSet<>(message.getTags()); flag = Flags.setTags(flag, hasTags(tags)); out.writeByte(flag); // created time out.writeLong(message.getCreatedTime().getTime()); if (Flags.isDelay(flag)) { out.writeLong(message.getScheduleReceiveTime().getTime()); } else { // expired time out.writeLong(System.currentTimeMillis()); } // subject PayloadHolderUtils.writeString(message.getSubject(), out); // message id PayloadHolderUtils.writeString(message.getMessageId(), out); writeTags(tags, out); out.markWriterIndex(); // writerIndex + sizeof(bodyLength<int>) final int bodyStart = out.writerIndex() + 4; out.ensureWritable(4); out.writerIndex(bodyStart); serializeMap(message.getAttrs(), out); final int bodyEnd = out.writerIndex(); final int messageEnd = out.writerIndex(); final int bodyLen = bodyEnd - bodyStart; final int messageLength = bodyEnd - messageStart; // write body length out.resetWriterIndex(); out.writeInt(bodyLen); // write message crc out.writerIndex(crcIndex); out.writeLong(messageCrc(out, messageStart, messageLength)); out.writerIndex(messageEnd); }
From source file:tp.MyJZLibDecoder.java
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { if (!in.isReadable()) { return;//from ww w. j a v a2s .c om } try { // Configure input. int inputLength = in.readableBytes(); //totalBytes += inputLength; z.avail_in = inputLength; if (in.hasArray()) { z.next_in = in.array(); z.next_in_index = in.arrayOffset() + in.readerIndex(); } else { byte[] array = new byte[inputLength]; in.getBytes(in.readerIndex(), array); z.next_in = array; z.next_in_index = 0; } int oldNextInIndex = z.next_in_index; // Configure output. int maxOutputLength = inputLength << 1; ByteBuf decompressed = ctx.alloc().heapBuffer(maxOutputLength); try { loop: for (;;) { z.avail_out = maxOutputLength; decompressed.ensureWritable(maxOutputLength); z.next_out = decompressed.array(); z.next_out_index = decompressed.arrayOffset() + decompressed.writerIndex(); int oldNextOutIndex = z.next_out_index; // Decompress 'in' into 'out' int resultCode = z.inflate(JZlib.Z_SYNC_FLUSH); int outputLength = z.next_out_index - oldNextOutIndex; if (outputLength > 0) { decompressed.writerIndex(decompressed.writerIndex() + outputLength); } switch (resultCode) { case JZlib.Z_NEED_DICT: if (dictionary == null) { ZlibUtil.fail(z, "decompression failure", resultCode); } else { resultCode = z.inflateSetDictionary(dictionary, dictionary.length); if (resultCode != JZlib.Z_OK) { ZlibUtil.fail(z, "failed to set the dictionary", resultCode); } } break; case JZlib.Z_STREAM_END: finished = true; // Do not decode anymore. z.inflateEnd(); break loop; case JZlib.Z_OK: break; case JZlib.Z_BUF_ERROR: if (z.avail_in <= 0) { //ZlibUtil.fail(z, "decompression failure [Z_BUF_ERROR] ", resultCode); break loop; } break; default: ZlibUtil.fail(z, "decompression failure", resultCode); } } } finally { in.skipBytes(z.next_in_index - oldNextInIndex); //System.err.println("[recived][numBytes] = "+inputLength); if (decompressed.isReadable()) { out.add(decompressed); } else { decompressed.release(); } } } finally { // Deference the external references explicitly to tell the VM that // the allocated byte arrays are temporary so that the call stack // can be utilized. // I'm not sure if the modern VMs do this optimization though. z.next_in = null; z.next_out = null; } }
From source file:tp.MyJZLibEncoder.java
@Override protected void encode(ChannelHandlerContext ctx, ByteBuf in, ByteBuf out) throws Exception { if (finished.get()) { out.writeBytes(in);//ww w. j a v a 2 s .c om return; } ByteBuf uncompressed = in; byte[] inAry = new byte[uncompressed.readableBytes()]; uncompressed.readBytes(inAry); int sizeEstimate = (int) Math.ceil(inAry.length * 1.001) + 12; out.ensureWritable(sizeEstimate); synchronized (deflater) { if (gzip) { crc.update(inAry); if (writeHeader) { out.writeBytes(gzipHeader); writeHeader = false; } } deflater.setInput(inAry); while (!deflater.needsInput()) { int numBytes = deflater.deflate(encodeBuf, 0, encodeBuf.length, Deflater.SYNC_FLUSH); out.writeBytes(encodeBuf, 0, numBytes); //System.err.println("[Send][numBytes] = "+numBytes); } } }