List of usage examples for io.netty.buffer ByteBuf readerIndex
public abstract int readerIndex();
From source file:com.heliosapm.ohcrs.impls.oracle.OracleDriverCodec.java
License:Apache License
/** * {@inheritDoc}/*w ww . j av a 2 s . c o m*/ * @see com.heliosapm.ohcrs.core.DriverCodec#write(java.lang.Object, com.heliosapm.ohcrs.core.DBType, io.netty.buffer.ByteBuf) */ @Override public int write(final Datum t, final DBType d, final ByteBuf b) throws SQLException { if (prefix(t, d, b)) { final byte[] bytes = t.getBytes(); b.writeInt(bytes.length); if (t instanceof oracle.sql.CHAR) { b.writeInt(((oracle.sql.CHAR) t).oracleId()); } b.writeBytes(bytes); } return b.readerIndex(); }
From source file:com.heliosapm.streams.onramp.GZipDetector.java
License:Apache License
/** * {@inheritDoc}/*from w w w . j a va 2 s. c om*/ * @see io.netty.handler.codec.ByteToMessageDecoder#decode(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List) */ @Override protected void decode(final ChannelHandlerContext ctx, final ByteBuf buff, final List<Object> out) throws Exception { if (buff.readableBytes() > 4) { final int magic1 = buff.getUnsignedByte(buff.readerIndex()); final int magic2 = buff.getUnsignedByte(buff.readerIndex() + 1); if (isGzip(magic1, magic2)) { enableGzip(ctx); } else { ctx.pipeline().remove(this); } out.add(buff.retain()); } }
From source file:com.heliosapm.tsdblite.handlers.ProtocolSwitch.java
License:Apache License
/** * {@inheritDoc}//from w w w. ja v a 2 s . co m * @see io.netty.handler.codec.ByteToMessageDecoder#decode(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List) */ @Override protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out) throws Exception { // Will use the first five bytes to detect a protocol. if (in.readableBytes() < 5) { log.info("No ProtocolSwitch. Bytes: {}", in.readableBytes()); return; } final int magic1 = in.getUnsignedByte(in.readerIndex()); final int magic2 = in.getUnsignedByte(in.readerIndex() + 1); if (detectGzip && isGzip(magic1, magic2)) { enableGzip(ctx); log.info("Enabled GZip on channel [{}]", ctx.channel().id().asShortText()); } else if (isHttp(magic1, magic2)) { switchToHttp(ctx); log.info("Switched to HTTP on channel [{}]", ctx.channel().id().asShortText()); } else if (isText(magic1, magic2)) { switchToPlainText(ctx); log.info("Switched to PlainText on channel [{}]", ctx.channel().id().asShortText()); } else { log.error("No protocol recognized on [{}]", ctx.channel().id().asLongText()); in.clear(); ctx.close(); } }
From source file:com.hzmsc.scada.Jmtis.server.PortUnificationServerHandler.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { System.out.println(in.getUnsignedByte(in.readerIndex()) + ".........................."); // Will use the first five bytes to detect a protocol. if (in.readableBytes() < 5) { return;/* w ww . j a v a2s . c om*/ } if (isSsl(in)) { enableSsl(ctx); } else { final int magic1 = in.getUnsignedByte(in.readerIndex()); final int magic2 = in.getUnsignedByte(in.readerIndex() + 1); if (isGzip(magic1, magic2)) { enableGzip(ctx); } else if (isHttp(magic1, magic2)) { switchToHttp(ctx); } else if (isJMAC(magic1, magic2)) { switchToJMAC(ctx); } else { // Unknown protocol; discard everything and close the connection. in.clear(); ctx.close(); } } }
From source file:com.ibasco.agql.core.utils.ByteBufUtils.java
License:Open Source License
public static String readString(ByteBuf buffer, Charset encoding, boolean readNonNullTerminated, String defaultString) {//from w w w.ja v a 2s . co m int length = buffer.bytesBefore((byte) 0); if (length < 0) { if (readNonNullTerminated && buffer.readableBytes() > 0) length = buffer.readableBytes(); else return null; } String data = buffer.readCharSequence(length, encoding).toString(); //Discard the null terminator (if available) if (buffer.readableBytes() > 2 && buffer.getByte(buffer.readerIndex()) == 0) buffer.readByte(); return data; }
From source file:com.ibasco.agql.protocols.valve.source.query.handlers.SourceRconPacketDecoder.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { final String separator = "================================================================================================="; //TODO: Move all code logic below to SourceRconPacketBuilder log.debug(separator);// w w w. j a v a2 s . c om log.debug(" ({}) DECODING INCOMING DATA : Bytes Received = {} {}", index.incrementAndGet(), in.readableBytes(), index.get() > 1 ? "[Continuation]" : ""); log.debug(separator); String desc = StringUtils.rightPad("Minimum allowable size?", PAD_SIZE); //Verify we have the minimum allowable size if (in.readableBytes() < 14) { log.debug(" [ ] {} = NO (Actual Readable Bytes: {})", desc, in.readableBytes()); return; } log.debug(" [x] {} = YES (Actual Readable Bytes: {})", desc, in.readableBytes()); //Reset if this happens to be not a valid source rcon packet in.markReaderIndex(); //Read and Verify size desc = StringUtils.rightPad("Bytes received at least => than the \"declared\" size?", PAD_SIZE); int size = in.readIntLE(); int readableBytes = in.readableBytes(); if (readableBytes < size) { log.debug(" [ ] {} = NO (Declared Size: {}, Actual Bytes Read: {})", desc, readableBytes, size); in.resetReaderIndex(); return; } log.debug(" [x] {} = YES (Declared Size: {}, Actual Bytes Read: {})", desc, readableBytes, size); //Read and verify request id desc = StringUtils.rightPad("Request Id within the valid range?", PAD_SIZE); int id = in.readIntLE(); if (!(id == -1 || id == SourceRconUtil.RCON_TERMINATOR_RID || SourceRconUtil.isValidRequestId(id))) { log.debug(" [ ] {} = NO (Actual: {})", desc, id); in.resetReaderIndex(); return; } log.debug(" [x] {} = YES (Actual: {})", desc, id); //Read and verify request type desc = StringUtils.rightPad("Valid response type?", PAD_SIZE); int type = in.readIntLE(); if (get(type) == null) { log.debug(" [ ] {} = NO (Actual: {})", desc, type); in.resetReaderIndex(); return; } log.debug(" [x] {} = YES (Actual: {} = {})", desc, type, SourceRconResponseType.get(type)); //Read and verify body desc = StringUtils.rightPad("Contains Body?", PAD_SIZE); int bodyLength = in.bytesBefore((byte) 0); String body = StringUtils.EMPTY; if (bodyLength <= 0) log.debug(" [ ] {} = NO", desc); else { body = in.readCharSequence(bodyLength, StandardCharsets.UTF_8).toString(); log.debug(" [x] {} = YES (Length: {}, Body: {})", desc, bodyLength, StringUtils.replaceAll(StringUtils.truncate(body, 30), "\n", "\\\\n")); } //Peek at the last two bytes and verify that they are null-bytes byte bodyTerminator = in.getByte(in.readerIndex()); byte packetTerminator = in.getByte(in.readerIndex() + 1); desc = StringUtils.rightPad("Contains TWO null-terminating bytes at the end?", PAD_SIZE); //Make sure the last two bytes are NULL bytes (request id: 999 is reserved for split packet responses) if ((bodyTerminator != 0 || packetTerminator != 0) && (id == SourceRconUtil.RCON_TERMINATOR_RID)) { log.debug("Skipping {} bytes", in.readableBytes()); in.skipBytes(in.readableBytes()); return; } else if (bodyTerminator != 0 || packetTerminator != 0) { log.debug(" [ ] {} = NO (Actual: Body Terminator = {}, Packet Terminator = {})", desc, bodyTerminator, packetTerminator); in.resetReaderIndex(); return; } else { log.debug(" [x] {} = YES (Actual: Body Terminator = {}, Packet Terminator = {})", desc, bodyTerminator, packetTerminator); //All is good, skip the last two bytes if (in.readableBytes() >= 2) in.skipBytes(2); } //At this point, we can now construct a packet log.debug(" [x] Status: PASS (Size = {}, Id = {}, Type = {}, Remaining Bytes = {}, Body Size = {})", size, id, type, in.readableBytes(), bodyLength); log.debug(separator); //Reset the index index.set(0); //Construct the response packet and send to the next handlers SourceRconResponsePacket responsePacket; //Did we receive a terminator packet? if (this.terminatingPacketsEnabled && id == SourceRconUtil.RCON_TERMINATOR_RID && StringUtils.isBlank(body)) { responsePacket = new SourceRconTermResponsePacket(); } else { responsePacket = SourceRconPacketBuilder.getResponsePacket(type); } if (responsePacket != null) { responsePacket.setSize(size); responsePacket.setId(id); responsePacket.setType(type); responsePacket.setBody(body); log.debug( "Decode Complete. Passing response for request id : '{}' to the next handler. Remaining bytes ({})", id, in.readableBytes()); out.add(responsePacket); } }
From source file:com.ibm.crail.datanode.netty.CrailNettyUtils.java
License:Apache License
public static void showByteBufContent(ByteBuf buf, int offset, int bytes) { /* this dump the content of first bytes from the payload */ if (buf != null) { int ori_rindex = buf.readerIndex(); LOG.info("DUMP: TID:" + Thread.currentThread().getId() + " NettyByteBuf : " + buf); int min = (buf.capacity() - offset); if (min > bytes) min = bytes;/*from ww w. ja va2 s . com*/ String str = "DUMP: TID:" + Thread.currentThread().getId() + " DUMP (" + offset + " ,+" + min + ") : "; //for loop update it to the end limit min += offset; for (int i = offset; i < min; i++) { //str += Character.toHexString(); str += Byte.toString(buf.getByte(i)) + " : "; if (i % 32 == 0) str += "\n"; } LOG.info(str); buf.readerIndex(ori_rindex); } else { LOG.info("DUMP : payload content is NULL"); } }
From source file:com.jamierf.jsonrpc.util.JsonObjectDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { if (state == ST_CORRUPTED) { in.skipBytes(in.readableBytes()); return;/*from ww w .j av a 2s . c o m*/ } // index of next byte to process. int idx = this.idx; int wrtIdx = in.writerIndex(); if (wrtIdx > maxObjectLength) { // buffer size exceeded maxObjectLength; discarding the complete buffer. in.skipBytes(in.readableBytes()); reset(); throw new TooLongFrameException( "object length exceeds " + maxObjectLength + ": " + wrtIdx + " bytes discarded"); } for (/* use current idx */; idx < wrtIdx; idx++) { byte c = in.getByte(idx); if (state == ST_DECODING_NORMAL) { decodeByte(c, in, idx); // All opening braces/brackets have been closed. That's enough to conclude // that the JSON object/array is complete. if (openBraces == 0) { ByteBuf json = extractObject(ctx, in, in.readerIndex(), idx + 1 - in.readerIndex()); if (json != null) { out.add(json); } // The JSON object/array was extracted => discard the bytes from // the input buffer. in.readerIndex(idx + 1); // Reset the object state to get ready for the next JSON object/text // coming along the byte stream. reset(); } } else if (state == ST_DECODING_ARRAY_STREAM) { decodeByte(c, in, idx); if (!insideString && (openBraces == 1 && c == ',' || openBraces == 0 && c == ']')) { // skip leading spaces. No range check is needed and the loop will terminate // because the byte at position idx is not a whitespace. for (int i = in.readerIndex(); Character.isWhitespace(in.getByte(i)); i++) { in.skipBytes(1); } // skip trailing spaces. int idxNoSpaces = idx - 1; while (idxNoSpaces >= in.readerIndex() && Character.isWhitespace(in.getByte(idxNoSpaces))) { idxNoSpaces--; } ByteBuf json = extractObject(ctx, in, in.readerIndex(), idxNoSpaces + 1 - in.readerIndex()); if (json != null) { out.add(json); } in.readerIndex(idx + 1); if (c == ']') { reset(); } } // JSON object/array detected. Accumulate bytes until all braces/brackets are closed. } else if (c == '{' || c == '[') { initDecoding(c); if (state == ST_DECODING_ARRAY_STREAM) { // Discard the array bracket in.skipBytes(1); } // Discard leading spaces in front of a JSON object/array. } else if (Character.isWhitespace(c)) { in.skipBytes(1); } else { state = ST_CORRUPTED; throw new CorruptedFrameException( "invalid JSON received at byte position " + idx + ": " + ByteBufUtil.hexDump(in)); } } if (in.readableBytes() == 0) { this.idx = 0; } else { this.idx = idx; } }
From source file:com.kael.surf.net.codec.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 . ja v a 2 s . c o m*/ */ protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception { Integer packetNum = ctx.attr(countKey).get(); if (packetNum == null || packetNum <= 100) { ctx.attr(countKey).set(packetNum == null ? 1 : packetNum + 1); int readableBytes = in.readableBytes(); if (skipPolicy && readableBytes >= lengthFieldEndOffset) { if (readableBytes >= POLICY_STR_BYTES.length) { byte[] dst = new byte[POLICY_STR_BYTES.length]; in.getBytes(in.readerIndex(), dst); if (isEqual(dst, POLICY_STR_BYTES)) { in.skipBytes(POLICY_STR_BYTES.length); return new String(dst); } } else { byte[] dst = new byte[readableBytes]; in.getBytes(in.readerIndex(), dst); if (isEqual(dst, POLICY_STR_BYTES)) { return null; } } } if (skipTencentGTWHead && readableBytes >= lengthFieldEndOffset) { if (readableBytes >= GET_BYTES.length) { byte[] dst = new byte[readableBytes]; in.getBytes(in.readerIndex(), dst); if (isBeginWith(dst, GET_BYTES)) { int endIndex = findEndIndex(dst, CRLF_BYTES); if (endIndex == -1) { return null; } in.skipBytes(endIndex); } } else { byte[] dst = new byte[readableBytes]; in.getBytes(in.readerIndex(), dst); if (isEqual(dst, GET_BYTES)) { return null; } } } } 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.l2jmobius.gameserver.network.client.Crypt.java
License:Open Source License
@Override public void encrypt(ByteBuf buf) { if (!_isEnabled) { _isEnabled = true;//from www .j ava2 s . c o m onPacketSent(buf); return; } onPacketSent(buf); int a = 0; while (buf.isReadable()) { final int b = buf.readByte() & 0xFF; a = b ^ _outKey[(buf.readerIndex() - 1) & 15] ^ a; buf.setByte(buf.readerIndex() - 1, a); } shiftKey(_outKey, buf.writerIndex()); }