List of usage examples for io.netty.buffer ByteBuf skipBytes
public abstract ByteBuf skipBytes(int length);
From source file:com.streamsets.pipeline.lib.parser.net.DelimitedLengthFieldBasedFrameDecoder.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 ww w .j av a 2s . com */ 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); return null; } if (consumingLength) { int delimIndex = indexOf(in, delimiter); if (delimIndex < 0) { return null; } final String lengthStr = in.toString(in.readerIndex(), delimIndex, lengthFieldCharset); try { frameLength = Long.parseLong(trimLengthString ? lengthStr.trim() : lengthStr); } catch (NumberFormatException e) { throw new CorruptedFrameException(String.format("Invalid length field decoded (in %s charset): %s", lengthFieldCharset.name(), lengthStr), e); } if (frameLength < 0) { throw new CorruptedFrameException("negative pre-adjustment length field: " + frameLength); } frameLength += lengthAdjustment; //consume length field and delimiter bytes in.skipBytes(delimIndex + delimiter.capacity()); //consume delimiter bytes consumingLength = false; } 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; consumingLength = 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) { // need more bytes available to read actual frame return null; } // the frame is now entirely present, reset state vars consumingLength = true; frameLength = 0; // 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.streamsets.pipeline.lib.parser.net.netflow.v5.NetflowV5Decoder.java
License:Apache License
@Override public List<NetflowV5Message> parse(int netflowVersion, int packetLength, boolean packetLengthCheck, ByteBuf buf, InetSocketAddress sender, InetSocketAddress recipient) throws OnRecordErrorException { if (!readHeader) { // 2-3//from w ww .ja v a 2s . c om count = buf.readUnsignedShort(); if (count <= 0) { throw new OnRecordErrorException(Errors.NETFLOW_01, Utils.format("Count is invalid: {}", count)); } else if (packetLengthCheck && packetLength < V5_HEADER_SIZE + count * V5_FLOW_SIZE) { String msg = Utils.format("Readable bytes {} too small for count {} (max {})", packetLength, count, (V5_HEADER_SIZE + count * V5_FLOW_SIZE)); throw new OnRecordErrorException(Errors.NETFLOW_01, msg); } readerId = String.valueOf(recipient); // 4-7 uptime = buf.readUnsignedInt(); // 8-11 seconds = buf.readUnsignedInt(); // 12-15 nanos = buf.readUnsignedInt(); millis = nanos / 1000000; // java timestamp, which is milliseconds timestamp = (seconds * 1000L) + millis; packetId = UUIDs.startOfJavaTimestamp(timestamp); // 16-19 flowSequence = buf.readUnsignedInt(); // 20 engineType = buf.readUnsignedByte(); // 21 engineId = buf.readUnsignedByte(); // the first 2 bits are the sampling mode, the remaining 14 the interval // 22-23 sampling = buf.readUnsignedShort(); samplingInterval = sampling & 0x3FFF; samplingMode = sampling >> 14; readHeader = true; parentDecoder.doCheckpoint(); } while (readIndex < count) { //ByteBuf flowBuf = buf.slice(V5_HEADER_SIZE + (readIndex * V5_FLOW_SIZE), V5_FLOW_SIZE); NetflowV5Message msg = new NetflowV5Message(); msg.setSeconds(seconds); msg.setNanos(nanos); msg.setCount(count); // 0 int srcaddr = (int) buf.readUnsignedInt(); // 4 int dstaddr = (int) buf.readUnsignedInt(); // 8 int nexthop = (int) buf.readUnsignedInt(); // 12 msg.setSnmpInput(buf.readUnsignedShort()); // 14 msg.setSnmpOnput(buf.readUnsignedShort()); msg.setPacketId(packetId.toString()); msg.setSender((sender == null) ? "unknown" : sender.getAddress().toString()); msg.setLength(packetLength); msg.setUptime(uptime); msg.setTimestamp(timestamp); msg.setFlowSequence(flowSequence); msg.setEngineId(engineId); msg.setEngineType(engineType); msg.setRawSampling(sampling); msg.setSamplingInterval(samplingInterval); msg.setSamplingMode(samplingMode); msg.setReaderId(readerId); // 16 long packets = buf.readUnsignedInt(); // 20 long octets = buf.readUnsignedInt(); // 24 long first = buf.readUnsignedInt(); msg.setRawFirst(first); if (first > 0) { msg.setFirst(timestamp - uptime + first); } else { msg.setFirst(0L); } // 28 long last = buf.readUnsignedInt(); msg.setRawLast(last); if (last > 0) { msg.setLast(timestamp - uptime + last); } else { msg.setLast(0L); } msg.setId(UUIDs.timeBased().toString()); msg.setSrcAddr(srcaddr); msg.setDstAddr(dstaddr); msg.setNextHop(nexthop); msg.setSrcAddrString(NetflowCommonDecoder.ipV4ToString(srcaddr)); msg.setDstAddrString(NetflowCommonDecoder.ipV4ToString(dstaddr)); msg.setNexthopString(NetflowCommonDecoder.ipV4ToString(nexthop)); // 32 msg.setSrcPort(buf.readUnsignedShort()); // 34 msg.setDstPort(buf.readUnsignedShort()); // 36 is "pad1" (unused zero bytes) buf.readByte(); // 37 msg.setTcpFlags(buf.readUnsignedByte()); // 38 msg.setProto(buf.readUnsignedByte()); // 39 msg.setTos(buf.readUnsignedByte()); // 40 msg.setSrcAs(buf.readUnsignedShort()); // 42 msg.setDstAs(buf.readUnsignedShort()); // 44 msg.setSrcMask(buf.readUnsignedByte()); // 45 msg.setDstMask(buf.readUnsignedByte()); msg.setdPkts(packets); msg.setdOctets(octets); // 46-47 is "pad2" (unused zero bytes) buf.skipBytes(2); result.add(msg); readIndex++; parentDecoder.doCheckpoint(); } // if we reached this point without any further Signal errors, we have finished consuming //checkpoint(); LinkedList<NetflowV5Message> returnResults = new LinkedList<>(result); resetState(); return returnResults; }
From source file:com.streamsets.pipeline.lib.parser.net.syslog.SyslogDecoder.java
License:Apache License
public void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out, InetSocketAddress recipient, InetSocketAddress sender) throws OnRecordErrorException { if (ctx != null) { if (sender == null) { SocketAddress socketAddress = ctx.channel().remoteAddress(); if (socketAddress instanceof InetSocketAddress) { sender = (InetSocketAddress) socketAddress; }//from ww w . j a va 2 s . co m } if (recipient == null) { SocketAddress socketAddress = ctx.channel().localAddress(); if (socketAddress instanceof InetSocketAddress) { recipient = (InetSocketAddress) socketAddress; } } } final SyslogMessage syslogMsg = new SyslogMessage(); if (sender != null) { String senderHost = resolveHostAddressString(sender); syslogMsg.setSenderHost(senderHost); syslogMsg.setSenderAddress(resolveAddressString(senderHost, sender)); syslogMsg.setSenderPort(sender.getPort()); } final String msg = buf.toString(charset); int msgLen = msg.length(); int curPos = 0; syslogMsg.setRawMessage(msg); if (msg.charAt(curPos) != '<') { throw new OnRecordErrorException(Errors.SYSLOG_01, "cannot find open bracket '<'", msg); } int endBracketPos = msg.indexOf('>'); if (endBracketPos <= 0 || endBracketPos > 6) { throw new OnRecordErrorException(Errors.SYSLOG_01, "cannot find end bracket '>'", msg); } String priority = msg.substring(1, endBracketPos); int pri; try { pri = Integer.parseInt(priority); } catch (NumberFormatException nfe) { throw new OnRecordErrorException(Errors.SYSLOG_01, nfe, msg, nfe); } int facility = pri / 8; int severity = pri % 8; // Remember priority syslogMsg.setPriority(pri); // put fac / sev into header syslogMsg.setFacility(facility); syslogMsg.setSeverity(severity); if (msgLen <= endBracketPos + 1) { throw new OnRecordErrorException(Errors.SYSLOG_02, msg); } // update parsing position curPos = endBracketPos + 1; // remember version string if (msgLen > curPos + 2 && "1 ".equals(msg.substring(curPos, curPos + 2))) { // this is curious, I guess the code above matches 1 exactly because // there has not been another version. syslogMsg.setSyslogVersion(1); curPos += 2; } // now parse timestamp (handle different varieties) long ts; String tsString; char dateStartChar = msg.charAt(curPos); while (dateStartChar == ' ' && curPos < msgLen - 1) { // consume any spaces immediately after PRI dateStartChar = msg.charAt(++curPos); } // no timestamp specified; use relay current time if (dateStartChar == '-') { ts = clock.millis(); if (msgLen <= curPos + 2) { throw new OnRecordErrorException(Errors.SYSLOG_03, msg); } curPos += 2; // assume we skip past a space to get to the hostname // rfc3164 timestamp } else if (dateStartChar >= 'A' && dateStartChar <= 'Z') { if (msgLen <= curPos + RFC3164_LEN) { throw new OnRecordErrorException(Errors.SYSLOG_04, msg); } tsString = msg.substring(curPos, curPos + RFC3164_LEN); ts = parseRfc3164Time(tsString); curPos += RFC3164_LEN + 1; // rfc 5424 timestamp } else { int nextSpace = msg.indexOf(' ', curPos); if (nextSpace == -1) { throw new OnRecordErrorException(Errors.SYSLOG_04, msg); } tsString = msg.substring(curPos, nextSpace); ts = parseRfc5424Date(tsString); curPos = nextSpace + 1; } syslogMsg.setTimestamp(ts); // parse out hostname int nextSpace = msg.indexOf(' ', curPos); if (nextSpace == -1) { throw new OnRecordErrorException(Errors.SYSLOG_03, msg); } String host = msg.substring(curPos, nextSpace); syslogMsg.setHost(host); if (msgLen > nextSpace + 1) { curPos = nextSpace + 1; syslogMsg.setRemainingMessage(msg.substring(curPos)); } else { syslogMsg.setRemainingMessage(""); } if (recipient != null) { String receiverHost = resolveHostAddressString(recipient); syslogMsg.setReceiverHost(receiverHost); syslogMsg.setReceiverAddress(resolveAddressString(receiverHost, recipient)); syslogMsg.setReceiverPort(recipient.getPort()); } out.add(syslogMsg); // consume the buffer that was just read (as an entire String) buf.skipBytes(buf.readableBytes()); }
From source file:com.tesora.dve.db.mysql.common.MysqlAPIUtils.java
License:Open Source License
public static ArrayList<Integer> locateLengthCodedStrings(ByteBuf cb, int skipAmount) { ArrayList<Integer> offsets = new ArrayList<>(); ByteBuf slice = cb.slice().order(ByteOrder.LITTLE_ENDIAN); slice.skipBytes(skipAmount); while (slice.isReadable()) { offsets.add(slice.readerIndex()); skipLengthCodedString(slice);//from w w w . j a va 2 s. c om } return offsets; }
From source file:com.tesora.dve.db.mysql.common.MysqlAPIUtils.java
License:Open Source License
public static void skipLengthCodedString(ByteBuf cb) { cb.skipBytes((int) getLengthCodedLong(cb)); }
From source file:com.tesora.dve.db.mysql.common.MysqlAPIUtils.java
License:Open Source License
/** * Methods to read and write length coded times in the Binary Protocol * // w w w.ja v a 2s . co m * if days, hours, minutes, seconds and micro_seconds are all 0, length is 0 * and no other field is sent * * if micro_seconds is 0, length is 8 and micro_seconds is not sent * otherwise length is 12 * * Fields * length (1) -- number of bytes following (valid values: 0, 8, 12) * is_negative (1) -- (1 if minus, 0 for plus) * days (4) -- days * hours (1) -- hours * minutes (1) -- minutes * seconds (1) -- seconds * micro_seconds (4) -- micro-seconds */ public static Time getLengthCodedTime(ByteBuf cb) throws PEException { Calendar cal = Calendar.getInstance(); cal.clear(); short length = cb.readUnsignedByte(); if (length == 0) return null; Time ret = null; if (length >= 8) { cb.skipBytes(1); // this is the sign - we are ignoring this for now cb.skipBytes(4); // this is "days" - we are ignoring this for now cal.set(Calendar.HOUR_OF_DAY, cb.readByte()); cal.set(Calendar.MINUTE, cb.readByte()); cal.set(Calendar.SECOND, cb.readByte()); if (length == 12) { long microSeconds = cb.readUnsignedInt(); cal.set(Calendar.MILLISECOND, (int) (microSeconds / 1000)); } if (length > 12) { throw new PEException("Invalid length specified to date type (" + length + ")"); } ret = new Time(cal.getTimeInMillis()); } return ret; }
From source file:com.tesora.dve.db.mysql.libmy.MyBinaryResultRow.java
License:Open Source License
@Override public void unmarshallMessage(ByteBuf cb) throws PEException { int expectedFieldCount = fieldConverters.size(); int expectedBitmapLength = MyNullBitmap.computeSize(expectedFieldCount, MyNullBitmap.BitmapType.RESULT_ROW); cb = cb.order(ByteOrder.LITTLE_ENDIAN); cb.skipBytes(1);//skip the bin row marker. byte[] nullBitmap = new byte[expectedBitmapLength]; cb.readBytes(nullBitmap);/*from ww w . j a va 2s . c o m*/ MyNullBitmap resultBitmap = new MyNullBitmap(nullBitmap, expectedFieldCount, MyNullBitmap.BitmapType.RESULT_ROW); ByteBuf values = cb; for (int i = 0; i < expectedFieldCount; i++) { ByteBuf existing = fieldSlices.get(i); ByteBuf nextSlice = null; int startIndex = values.readerIndex(); if (!resultBitmap.getBit(i + 1)) { fieldConverters.get(i).readObject(values);//TODO: we throw out the unmarshalled value, we could cache it. int endingOffset = values.readerIndex(); nextSlice = values.slice(startIndex, endingOffset - startIndex); } if (existing != null) existing.release(); fieldSlices.set(i, nextSlice); } if (cb.readableBytes() > 0) { log.warn("Decoded binary row had {} leftover bytes, re-encoding may fail.", cb.readableBytes()); cb.skipBytes(cb.readableBytes());//consume rest of buffer. } }
From source file:com.tesora.dve.db.mysql.libmy.MyErrorResponse.java
License:Open Source License
@Override public void unmarshallMessage(ByteBuf cb) { cb.skipBytes(1); // error header errorNumber = cb.readUnsignedShort(); cb.skipBytes(1);//from w w w.j a va 2s.c o m sqlState = MysqlAPIUtils.readBytesAsString(cb, 5, CharsetUtil.UTF_8); errorMsg = MysqlAPIUtils.readBytesAsString(cb, CharsetUtil.UTF_8); }
From source file:com.tesora.dve.db.mysql.libmy.MyErrorResponse.java
License:Open Source License
public static PESQLStateException asException(ByteBuf in) { ByteBuf cb = in.order(ByteOrder.LITTLE_ENDIAN); cb.skipBytes(1); // error header int errorNumber = cb.readUnsignedShort(); cb.skipBytes(1); // sqlState header String sqlState = MysqlAPIUtils.readBytesAsString(cb, 5, CharsetUtil.UTF_8); String errorMsg = MysqlAPIUtils.readBytesAsString(cb, CharsetUtil.UTF_8); return new PESQLStateException(errorNumber, sqlState, errorMsg); }
From source file:com.tesora.dve.db.mysql.libmy.MyFieldPktResponse.java
License:Open Source License
private void unpackTypeInfo(ByteBuf cb) { int remainingPayload = cb.readableBytes(); int charsetOffset = remainingPayload + CHARSET_OFFSET_RELATIVE_TO_EOF; cb.skipBytes(charsetOffset); charset = cb.readByte();/* ww w . ja v a 2 s. c om*/ cb.skipBytes(1); column_length = cb.readInt();//TODO: this should be an unsigned int. Will cause problems if length exceeds Integer.MAX_VALUE. column_type = MyFieldType.fromByte(cb.readByte()); flags = cb.readShort(); scale = cb.readByte(); //plus two pad bytes of zero }