List of usage examples for io.netty.buffer ByteBuf readUnsignedShort
public abstract int readUnsignedShort();
From source file:com.spotify.folsom.client.binary.BinaryMemcacheDecoder.java
License:Apache License
@Override protected void decode(final ChannelHandlerContext ctx, final ByteBuf buf, final List<Object> out) throws Exception { for (int i = 0; i < BATCH_SIZE; i++) { if (buf.readableBytes() < 24) { return; }/* www. j a v a 2s . c o m*/ buf.markReaderIndex(); final int magicNumber = buf.readUnsignedByte(); // byte 0 if (magicNumber != 0x81) { throw fail(buf, String.format("Invalid magic number: 0x%2x", magicNumber)); } final int opcode = buf.readByte(); // byte 1 final int keyLength = buf.readUnsignedShort(); // byte 2-3 final int extrasLength = buf.readUnsignedByte(); // byte 4 buf.skipBytes(1); final int statusCode = buf.readUnsignedShort(); // byte 6-7 final MemcacheStatus status = MemcacheStatus.fromInt(statusCode); final int totalLength = buf.readInt(); // byte 8-11 final int opaque = buf.readInt(); final long cas = buf.readLong(); if (buf.readableBytes() < totalLength) { buf.resetReaderIndex(); return; } buf.skipBytes(extrasLength); buf.skipBytes(keyLength); final int valueLength = totalLength - keyLength - extrasLength; byte[] valueBytes; if (valueLength == 0) { valueBytes = NO_BYTES; } else { valueBytes = new byte[valueLength]; } buf.readBytes(valueBytes); replies.add(new ResponsePacket((byte) opcode, status, opaque, cas, valueBytes)); if ((opaque & 0xFF) == 0) { out.add(replies); replies = new BinaryResponse(); } } }
From source file:com.spotify.heroic.consumer.collectd.CollectdParser.java
License:Apache License
public static Iterator<CollectdSample> parse(final ByteBuf frame) { if (frame.readableBytes() < 4) { throw new RuntimeException("frame to short"); }//w ww . j a v a 2 s . c o m frame.order(ByteOrder.BIG_ENDIAN); return new Iterator<CollectdSample>() { private Decoded decoded = new Decoded(); @Override public boolean hasNext() { return frame.readableBytes() > 0; } @Override public CollectdSample next() { while (true) { final int type = frame.readUnsignedShort(); final int size = frame.readUnsignedShort(); switch (type) { case HOST: decoded.host = parseString(frame, size); break; case TIME: decoded.time = frame.readLong(); break; case TIME_HR: decoded.time = (long) (((double) frame.readLong()) / FACTOR_HR); break; case PLUGIN: decoded.plugin = parseString(frame, size); break; case PLUGIN_INSTANCE: decoded.pluginInstance = parseString(frame, size); break; case TYPE: decoded.type = parseString(frame, size); break; case TYPE_INSTANCE: decoded.typeInstance = parseString(frame, size); break; case INTERVAL: decoded.interval = frame.readLong(); break; case INTERVAL_HR: decoded.interval = (long) (((double) frame.readLong()) / FACTOR_HR); break; case MESSAGE: decoded.message = parseString(frame, size); break; case SEVERITY: decoded.severity = frame.readLong(); break; case VALUES: return decoded.toSample(parseValues(frame, size)); default: log.warn("unhandled type: " + type); break; } } } }; }
From source file:com.spotify.heroic.consumer.collectd.CollectdParser.java
License:Apache License
public static List<CollectdValue> parseValues(final ByteBuf frame, final int size) { final int n = frame.readUnsignedShort(); final List<Integer> types = ImmutableList.copyOf(IntStream.range(0, n).map(i -> { return frame.readByte(); }).iterator());/*w w w . ja va2 s.c o m*/ final ImmutableList.Builder<CollectdValue> values = ImmutableList.builder(); for (final int type : types) { switch (type) { case CollectdSample.COUNTER: final long c = frame.readLong(); if (c < 0) { throw new IllegalArgumentException("value too large for signed type"); } values.add(new Counter(c)); break; case CollectdSample.GAUGE: frame.order(ByteOrder.LITTLE_ENDIAN); values.add(new CollectdValue.Gauge(frame.readDouble())); frame.order(ByteOrder.BIG_ENDIAN); break; case CollectdSample.DERIVE: values.add(new CollectdValue.Derive(frame.readLong())); break; case CollectdSample.ABSOLUTE: final long a = frame.readLong(); if (a < 0) { throw new IllegalArgumentException("value too large for signed type"); } values.add(new CollectdValue.Absolute(a)); break; default: throw new IllegalArgumentException("invalid sample type: " + type); } } return values.build(); }
From source file:com.streamsets.pipeline.lib.parser.net.netflow.NetflowCommonDecoder.java
License:Apache License
protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out, InetSocketAddress sender, InetSocketAddress recipient, boolean packetLengthCheck) throws OnRecordErrorException { int packetLength = buf.readableBytes(); if (!readVersion) { // 0-1, for both Netflow 5 and 9 version = buf.readUnsignedShort(); readVersion = true;/*from w ww . j a v a2s .co m*/ checkpoint(); } if (ctx != null) { if (sender == null) { SocketAddress socketAddress = ctx.channel().remoteAddress(); if (socketAddress instanceof InetSocketAddress) { sender = (InetSocketAddress) socketAddress; } } if (recipient == null) { SocketAddress socketAddress = ctx.channel().localAddress(); if (socketAddress instanceof InetSocketAddress) { recipient = (InetSocketAddress) socketAddress; } } } // lazy instantiation of the version specific decoder, but use the VersionSpecificNetflowDecoder<?> versionSpecificNetflowDecoder; switch (version) { case 5: if (netflowV5Decoder == null) { netflowV5Decoder = new NetflowV5Decoder(this); } versionSpecificNetflowDecoder = netflowV5Decoder; break; case 9: if (netflowV9Decoder == null) { // lazy instantiation of the version specific decoder netflowV9Decoder = new NetflowV9Decoder(this, outputValuesMode, templateCacheProvider); } versionSpecificNetflowDecoder = netflowV9Decoder; break; default: resetStateVariables(); throw new OnRecordErrorException(Errors.NETFLOW_00, version); } try { out.addAll(versionSpecificNetflowDecoder.parse(version, packetLength, packetLengthCheck, buf, sender, recipient)); // if the version specific decoder successfully parsed the buffer (i.e. we made it here), it means we have // completed an entire packet, and hence it is time to reset state resetStateVariables(); } catch (Exception e) { // ReplayingDecoder can call this multiple times by way of the special io.netty.util.Signal class, which // extends java.lang.Error // so on a Signal, we do NOT want to reset state, and hence a finally block will not suffice (since it will also // execute for unchecked exceptions like Signal resetStateVariables(); throw e; } }
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 w w . j ava2s . c o m*/ 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.netflow.v9.NetflowV9Decoder.java
License:Apache License
private int readUnsignedShortAndCheckpoint(ByteBuf buf) { final int val = buf.readUnsignedShort(); parentDecoder.doCheckpoint();//from w w w .ja v a 2s .com return val; }
From source file:com.tesora.dve.db.mysql.common.MysqlAPIUtils.java
License:Open Source License
public static long getLengthCodedLong(ByteBuf cb) { short byte1 = cb.readUnsignedByte(); if (byte1 == LEN_CODED_NULL) return 0; if (byte1 <= LEN_CODED_8_BITS) { return byte1; } else if (byte1 == LEN_CODED_16_BITS) { return cb.readUnsignedShort(); } else if (byte1 == LEN_CODED_24_BITS) return cb.readUnsignedMedium(); return cb.readLong(); }
From source file:com.tesora.dve.db.mysql.common.MysqlAPIUtils.java
License:Open Source License
/** * Methods to read and write length coded dates in the Binary Protocol * /*from w w w . ja v a 2 s .c om*/ * if year, month, day, hour, minutes, seconds and micro_seconds are all 0, * length is 0 and no other field is sent if hour, minutes, seconds and * micro_seconds are all 0, length is 4 and no other field is sent if * micro_seconds is 0, length is 7 and micro_seconds is not sent otherwise * length is 11 * * Fields * length (1) -- number of bytes following (valid values: 0, 4, 7, 11) * year (2) -- year * month (1) -- month * day (1) -- day * hour (1) -- hour * minute (1) -- minutes * second (1) -- seconds * micro_second (4) -- micro-seconds * */ public static Date getLengthCodedDate(ByteBuf cb) throws PEException { final Calendar cal = Calendar.getInstance(); cal.clear(); short length = cb.readUnsignedByte(); if (length >= 4) { cal.set(Calendar.YEAR, cb.readUnsignedShort()); cal.set(Calendar.MONTH, cb.readByte() - 1); // MONTH is zero based cal.set(Calendar.DAY_OF_MONTH, cb.readByte()); if (length >= 7) { cal.set(Calendar.HOUR_OF_DAY, cb.readByte()); cal.set(Calendar.MINUTE, cb.readByte()); cal.set(Calendar.SECOND, cb.readByte()); } if (length == 11) { long microSeconds = cb.readUnsignedInt(); cal.set(Calendar.MILLISECOND, (int) (microSeconds / 1000)); } if (length > 11) { throw new PEException("Invalid length specified to date type (" + length + ")"); } return cal.getTime(); } return null; }
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 av a 2 s .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); }