List of usage examples for io.netty.buffer ByteBuf readSlice
public abstract ByteBuf readSlice(int length);
From source file:org.opendaylight.protocol.rsvp.parser.spi.subobjects.XROSubobjectListParser.java
License:Open Source License
public List<SubobjectContainer> parseList(final ByteBuf byteBuf) throws RSVPParsingException { final List<SubobjectContainer> subs = new ArrayList<>(); while (byteBuf.isReadable()) { final boolean mandatory = ((byteBuf.getUnsignedByte(byteBuf.readerIndex()) & (1 << Values.FIRST_BIT_OFFSET)) != 0) ? true : false; final int type = UnsignedBytes.checkedCast( (byteBuf.readUnsignedByte() & Values.BYTE_MAX_VALUE_BYTES) & ~(1 << Values.FIRST_BIT_OFFSET)); final int length = byteBuf.readUnsignedByte() - HEADER_LENGHT; if (length > byteBuf.readableBytes()) { throw new RSVPParsingException( "Wrong length specified. Passed: " + length + "; Expected: <= " + byteBuf.readableBytes()); }/*from w w w . j ava 2s . c o m*/ LOG.debug("Attempt to parse subobject from bytes: {}", ByteBufUtil.hexDump(byteBuf)); final SubobjectContainer sub = this.subobjReg.parseSubobject(type, byteBuf.readSlice(length), mandatory); if (sub == null) { LOG.warn("Unknown subobject type: {}. Ignoring subobject.", type); } else { LOG.debug("Subobject was parsed. {}", sub); subs.add(sub); } } return subs; }
From source file:org.redisson.client.handler.CommandDecoder.java
License:Apache License
public ByteBuf readBytes(ByteBuf is) throws IOException { long l = readLong(is); if (l > Integer.MAX_VALUE) { throw new IllegalArgumentException("Java only supports arrays up to " + Integer.MAX_VALUE + " in size"); }//from ww w . j av a 2s. co m int size = (int) l; if (size == -1) { return null; } ByteBuf buffer = is.readSlice(size); int cr = is.readByte(); int lf = is.readByte(); if (cr != CR || lf != LF) { throw new IOException("Improper line ending: " + cr + ", " + lf); } return buffer; }
From source file:org.redisson.codec.MapCacheEventCodec.java
License:Apache License
private Object decode(ByteBuf buf, State state, Decoder<?> decoder) throws IOException { int keyLen;/* ww w.java 2s . co m*/ if (osType == OSType.WINDOWS) { keyLen = buf.readIntLE(); } else if (osType == OSType.HPNONSTOP) { keyLen = (int) buf.readLong(); } else { keyLen = (int) buf.readLongLE(); } ByteBuf keyBuf = buf.readSlice(keyLen); Object key = decoder.decode(keyBuf, state); return key; }
From source file:org.springframework.boot.context.embedded.netty.HttpContentInputStream.java
License:Apache License
private ByteBuf readContent(int length) throws IOException { ByteBuf content = current.content(); if (length < content.readableBytes()) { return content.readSlice(length); } else {/* w w w. j a v a 2 s . c o m*/ return content; } }
From source file:org.starnub.starnubserver.servers.starbound.TCPProxyServerPacketDecoder.java
License:Open Source License
@SuppressWarnings("unchecked") @Override// w w w . j a va 2s . c o m public void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { switch (state()) { case READ_PACKET_ID: { packetIDIndex = in.readerIndex(); byte packetId = in.getByte(packetIDIndex); packet = PACKET_POOL.get(packetId); checkpoint(DecoderState.READ_VLQ); } case READ_VLQ: { clearVLQ(); int tempIndexMarker = packetIDIndex + 1; while (vlqLength <= 10) { int tmpByte = in.getByte(tempIndexMarker); payloadLength = (payloadLength << 7) | (tmpByte & 0x7f); vlqLength++; if ((tmpByte & 0x80) == 0) { break; } tempIndexMarker++; } if ((payloadLength & 1) == 0x00) { payloadLength = payloadLength >> 1; } else { payloadLength = -((payloadLength >> 1) + 1); } compressed = payloadLength < 0; if (compressed) { payloadLength = -payloadLength; } checkpoint(DecoderState.READ_PAYLOAD); } case READ_PAYLOAD: { if (packet != null) { CopyOnWriteArrayList<EventSubscription> hashSet = PACKET_EVENT_ROUTER.getEVENT_SUBSCRIPTION_MAP() .get(packet.getClass()); /* Handle Packet if there is an events handler for it, else do not create objects */ if (hashSet != null) { in.skipBytes(1 + vlqLength); if (compressed) { packet.read(Unpooled.wrappedBuffer(1, decompress(in.readBytes(payloadLength).array()))); } else { packet.read(in.readBytes(payloadLength)); } for (EventSubscription<Packet> packetEventSubscription : hashSet) { if (packet.isRecycle()) { break; } try { packetEventSubscription.getEVENT_HANDLER().onEvent(packet); } catch (Exception e) { e.printStackTrace(); } } /* Write packet out, if not recycling */ if (!packet.isRecycle()) { packet.routeToDestination(); } else { packet.resetRecycle(); } } else { destinationCTX.writeAndFlush(in.readSlice(1 + vlqLength + payloadLength).retain(), destinationCTX.voidPromise()); } } else { destinationCTX.writeAndFlush(in.readSlice(1 + vlqLength + payloadLength).retain(), destinationCTX.voidPromise()); } checkpoint(DecoderState.READ_PACKET_ID); break; } default: throw new Error("Error Decoding - Reached the unreachable void."); } }
From source file:org.traccar.protocol.AdmProtocolDecoder.java
License:Apache License
private Position parseCommandResponse(Channel channel, SocketAddress remoteAddress, ByteBuf buf) { DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); if (deviceSession == null) { return null; }/*from www . j av a 2s . c o m*/ Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); getLastLocation(position, null); int responseTextLength = buf.bytesBefore((byte) 0); if (responseTextLength < 0) { responseTextLength = CMD_RESPONSE_SIZE - 3; } position.set(Position.KEY_RESULT, buf.readSlice(responseTextLength).toString(StandardCharsets.UTF_8)); return position; }
From source file:org.traccar.protocol.AdmProtocolDecoder.java
License:Apache License
@Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg; buf.readUnsignedShortLE(); // device id int size = buf.readUnsignedByte(); if (size != CMD_RESPONSE_SIZE) { int type = buf.readUnsignedByte(); if (type == MSG_IMEI) { getDeviceSession(channel, remoteAddress, buf.readSlice(15).toString(StandardCharsets.UTF_8)); } else {//from ww w . ja va2 s .co m return decodeData(channel, remoteAddress, buf, type); } } else { return parseCommandResponse(channel, remoteAddress, buf); } return null; }
From source file:org.traccar.protocol.AnytrekProtocolDecoder.java
License:Apache License
@Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg; buf.skipBytes(2); // header buf.readUnsignedShortLE(); // size int type = buf.readUnsignedByte(); String imei = ByteBufUtil.hexDump(buf.readSlice(8)).substring(2); DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei); if (deviceSession == null) { return null; }//from w w w . java2 s. co m Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); position.set(Position.KEY_VERSION_FW, buf.readUnsignedShortLE()); position.set(Position.KEY_BATTERY, buf.readUnsignedShortLE() * 0.01); position.set(Position.KEY_RSSI, buf.readUnsignedByte()); DateBuilder dateBuilder = new DateBuilder() .setDate(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte()) .setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte()); position.setTime(dateBuilder.getDate()); position.set(Position.KEY_SATELLITES, BitUtil.to(buf.readUnsignedByte(), 4)); double latitude = buf.readUnsignedIntLE() / 1800000.0; double longitude = buf.readUnsignedIntLE() / 1800000.0; position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte())); int flags = buf.readUnsignedShortLE(); position.setCourse(BitUtil.to(flags, 10)); position.setValid(BitUtil.check(flags, 12)); if (!BitUtil.check(flags, 10)) { latitude = -latitude; } if (BitUtil.check(flags, 11)) { longitude = -longitude; } position.setLatitude(latitude); position.setLongitude(longitude); buf.readUnsignedIntLE(); // info index buf.readUnsignedIntLE(); // setting index flags = buf.readUnsignedByte(); position.set(Position.KEY_CHARGE, BitUtil.check(flags, 0)); position.set(Position.KEY_IGNITION, BitUtil.check(flags, 1)); position.set(Position.KEY_ALARM, BitUtil.check(flags, 4) ? Position.ALARM_GENERAL : null); buf.readUnsignedShortLE(); // charge current position.set(Position.KEY_ODOMETER, buf.readUnsignedIntLE()); sendResponse(channel, remoteAddress, type); return position; }
From source file:org.traccar.protocol.ApelProtocolDecoder.java
License:Apache License
@Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg; int type = buf.readUnsignedShortLE(); boolean alarm = (type & 0x8000) != 0; type = type & 0x7FFF;//ww w . j a va 2 s. c o m buf.readUnsignedShortLE(); // length if (alarm) { sendSimpleMessage(channel, MSG_ACK_ALARM); } if (type == MSG_TRACKER_ID) { return null; // unsupported authentication type } if (type == MSG_TRACKER_ID_EXT) { buf.readUnsignedIntLE(); // id int length = buf.readUnsignedShortLE(); buf.skipBytes(length); length = buf.readUnsignedShortLE(); getDeviceSession(channel, remoteAddress, buf.readSlice(length).toString(StandardCharsets.US_ASCII)); } else if (type == MSG_LAST_LOG_INDEX) { long index = buf.readUnsignedIntLE(); if (index > 0) { newIndex = index; requestArchive(channel); } } else if (type == MSG_CURRENT_GPS_DATA || type == MSG_STATE_FULL_INFO_T104 || type == MSG_LOG_RECORDS) { DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); if (deviceSession == null) { return null; } List<Position> positions = new LinkedList<>(); int recordCount = 1; if (type == MSG_LOG_RECORDS) { recordCount = buf.readUnsignedShortLE(); } for (int j = 0; j < recordCount; j++) { Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); int subtype = type; if (type == MSG_LOG_RECORDS) { position.set(Position.KEY_ARCHIVE, true); lastIndex = buf.readUnsignedIntLE() + 1; position.set(Position.KEY_INDEX, lastIndex); subtype = buf.readUnsignedShortLE(); if (subtype != MSG_CURRENT_GPS_DATA && subtype != MSG_STATE_FULL_INFO_T104) { buf.skipBytes(buf.readUnsignedShortLE()); continue; } buf.readUnsignedShortLE(); // length } position.setTime(new Date(buf.readUnsignedIntLE() * 1000)); position.setLatitude(buf.readIntLE() * 180.0 / 0x7FFFFFFF); position.setLongitude(buf.readIntLE() * 180.0 / 0x7FFFFFFF); if (subtype == MSG_STATE_FULL_INFO_T104) { int speed = buf.readUnsignedByte(); position.setValid(speed != 255); position.setSpeed(UnitsConverter.knotsFromKph(speed)); position.set(Position.KEY_HDOP, buf.readByte()); } else { int speed = buf.readShortLE(); position.setValid(speed != -1); position.setSpeed(UnitsConverter.knotsFromKph(speed * 0.01)); } position.setCourse(buf.readShortLE() * 0.01); position.setAltitude(buf.readShortLE()); if (subtype == MSG_STATE_FULL_INFO_T104) { position.set(Position.KEY_SATELLITES, buf.readUnsignedByte()); position.set(Position.KEY_RSSI, buf.readUnsignedByte()); position.set(Position.KEY_EVENT, buf.readUnsignedShortLE()); position.set(Position.KEY_ODOMETER, buf.readUnsignedIntLE()); position.set(Position.KEY_INPUT, buf.readUnsignedByte()); position.set(Position.KEY_OUTPUT, buf.readUnsignedByte()); for (int i = 1; i <= 8; i++) { position.set(Position.PREFIX_ADC + i, buf.readUnsignedShortLE()); } position.set(Position.PREFIX_COUNT + 1, buf.readUnsignedIntLE()); position.set(Position.PREFIX_COUNT + 2, buf.readUnsignedIntLE()); position.set(Position.PREFIX_COUNT + 3, buf.readUnsignedIntLE()); } positions.add(position); } buf.readUnsignedIntLE(); // crc if (type == MSG_LOG_RECORDS) { requestArchive(channel); } else { sendSimpleMessage(channel, MSG_REQUEST_LAST_LOG_INDEX); } return positions; } return null; }
From source file:org.traccar.protocol.AplicomProtocolDecoder.java
License:Apache License
private void decodeCanData(ByteBuf buf, Position position) { buf.readUnsignedMedium(); // packet identifier position.set(Position.KEY_VERSION_FW, buf.readUnsignedByte()); int count = buf.readUnsignedByte(); buf.readUnsignedByte(); // batch count buf.readUnsignedShort(); // selector bit buf.readUnsignedInt(); // timestamp buf.skipBytes(8);/* w w w. j av a 2s .c o m*/ ArrayList<ByteBuf> values = new ArrayList<>(count); for (int i = 0; i < count; i++) { values.add(buf.readSlice(8)); } for (int i = 0; i < count; i++) { ByteBuf value = values.get(i); switch (buf.readInt()) { case 0x20D: position.set(Position.KEY_RPM, value.readShortLE()); position.set("dieselTemperature", value.readShortLE() * 0.1); position.set("batteryVoltage", value.readShortLE() * 0.01); position.set("supplyAirTempDep1", value.readShortLE() * 0.1); break; case 0x30D: position.set("activeAlarm", ByteBufUtil.hexDump(value)); break; case 0x40C: position.set("airTempDep1", value.readShortLE() * 0.1); position.set("airTempDep2", value.readShortLE() * 0.1); break; case 0x40D: position.set("coldUnitState", ByteBufUtil.hexDump(value)); break; case 0x50C: position.set("defrostTempDep1", value.readShortLE() * 0.1); position.set("defrostTempDep2", value.readShortLE() * 0.1); break; case 0x50D: position.set("condenserPressure", value.readShortLE() * 0.1); position.set("suctionPressure", value.readShortLE() * 0.1); break; case 0x58C: value.readByte(); value.readShort(); // index switch (value.readByte()) { case 0x01: position.set("setpointZone1", value.readIntLE() * 0.1); break; case 0x02: position.set("setpointZone2", value.readIntLE() * 0.1); break; case 0x05: position.set("unitType", value.readIntLE()); break; case 0x13: position.set("dieselHours", value.readIntLE() / 60 / 60); break; case 0x14: position.set("electricHours", value.readIntLE() / 60 / 60); break; case 0x17: position.set("serviceIndicator", value.readIntLE()); break; case 0x18: position.set("softwareVersion", value.readIntLE() * 0.01); break; default: break; } break; default: LOGGER.warn("Aplicom CAN decoding error", new UnsupportedOperationException()); break; } } }