List of usage examples for io.netty.buffer ByteBuf toString
public abstract String toString(int index, int length, Charset charset);
From source file:org.spongepowered.common.network.ByteBufUtils.java
License:MIT License
public static String readUTF8String(ByteBuf from) { int len = readVarInt(from, 2); String str = from.toString(from.readerIndex(), len, Charsets.UTF_8); from.readerIndex(from.readerIndex() + len); return str;/*from ww w . j a v a 2s .c om*/ }
From source file:org.springframework.cloud.gateway.rsocket.support.Metadata.java
License:Apache License
private static String decodeString(ByteBuf byteBuf, AtomicInteger offset) { int length = byteBuf.getByte(offset.get()); int index = offset.addAndGet(Byte.BYTES); String s = byteBuf.toString(index, length, StandardCharsets.UTF_8); offset.addAndGet(length);/* www . j a v a2 s. c o m*/ return s; }
From source file:org.traccar.protocol.AtrackFrameDecoder.java
License:Apache License
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { if (buf.readableBytes() >= 2) { if (buf.getUnsignedShort(buf.readerIndex()) == 0xfe02) { if (buf.readableBytes() >= KEEPALIVE_LENGTH) { return buf.readRetainedSlice(KEEPALIVE_LENGTH); }/*from ww w .j a v a2 s . c o m*/ } else if (buf.getUnsignedShort(buf.readerIndex()) == 0x4050 && buf.getByte(buf.readerIndex() + 2) != ',') { if (buf.readableBytes() > 6) { int length = buf.getUnsignedShort(buf.readerIndex() + 4) + 4 + 2; if (buf.readableBytes() >= length) { return buf.readRetainedSlice(length); } } } else { int lengthStart = buf.indexOf(buf.readerIndex() + 3, buf.writerIndex(), (byte) ',') + 1; if (lengthStart > 0) { int lengthEnd = buf.indexOf(lengthStart, buf.writerIndex(), (byte) ','); if (lengthEnd > 0) { int length = lengthEnd + Integer.parseInt( buf.toString(lengthStart, lengthEnd - lengthStart, StandardCharsets.US_ASCII)); if (buf.readableBytes() > length && buf.getByte(buf.readerIndex() + length) == '\n') { length += 1; } if (buf.readableBytes() >= length) { return buf.readRetainedSlice(length); } } } else { int endIndex = BufferUtil.indexOf("\r\n", buf); if (endIndex > 0) { return buf.readRetainedSlice(endIndex - buf.readerIndex() + 2); } } } } return null; }
From source file:org.traccar.protocol.CityeasyProtocolDecoder.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.readUnsignedShort(); // length String imei = ByteBufUtil.hexDump(buf.readSlice(7)); DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei, imei + Checksum.luhn(Long.parseLong(imei))); if (deviceSession == null) { return null; }//from w ww . ja v a2 s .c o m int type = buf.readUnsignedShort(); if (type == MSG_LOCATION_REPORT || type == MSG_LOCATION_REQUEST) { String sentence = buf.toString(buf.readerIndex(), buf.readableBytes() - 8, StandardCharsets.US_ASCII); Parser parser = new Parser(PATTERN, sentence); if (!parser.matches()) { return null; } Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); if (parser.hasNext(15)) { position.setTime(parser.nextDateTime()); position.setValid(parser.next().equals("A")); position.set(Position.KEY_SATELLITES, parser.nextInt()); position.setLatitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG)); position.setLongitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG)); position.setSpeed(parser.nextDouble(0)); position.set(Position.KEY_HDOP, parser.nextDouble(0)); position.setAltitude(parser.nextDouble(0)); } else { getLastLocation(position, null); } position.setNetwork(new Network( CellTower.from(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0), parser.nextInt(0)))); return position; } return null; }
From source file:org.traccar.protocol.EnforaProtocolDecoder.java
License:Apache License
@Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg; // Find IMEI number int index = -1; for (int i = buf.readerIndex(); i < buf.writerIndex() - IMEI_LENGTH; i++) { index = i;/* w w w . ja v a 2 s.com*/ for (int j = i; j < i + IMEI_LENGTH; j++) { if (!Character.isDigit((char) buf.getByte(j))) { index = -1; break; } } if (index > 0) { break; } } if (index == -1) { return null; } String imei = buf.toString(index, IMEI_LENGTH, StandardCharsets.US_ASCII); DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei); if (deviceSession == null) { return null; } // Find NMEA sentence int start = BufferUtil.indexOf("GPRMC", buf); if (start == -1) { return null; } String sentence = buf.toString(start, buf.readableBytes() - start, StandardCharsets.US_ASCII); Parser parser = new Parser(PATTERN, sentence); if (!parser.matches()) { return null; } Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); DateBuilder dateBuilder = new DateBuilder().setTime(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0)); position.setValid(parser.next().equals("A")); position.setLatitude(parser.nextCoordinate()); position.setLongitude(parser.nextCoordinate()); position.setSpeed(parser.nextDouble(0)); position.setCourse(parser.nextDouble(0)); dateBuilder.setDateReverse(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0)); position.setTime(dateBuilder.getDate()); return position; }
From source file:org.traccar.protocol.Gl200FrameDecoder.java
License:Apache License
public static boolean isBinary(ByteBuf buf) { String header = buf.toString(buf.readerIndex(), 4, StandardCharsets.US_ASCII); if (header.equals("+ACK")) { return buf.getByte(buf.readerIndex() + header.length()) != (byte) ':'; } else {//ww w . j a v a 2s .c om return BINARY_HEADERS.contains(header); } }
From source file:org.traccar.protocol.Gl200FrameDecoder.java
License:Apache License
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { if (buf.readableBytes() < MINIMUM_LENGTH) { return null; }//from ww w.ja v a2 s .c o m if (isBinary(buf)) { int length; switch (buf.toString(buf.readerIndex(), 4, StandardCharsets.US_ASCII)) { case "+ACK": length = buf.getUnsignedByte(buf.readerIndex() + 6); break; case "+INF": case "+BNF": length = buf.getUnsignedShort(buf.readerIndex() + 7); break; case "+HBD": length = buf.getUnsignedByte(buf.readerIndex() + 5); break; case "+CRD": case "+BRD": length = buf.getUnsignedShort(buf.readerIndex() + 6); break; default: length = buf.getUnsignedShort(buf.readerIndex() + 9); break; } if (buf.readableBytes() >= length) { return buf.readRetainedSlice(length); } } else { int endIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '$'); if (endIndex < 0) { endIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) 0); } if (endIndex > 0) { ByteBuf frame = buf.readRetainedSlice(endIndex - buf.readerIndex()); buf.readByte(); // delimiter return frame; } } return null; }
From source file:org.traccar.protocol.Gps056FrameDecoder.java
License:Apache License
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { if (buf.readableBytes() >= MESSAGE_HEADER) { int length = Integer.parseInt(buf.toString(2, 2, StandardCharsets.US_ASCII)) + 5; if (buf.readableBytes() >= length) { ByteBuf frame = buf.readRetainedSlice(length); while (buf.isReadable() && buf.getUnsignedByte(buf.readerIndex()) != '$') { buf.readByte();/*from w ww.j ava2 s . co m*/ } return frame; } } return null; }
From source file:org.traccar.protocol.Gt06ProtocolDecoder.java
License:Apache License
private Object decodeExtended(Channel channel, SocketAddress remoteAddress, ByteBuf buf) { DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); if (deviceSession == null) { return null; }//from w w w. j a va 2 s . c o m if (deviceSession.getTimeZone() == null) { deviceSession.setTimeZone(getTimeZone(deviceSession.getDeviceId())); } Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); buf.readUnsignedShort(); // length int type = buf.readUnsignedByte(); if (type == MSG_STRING_INFO) { buf.readUnsignedInt(); // server flag String data; if (buf.readUnsignedByte() == 1) { data = buf.readSlice(buf.readableBytes() - 6).toString(StandardCharsets.US_ASCII); } else { data = buf.readSlice(buf.readableBytes() - 6).toString(StandardCharsets.UTF_16BE); } if (decodeLocationString(position, data) == null) { getLastLocation(position, null); position.set(Position.KEY_RESULT, data); } return position; } else if (type == MSG_INFO) { int subType = buf.readUnsignedByte(); getLastLocation(position, null); if (subType == 0x00) { position.set(Position.KEY_POWER, buf.readUnsignedShort() * 0.01); return position; } else if (subType == 0x05) { int flags = buf.readUnsignedByte(); position.set(Position.KEY_DOOR, BitUtil.check(flags, 0)); position.set(Position.PREFIX_IO + 1, BitUtil.check(flags, 2)); return position; } else if (subType == 0x0a) { buf.skipBytes(8); // imei buf.skipBytes(8); // imsi position.set("iccid", ByteBufUtil.hexDump(buf.readSlice(8))); return position; } else if (subType == 0x0d) { if (buf.getByte(buf.readerIndex()) != '!') { buf.skipBytes(6); } return decodeFuelData(position, buf.toString(buf.readerIndex(), buf.readableBytes() - 4 - 2, StandardCharsets.US_ASCII)); } } else if (type == MSG_X1_PHOTO_DATA) { int pictureId = buf.readInt(); ByteBuf photo = photos.get(pictureId); buf.readUnsignedInt(); // offset buf.readBytes(photo, buf.readUnsignedShort()); if (photo.writableBytes() > 0) { sendPhotoRequest(channel, pictureId); } else { Device device = Context.getDeviceManager().getById(deviceSession.getDeviceId()); position.set(Position.KEY_IMAGE, Context.getMediaManager().writeFile(device.getUniqueId(), photo, "jpg")); photos.remove(pictureId).release(); } } else if (type == MSG_AZ735_GPS || type == MSG_AZ735_ALARM) { if (!decodeGps(position, buf, true, deviceSession.getTimeZone())) { getLastLocation(position, position.getDeviceTime()); } if (decodeLbs(position, buf, true)) { position.set(Position.KEY_RSSI, buf.readUnsignedByte()); } buf.skipBytes(buf.readUnsignedByte()); // additional cell towers buf.skipBytes(buf.readUnsignedByte()); // wifi access point int status = buf.readUnsignedByte(); position.set(Position.KEY_STATUS, status); if (type == MSG_AZ735_ALARM) { switch (status) { case 0xA0: position.set(Position.KEY_ARMED, true); break; case 0xA1: position.set(Position.KEY_ARMED, false); break; case 0xA2: case 0xA3: position.set(Position.KEY_ALARM, Position.ALARM_LOW_BATTERY); break; case 0xA4: position.set(Position.KEY_ALARM, Position.ALARM_GENERAL); break; case 0xA5: position.set(Position.KEY_ALARM, Position.ALARM_DOOR); break; default: break; } } buf.skipBytes(buf.readUnsignedByte()); // reserved extension sendResponse(channel, true, type, buf.getShort(buf.writerIndex() - 6), null); return position; } else if (type == MSG_OBD) { DateBuilder dateBuilder = new DateBuilder(deviceSession.getTimeZone()) .setDate(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte()) .setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte()); getLastLocation(position, dateBuilder.getDate()); position.set(Position.KEY_IGNITION, buf.readUnsignedByte() > 0); String data = buf.readCharSequence(buf.readableBytes() - 18, StandardCharsets.US_ASCII).toString(); for (String pair : data.split(",")) { String[] values = pair.split("="); switch (Integer.parseInt(values[0].substring(0, 2), 16)) { case 40: position.set(Position.KEY_ODOMETER, Integer.parseInt(values[1], 16) * 0.01); break; case 43: position.set(Position.KEY_FUEL_LEVEL, Integer.parseInt(values[1], 16) * 0.01); break; case 45: position.set(Position.KEY_COOLANT_TEMP, Integer.parseInt(values[1], 16) * 0.01); break; case 53: position.set(Position.KEY_OBD_SPEED, Integer.parseInt(values[1], 16) * 0.01); break; case 54: position.set(Position.KEY_RPM, Integer.parseInt(values[1], 16) * 0.01); break; case 71: position.set(Position.KEY_FUEL_USED, Integer.parseInt(values[1], 16) * 0.01); break; case 73: position.set(Position.KEY_HOURS, Integer.parseInt(values[1], 16) * 0.01); break; case 74: position.set(Position.KEY_VIN, values[1]); break; default: break; } } return position; } return null; }
From source file:org.traccar.protocol.H02ProtocolDecoder.java
License:Apache License
@Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg; String marker = buf.toString(0, 1, StandardCharsets.US_ASCII); switch (marker) { case "*": String sentence = buf.toString(StandardCharsets.US_ASCII).trim(); int typeStart = sentence.indexOf(',', sentence.indexOf(',') + 1) + 1; int typeEnd = sentence.indexOf(',', typeStart); if (typeEnd > 0) { String type = sentence.substring(typeStart, typeEnd); switch (type) { case "NBR": return decodeLbs(sentence, channel, remoteAddress); case "LINK": return decodeLink(sentence, channel, remoteAddress); case "V3": return decodeV3(sentence, channel, remoteAddress); case "VP1": return decodeVp1(sentence, channel, remoteAddress); default: return decodeText(sentence, channel, remoteAddress); }/* ww w . j a v a 2 s .c o m*/ } else { return null; } case "$": return decodeBinary(buf, channel, remoteAddress); case "X": default: return null; } }