List of usage examples for io.netty.buffer ByteBuf readerIndex
public abstract int readerIndex();
From source file:com.antsdb.saltedfish.server.mysql.PacketEncoder.java
License:Open Source License
/** * Add header to finish the full packet/*from w w w. j a va2s. co m*/ * @param out * @param packetSeq * @param writeBodyFunc write packet body function */ public static void writePacket(ByteBuf out, byte packetSeq, Callback writeBodyFunc) { int start = out.writerIndex(); out.writeZero(4); writeBodyFunc.callback(); int end = out.writerIndex(); out.writeByte(0); out.writerIndex(start); int length = end - start - MySQLPacket.packetHeaderSize; BufferUtils.writeLongInt(out, length); out.writeByte(packetSeq); out.writerIndex(end); if (_log.isTraceEnabled()) { int readerIndex = out.readerIndex(); out.readerIndex(start); byte[] bytes = new byte[end - start]; out.readBytes(bytes); out.readerIndex(readerIndex); String dump = '\n' + UberUtil.hexDump(bytes); _log.trace(dump); } }
From source file:com.antsdb.saltedfish.server.mysql.ReplicationPacketDecoder.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { // do we have length field in buffer ? if (!in.isReadable(4)) { return;//from www . j a v a2 s. co m } // do we have entire packet in the buffer? in.markReaderIndex(); int size = BufferUtils.readLongInt(in) + 1; if (size == (0x00ffffff + 1)) { return; } if (!in.isReadable(size)) { in.resetReaderIndex(); return; } int pos = in.readerIndex(); try { ReplicationPacket packet = readPacket(in, size); out.add(packet); } finally { int currentPos = in.readerIndex(); in.skipBytes(size - (currentPos - pos)); } }
From source file:com.antsdb.saltedfish.server.mysql.util.BindValueUtil.java
License:Open Source License
public static long read(Heap heap, ByteBuf buf, int type) { long pValue = 0; switch (type & 0xff) { case Fields.FIELD_TYPE_TINY: pValue = Int4.allocSet(heap, buf.readByte()); break;/* w ww . j av a 2 s .c om*/ case Fields.FIELD_TYPE_SHORT: pValue = Int4.allocSet(heap, (short) BufferUtils.readInt(buf)); break; case Fields.FIELD_TYPE_LONG: pValue = Int4.allocSet(heap, BufferUtils.readLong(buf)); break; case Fields.FIELD_TYPE_LONGLONG: pValue = Int8.allocSet(heap, BufferUtils.readLongLong(buf)); break; case Fields.FIELD_TYPE_FLOAT: pValue = Float4.allocSet(heap, BufferUtils.readFloat(buf)); break; case Fields.FIELD_TYPE_DOUBLE: pValue = Float8.allocSet(heap, BufferUtils.readDouble(buf)); break; case Fields.FIELD_TYPE_TIME: case Fields.FIELD_TYPE_TIME2: pValue = FishTime.allocSet(heap, BufferUtils.readTime(buf)); break; case Fields.FIELD_TYPE_DATE: pValue = FishDate.allocSet(heap, BufferUtils.readDate(buf)); break; case Fields.FIELD_TYPE_DATETIME: case Fields.FIELD_TYPE_TIMESTAMP: case Fields.FIELD_TYPE_DATETIME2: case Fields.FIELD_TYPE_TIMESTAMP2: pValue = FishTimestamp.allocSet(heap, BufferUtils.readTimestamp(buf)); break; case Fields.FIELD_TYPE_VAR_STRING: case Fields.FIELD_TYPE_STRING: case Fields.FIELD_TYPE_VARCHAR: int len = (int) BufferUtils.readLength(buf); long pData = buf.memoryAddress() + buf.readerIndex(); pValue = FishUtf8.allocSet(heap, pData, len); buf.readerIndex(buf.readerIndex() + len); break; case Fields.FIELD_TYPE_DECIMAL: case Fields.FIELD_TYPE_NEW_DECIMAL: pValue = FishNumber.allocSet(heap, BufferUtils.readBigDecimal(buf)); break; default: throw new IllegalArgumentException("bindValue error,unsupported type:" + type); } return pValue; }
From source file:com.antsdb.saltedfish.server.mysql.util.BufferUtils.java
License:Open Source License
public static String readString(ByteBuf buf) { String s;/*from www .ja v a 2 s .com*/ int bytes = buf.bytesBefore((byte) 0); if (bytes == -1) { // string might not be terminated by 0 bytes = buf.readableBytes(); s = buf.toString(buf.readerIndex(), bytes, Charset.defaultCharset()); buf.skipBytes(bytes); } else { s = buf.toString(buf.readerIndex(), bytes, Charset.defaultCharset()); buf.skipBytes(bytes + 1); } return s; }
From source file:com.baidu.jprotobuf.pbrpc.transport.handler.RpcDataPackageDecoder.java
License:Apache License
protected Object decode(ChannelHandlerContext ctx, ByteBuf buf) throws Exception { // Make sure if the length field was received. if (buf.readableBytes() < RpcHeadMeta.SIZE) { // The length field was not received yet - return null. // This method will be invoked again when more packets are // received and appended to the buffer. return null; }// ww w. j a va2s .c o m // The length field is in the buffer. // Mark the current buffer position before reading the length field // because the whole frame might not be in the buffer yet. // We will reset the buffer position to the marked position if // there's not enough bytes in the buffer. buf.markReaderIndex(); // Read the RPC head long rpcMessageDecoderStart = System.nanoTime(); ByteBuffer buffer = buf.nioBuffer(buf.readerIndex(), RpcHeadMeta.SIZE); buffer.order(ByteOrder.LITTLE_ENDIAN); byte[] bytes = new byte[RpcHeadMeta.SIZE]; buffer.get(bytes); RpcHeadMeta headMeta = new RpcHeadMeta(); headMeta.read(bytes); // get total message size int messageSize = headMeta.getMessageSize() + RpcHeadMeta.SIZE; // Make sure if there's enough bytes in the buffer. if (buf.readableBytes() < messageSize) { // The whole bytes were not received yet - return null. // This method will be invoked again when more packets are // received and appended to the buffer. // Reset to the marked position to read the length field again // next time. buf.resetReaderIndex(); return null; } // check magic code String magicCode = headMeta.getMagicCodeAsString(); if (!ProtocolConstant.MAGIC_CODE.equals(magicCode)) { throw new Exception("Error magic code:" + magicCode); } // There's enough bytes in the buffer. Read it. byte[] totalBytes = new byte[messageSize]; buf.readBytes(totalBytes, 0, messageSize); RpcDataPackage rpcDataPackage = new RpcDataPackage(); rpcDataPackage.setTimeStamp(System.currentTimeMillis()); rpcDataPackage.read(totalBytes); // check if a chunk package if (rpcDataPackage.isChunkPackage()) { Long chunkStreamId = rpcDataPackage.getChunkStreamId(); RpcDataPackage chunkDataPackage = tempTrunkPackages.get(chunkStreamId); if (chunkDataPackage == null) { chunkDataPackage = rpcDataPackage; tempTrunkPackages.put(chunkStreamId, rpcDataPackage); } else { chunkDataPackage.mergeData(rpcDataPackage.getData()); } if (rpcDataPackage.isFinalPackage()) { chunkDataPackage.chunkInfo(chunkStreamId, -1); tempTrunkPackages.remove(chunkStreamId); return chunkDataPackage; } return null; } long rpcMessageDecoderEnd = System.nanoTime(); LOG.log(Level.FINE, "[profiling] nshead decode cost : " + (rpcMessageDecoderEnd - rpcMessageDecoderStart) / 1000); return rpcDataPackage; }
From source file:com.beeswax.hexbid.parser.BidProtobufParser.java
License:Apache License
/** * Parse serialized protocol buffer Bytebuf to protobuf object.</br> * Preferencing implementation of {@link ProtobufDecoder} * /* w w w. j a v a 2 s. c o m*/ * @param bytebuf * @return protocol buffer message * @throws InvalidProtocolBufferException */ public static <T extends Message.Builder> Message parseProtoBytebuf(ByteBuf bytebuf, T messageBuilder) throws InvalidProtocolBufferException { final byte[] array; final int offset; final int length = bytebuf.readableBytes(); if (bytebuf.hasArray()) { array = bytebuf.array(); offset = bytebuf.arrayOffset() + bytebuf.readerIndex(); } else { array = new byte[length]; bytebuf.getBytes(bytebuf.readerIndex(), array, 0, length); offset = 0; } return messageBuilder.mergeFrom(array, offset, length).buildPartial(); }
From source file:com.cc.nettytest.proxy.decoder.CCLengthFieldBasedFrameDecoder.java
License:Apache License
@Override public Object decode(ChannelHandlerContext ctx, ByteBuf inBuffer) throws Exception { if (discardingTooLongFrame) { long bytesToDiscard = this.bytesToDiscard; int localBytesToDiscard = (int) Math.min(bytesToDiscard, inBuffer.readableBytes()); inBuffer.skipBytes(localBytesToDiscard); bytesToDiscard -= localBytesToDiscard; this.bytesToDiscard = bytesToDiscard; failIfNecessary(ctx, false);//from ww w . java 2 s . c om return null; } if (inBuffer.readableBytes() < lengthFieldEndOffset) { return null; } int actualLengthFieldOffset = inBuffer.readerIndex() + lengthFieldOffset; long frameLength; switch (lengthFieldLength) { case 1: frameLength = inBuffer.getUnsignedByte(actualLengthFieldOffset); break; case 2: frameLength = inBuffer.getUnsignedShort(actualLengthFieldOffset); break; case 3: frameLength = inBuffer.getUnsignedMedium(actualLengthFieldOffset); break; case 4: frameLength = ByteBufUtil.swapInt((int) inBuffer.getUnsignedInt(actualLengthFieldOffset)); //SWAP FOR UIMANAGER break; case 8: frameLength = inBuffer.getLong(actualLengthFieldOffset); break; default: throw new Error("should not reach here"); } if (frameLength < 0) { inBuffer.skipBytes(lengthFieldEndOffset); throw new CorruptedFrameException("negative pre-adjustment length field: " + frameLength); } frameLength += lengthAdjustment + lengthFieldEndOffset; if (frameLength < lengthFieldEndOffset) { inBuffer.skipBytes(lengthFieldEndOffset); throw new CorruptedFrameException("Adjusted frame length (" + frameLength + ") is less " + "than lengthFieldEndOffset: " + lengthFieldEndOffset); } if (frameLength > maxFrameLength) { // Enter the discard mode and discard everything received so far. discardingTooLongFrame = true; tooLongFrameLength = frameLength; bytesToDiscard = frameLength - inBuffer.readableBytes(); inBuffer.skipBytes(inBuffer.readableBytes()); failIfNecessary(ctx, true); return null; } // never overflows because it's less than maxFrameLength int frameLengthInt = (int) frameLength; if (inBuffer.readableBytes() < frameLengthInt) { return null; } if (initialBytesToStrip > frameLengthInt) { inBuffer.skipBytes(frameLengthInt); throw new CorruptedFrameException("Adjusted frame length (" + frameLength + ") is less " + "than initialBytesToStrip: " + initialBytesToStrip); } inBuffer.skipBytes(initialBytesToStrip); // extract frame int readerIndex = inBuffer.readerIndex(); int actualFrameLength = frameLengthInt - initialBytesToStrip; ByteBuf frame = extractFrame(inBuffer, readerIndex, actualFrameLength); inBuffer.readerIndex(readerIndex + actualFrameLength); return frame; }
From source file:com.cc.nettytest.proxy.encoder.CCLengthFieldPrepender.java
License:Apache License
@Override public void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception { int length = lengthIncludesLengthFieldLength ? msg.readableBytes() + lengthFieldLength : msg.readableBytes();// w ww . j a va 2 s. c o m switch (lengthFieldLength) { case 1: if (length >= 256) { throw new IllegalArgumentException("length does not fit into a byte: " + length); } out.writeByte((byte) length); break; case 2: if (length >= 65536) { throw new IllegalArgumentException("length does not fit into a short integer: " + length); } out.writeShort((short) length); break; case 3: if (length >= 16777216) { throw new IllegalArgumentException("length does not fit into a medium integer: " + length); } out.writeMedium(length); break; case 4: out.writeInt(ByteBufUtil.swapInt((int) length)); //SWAP FOR UIMANAGER break; case 8: out.writeLong(length); break; default: throw new Error("should not reach here"); } out.writeBytes(msg, msg.readerIndex(), msg.readableBytes()); }
From source file:com.chat.common.netty.handler.decode.LengthFieldBasedFrameDecoder.java
License:Apache License
/** * Create a frame out of the {@link ByteBuf} and return it. * * @param ctx the {@link ChannelHandlerContext} which this {@link ByteToMessageDecoder} belongs to * @param in the {@link ByteBuf} from which to read data * @return frame the {@link ByteBuf} which represent the frame or {@code null} if no frame could * be created./*from w ww . j a va 2s . co m*/ */ protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception { if (discardingTooLongFrame) { long bytesToDiscard = this.bytesToDiscard; int localBytesToDiscard = (int) Math.min(bytesToDiscard, in.readableBytes()); in.skipBytes(localBytesToDiscard); bytesToDiscard -= localBytesToDiscard; this.bytesToDiscard = bytesToDiscard; failIfNecessary(false); } if (in.readableBytes() < lengthFieldEndOffset) { return null; } int actualLengthFieldOffset = in.readerIndex() + lengthFieldOffset; long frameLength = getUnadjustedFrameLength(in, actualLengthFieldOffset, lengthFieldLength, byteOrder); if (frameLength < 0) { in.skipBytes(lengthFieldEndOffset); throw new CorruptedFrameException("negative pre-adjustment length field: " + frameLength); } frameLength += lengthAdjustment + lengthFieldEndOffset; if (frameLength < lengthFieldEndOffset) { in.skipBytes(lengthFieldEndOffset); throw new CorruptedFrameException("Adjusted frame length (" + frameLength + ") is less " + "than lengthFieldEndOffset: " + lengthFieldEndOffset); } if (frameLength > maxFrameLength) { long discard = frameLength - in.readableBytes(); tooLongFrameLength = frameLength; if (discard < 0) { // buffer contains more bytes then the frameLength so we can discard all now in.skipBytes((int) frameLength); } else { // Enter the discard mode and discard everything received so far. discardingTooLongFrame = true; bytesToDiscard = discard; in.skipBytes(in.readableBytes()); } failIfNecessary(true); return null; } // never overflows because it's less than maxFrameLength int frameLengthInt = (int) frameLength; if (in.readableBytes() < frameLengthInt) { return null; } if (initialBytesToStrip > frameLengthInt) { in.skipBytes(frameLengthInt); throw new CorruptedFrameException("Adjusted frame length (" + frameLength + ") is less " + "than initialBytesToStrip: " + initialBytesToStrip); } in.skipBytes(initialBytesToStrip); // extract frame int readerIndex = in.readerIndex(); int actualFrameLength = frameLengthInt - initialBytesToStrip; ByteBuf frame = extractFrame(ctx, in, readerIndex, actualFrameLength); in.readerIndex(readerIndex + actualFrameLength); return frame; }
From source file:com.chat.common.netty.handler.decode.ProtobufDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception { final byte[] array; final int offset; final int length = msg.readableBytes(); if (msg.hasArray()) { array = msg.array();/* w w w.ja v a2 s. co m*/ offset = msg.arrayOffset() + msg.readerIndex(); } else { array = new byte[length]; msg.getBytes(msg.readerIndex(), array, 0, length); offset = 0; } if (extensionRegistry == null) { if (HAS_PARSER) { out.add(prototype.getParserForType().parseFrom(array, offset, length)); } else { out.add(prototype.newBuilderForType().mergeFrom(array, offset, length).build()); } } else { if (HAS_PARSER) { out.add(prototype.getParserForType().parseFrom(array, offset, length, extensionRegistry)); } else { out.add(prototype.newBuilderForType().mergeFrom(array, offset, length, extensionRegistry).build()); } } }