List of usage examples for io.netty.buffer ByteBuf readRetainedSlice
public abstract ByteBuf readRetainedSlice(int length);
From source file:org.traccar.protocol.T57FrameDecoder.java
License:Apache License
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { if (buf.readableBytes() < 10) { return null; }// ww w . j av a2s . c o m String type = buf.toString(buf.readerIndex() + 5, 2, StandardCharsets.US_ASCII); int count = type.equals("F3") ? 12 : 14; int index = 0; while (index >= 0 && count > 0) { index = buf.indexOf(index + 1, buf.writerIndex(), (byte) '#'); if (index > 0) { count -= 1; } } return index > 0 ? buf.readRetainedSlice(index + 1 - buf.readerIndex()) : null; }
From source file:org.traccar.protocol.TelicFrameDecoder.java
License:Apache License
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { if (buf.readableBytes() < 4) { return null; }/*ww w .j av a 2 s .c o m*/ long length = buf.getUnsignedIntLE(buf.readerIndex()); if (length < 1024) { if (buf.readableBytes() >= length + 4) { buf.readUnsignedIntLE(); return buf.readRetainedSlice((int) length); } } else { int endIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) 0); if (endIndex >= 0) { ByteBuf frame = buf.readRetainedSlice(endIndex - buf.readerIndex()); buf.readByte(); if (frame.readableBytes() > 0) { return frame; } } } return null; }
From source file:org.traccar.protocol.TeltonikaFrameDecoder.java
License:Apache License
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { // Check minimum length if (buf.readableBytes() < MESSAGE_MINIMUM_LENGTH) { return null; }// w ww . java 2 s . c o m // Read packet int length = buf.getUnsignedShort(buf.readerIndex()); if (length > 0) { if (buf.readableBytes() >= (length + 2)) { return buf.readRetainedSlice(length + 2); } } else { int dataLength = buf.getInt(buf.readerIndex() + 4); if (buf.readableBytes() >= (dataLength + 12)) { return buf.readRetainedSlice(dataLength + 12); } } return null; }
From source file:org.traccar.protocol.Tk102ProtocolDecoder.java
License:Apache License
@Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg; buf.skipBytes(1); // header int type = buf.readUnsignedByte(); ByteBuf dataSequence = buf.readSlice(10); int length = buf.readUnsignedByte(); if (type == MSG_LOGIN_REQUEST || type == MSG_LOGIN_REQUEST_2) { ByteBuf data = buf.readSlice(length); String id;/*from w w w . j ava 2s .com*/ if (type == MSG_LOGIN_REQUEST) { id = data.toString(StandardCharsets.US_ASCII); } else { id = data.copy(1, 15).toString(StandardCharsets.US_ASCII); } if (getDeviceSession(channel, remoteAddress, id) != null) { ByteBuf response = Unpooled.buffer(); response.writeByte(MODE_GPRS); response.writeBytes(data); sendResponse(channel, MSG_LOGIN_RESPONSE, dataSequence, response); } } else if (type == MSG_HEARTBEAT_REQUEST) { sendResponse(channel, MSG_HEARTBEAT_RESPONSE, dataSequence, buf.readRetainedSlice(length)); } else { DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); if (deviceSession == null) { return null; } Parser parser = new Parser(PATTERN, buf.readSlice(length).toString(StandardCharsets.US_ASCII)); 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)); dateBuilder.setDateReverse(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0)); position.setTime(dateBuilder.getDate()); return position; } return null; }
From source file:org.traccar.protocol.Tk103FrameDecoder.java
License:Apache License
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { if (buf.readableBytes() < 2) { return null; }/*from w ww. java 2 s . c o m*/ int frameStartIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '('); if (frameStartIndex == -1) { buf.clear(); return null; } int frameEndIndex, freeTextSymbolCounter; for (frameEndIndex = frameStartIndex, freeTextSymbolCounter = 0;; frameEndIndex++) { int freeTextIndex = frameEndIndex; frameEndIndex = buf.indexOf(frameEndIndex, buf.writerIndex(), (byte) ')'); if (frameEndIndex == -1) { break; } for (;; freeTextIndex++, freeTextSymbolCounter++) { freeTextIndex = buf.indexOf(freeTextIndex, frameEndIndex, (byte) '$'); if (freeTextIndex == -1 || freeTextIndex >= frameEndIndex) { break; } } if (freeTextSymbolCounter % 2 == 0) { break; } } if (frameEndIndex == -1) { while (buf.readableBytes() > 1024) { int discardUntilIndex = buf.indexOf(buf.readerIndex() + 1, buf.writerIndex(), (byte) '('); if (discardUntilIndex == -1) { buf.clear(); } else { buf.readerIndex(discardUntilIndex); } } return null; } buf.readerIndex(frameStartIndex); return buf.readRetainedSlice(frameEndIndex + 1 - frameStartIndex); }
From source file:org.traccar.protocol.TmgFrameDecoder.java
License:Apache License
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { int beginIndex = findHeader(buf); if (beginIndex >= 0) { buf.readerIndex(beginIndex);/*from w w w . j av a 2s . com*/ int endIndex = buf.indexOf(beginIndex, buf.writerIndex(), (byte) '\n'); if (endIndex >= 0) { ByteBuf frame = buf.readRetainedSlice(endIndex - beginIndex); buf.readByte(); // delimiter return frame; } } return null; }
From source file:org.traccar.protocol.TotemFrameDecoder.java
License:Apache License
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { if (buf.readableBytes() < 10) { return null; }//from w ww .j ava2 s.c o m int beginIndex = BufferUtil.indexOf("$$", buf); if (beginIndex == -1) { return null; } else if (beginIndex > buf.readerIndex()) { buf.readerIndex(beginIndex); } int length; if (buf.getByte(buf.readerIndex() + 2) == (byte) '0') { length = Integer.parseInt(buf.toString(buf.readerIndex() + 2, 4, StandardCharsets.US_ASCII)); } else { length = Integer.parseInt(buf.toString(buf.readerIndex() + 2, 2, StandardCharsets.US_ASCII), 16); } if (length <= buf.readableBytes()) { return buf.readRetainedSlice(length); } return null; }
From source file:org.traccar.protocol.TramigoFrameDecoder.java
License:Apache License
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { if (buf.readableBytes() < 20) { return null; }/*from w w w . jav a 2s .c o m*/ int length; if (buf.getUnsignedByte(buf.readerIndex()) == 0x80) { length = buf.getUnsignedShortLE(buf.readerIndex() + 6); } else { length = buf.getUnsignedShort(buf.readerIndex() + 6); } if (length >= buf.readableBytes()) { return buf.readRetainedSlice(length); } return null; }
From source file:org.traccar.protocol.UlbotechFrameDecoder.java
License:Apache License
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { if (buf.readableBytes() < 2) { return null; }//from ww w.ja va 2 s.c om if (buf.getUnsignedByte(buf.readerIndex()) == 0xF8) { int index = buf.indexOf(buf.readerIndex() + 1, buf.writerIndex(), (byte) 0xF8); if (index != -1) { ByteBuf result = Unpooled.buffer(index + 1 - buf.readerIndex()); while (buf.readerIndex() <= index) { int b = buf.readUnsignedByte(); if (b == 0xF7) { int ext = buf.readUnsignedByte(); if (ext == 0x00) { result.writeByte(0xF7); } else if (ext == 0x0F) { result.writeByte(0xF8); } } else { result.writeByte(b); } } return result; } } else { int index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '#'); if (index != -1) { return buf.readRetainedSlice(index + 1 - buf.readerIndex()); } } return null; }
From source file:org.traccar.protocol.VtfmsFrameDecoder.java
License:Apache License
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { int endIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) ')'); if (endIndex > 0) { endIndex += 1 + 3;/* w w w . j a v a 2 s. c o m*/ if (buf.writerIndex() >= endIndex) { return buf.readRetainedSlice(endIndex - buf.readerIndex()); } } return null; }