List of usage examples for io.netty.buffer ByteBuf readUnsignedByte
public abstract short readUnsignedByte();
From source file:com.spotify.folsom.client.ascii.AsciiMemcacheDecoder.java
License:Apache License
private StringBuilder readLine(final ByteBuf buf, final int available) throws IOException { if (consumed) { line.setLength(0);/*from w w w.ja va 2 s . co m*/ consumed = false; } for (int i = 0; i < available - 1; i++) { final char b = (char) buf.readUnsignedByte(); if (b == '\r') { if (buf.readUnsignedByte() == '\n') { consumed = true; return line; } throw new IOException("Expected newline, got something else"); } line.append(b); } return null; }
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; }// ww w. j a v a 2 s. 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.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 www . j a v a2s . 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.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
static boolean isLengthCodedLongNull(ByteBuf cb) { boolean isNull = (cb.getUnsignedByte(cb.readerIndex()) == LEN_CODED_NULL); if (isNull)/*from w ww . ja v a 2s . co m*/ cb.readUnsignedByte(); return isNull; }
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 .j a v a2 s .c o m*/ * 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.common.MysqlAPIUtils.java
License:Open Source License
/** * Methods to read and write length coded times in the Binary Protocol * /* ww w.j av a2 s .c o 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.mysqlapi.repl.messages.MyFormatDescriptionLogEvent.java
License:Open Source License
@Override public void unmarshallMessage(ByteBuf cb) { binaryLogVersion = cb.readShort();//from w w w . j a v a 2s . c o m serverVersion = MysqlAPIUtils.readBytesAsString(cb, ST_SERVER_VER_LEN, CharsetUtil.UTF_8); createTime = cb.readUnsignedInt(); eventTypeLength = cb.readUnsignedByte(); int numberOfEntries = cb.readableBytes() - 1;//string is null terminated. switch (MyBinLogVerType.fromByte((byte) binaryLogVersion)) { case MySQL_5_0: for (int i = 1; i <= numberOfEntries; i++) { eventTypeValues.put(MyLogEventType.fromByte((byte) i), cb.readByte()); } cb.skipBytes(1);//throw out EOF break; default: // TODO throw???? logger.error("Cannot process binary log messages that are not for MySQL 5.0"); } }
From source file:com.tesora.dve.mysqlapi.repl.messages.MyUserVarLogEvent.java
License:Open Source License
long readValue(ByteBuf decimalPortionBuf, int valueLen) throws PEException { if (valueLen < 1 || valueLen > 4) throw new PEException("Cannot decode decimal buffer. Invalid read length of " + valueLen); long value = 0; if (valueLen == 4) { value = decimalPortionBuf.readUnsignedInt(); } else if (valueLen == 3) { value = decimalPortionBuf.readUnsignedMedium(); } else if (valueLen == 2) { value = decimalPortionBuf.readUnsignedShort(); } else if (valueLen == 1) { value = decimalPortionBuf.readUnsignedByte(); }/*from w w w .j a va 2 s . c om*/ return value; }
From source file:com.trinity.engine.protocol.detail.ConnectionReplayingDecoder.java
License:Apache License
/** * {@inheritDoc}//from w w w .ja v a 2s . co m */ @Override protected void decode(ChannelHandlerContext context, ByteBuf input, List<Object> output) throws Exception { switch (state()) { case READ_ID: mMessageId = input.readUnsignedByte(); checkpoint(DecoderState.READ_LENGTH); break; case READ_LENGTH: mMessageLength = input.readUnsignedShort(); checkpoint(DecoderState.READ_CONTENT); break; case READ_CONTENT: output.add(mLookupService.decode(mMessageId, input.readBytes(mMessageLength))); checkpoint(DecoderState.READ_ID); } }