List of usage examples for io.netty.buffer ByteBuf readRetainedSlice
public abstract ByteBuf readRetainedSlice(int length);
From source file:org.traccar.protocol.Gt06FrameDecoder.java
License:Apache License
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { if (buf.readableBytes() < 5) { return null; }// w w w. ja v a2s .c o m int length = 2 + 2; // head and tail if (buf.getByte(buf.readerIndex()) == 0x78) { length += 1 + buf.getUnsignedByte(buf.readerIndex() + 2); } else { length += 2 + buf.getUnsignedShort(buf.readerIndex() + 2); } if (buf.readableBytes() >= length && buf.getUnsignedShort(buf.readerIndex() + length - 2) == 0x0d0a) { return buf.readRetainedSlice(length); } int endIndex = buf.readerIndex() - 1; do { endIndex = buf.indexOf(endIndex + 1, buf.writerIndex(), (byte) 0x0d); if (endIndex > 0 && buf.writerIndex() > endIndex + 1 && buf.getByte(endIndex + 1) == 0x0a) { return buf.readRetainedSlice(endIndex + 2 - buf.readerIndex()); } } while (endIndex > 0); return null; }
From source file:org.traccar.protocol.H02FrameDecoder.java
License:Apache License
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { char marker = (char) buf.getByte(buf.readerIndex()); while (marker != '*' && marker != '$' && marker != 'X' && buf.readableBytes() > 0) { buf.skipBytes(1);//from w ww . j a va 2 s .c om if (buf.readableBytes() > 0) { marker = (char) buf.getByte(buf.readerIndex()); } } switch (marker) { case '*': // Return text message int index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '#'); if (index != -1) { ByteBuf result = buf.readRetainedSlice(index + 1 - buf.readerIndex()); while (buf.isReadable() && (buf.getByte(buf.readerIndex()) == '\r' || buf.getByte(buf.readerIndex()) == '\n')) { buf.readByte(); // skip new line } return result; } break; case '$': if (messageLength == 0) { if (buf.readableBytes() == MESSAGE_LONG) { messageLength = MESSAGE_LONG; } else { messageLength = MESSAGE_SHORT; } } if (buf.readableBytes() >= messageLength) { return buf.readRetainedSlice(messageLength); } break; case 'X': if (buf.readableBytes() >= MESSAGE_SHORT) { return buf.readRetainedSlice(MESSAGE_SHORT); } break; default: return null; } return null; }
From source file:org.traccar.protocol.IntellitracFrameDecoder.java
License:Apache License
@Override protected Object decode(ChannelHandlerContext ctx, ByteBuf buf) throws Exception { // Check minimum length if (buf.readableBytes() < MESSAGE_MINIMUM_LENGTH) { return null; }//from w ww . j a v a 2s .com // Check for sync packet if (buf.getUnsignedShort(buf.readerIndex()) == 0xFAF8) { ByteBuf syncMessage = buf.readRetainedSlice(8); if (ctx != null && ctx.channel() != null) { ctx.channel().writeAndFlush(new NetworkMessage(syncMessage, ctx.channel().remoteAddress())); } } return super.decode(ctx, buf); }
From source file:org.traccar.protocol.ItsFrameDecoder.java
License:Apache License
private ByteBuf readFrame(ByteBuf buf, int delimiterIndex, int skip) { int headerIndex = buf.indexOf(buf.readerIndex() + 1, buf.writerIndex(), (byte) '$'); if (headerIndex > 0 && headerIndex < delimiterIndex) { return buf.readRetainedSlice(headerIndex - buf.readerIndex()); } else {// w w w . j a v a 2 s. co m ByteBuf frame = buf.readRetainedSlice(delimiterIndex - buf.readerIndex()); buf.skipBytes(skip); return frame; } }
From source file:org.traccar.protocol.JpKorjarFrameDecoder.java
License:Apache License
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { if (buf.readableBytes() < 80) { return null; }/*from w w w.j a va 2 s. c om*/ int spaceIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) ' '); if (spaceIndex == -1) { return null; } int endIndex = buf.indexOf(spaceIndex, buf.writerIndex(), (byte) ','); if (endIndex == -1) { return null; } return buf.readRetainedSlice(endIndex + 1); }
From source file:org.traccar.protocol.Jt600FrameDecoder.java
License:Apache License
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { if (buf.readableBytes() < 10) { return null; }//w ww.j av a 2 s . c om char type = (char) buf.getByte(buf.readerIndex()); if (type == '$') { boolean longFormat = buf.getUnsignedByte(buf.readerIndex() + 1) == 0x75; int length = buf.getUnsignedShort(buf.readerIndex() + (longFormat ? 8 : 7)) + 10; if (length <= buf.readableBytes()) { return buf.readRetainedSlice(length); } } else if (type == '(') { int endIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) ')'); if (endIndex != -1) { return buf.readRetainedSlice(endIndex + 1); } } else { throw new ParseException(null, 0); // unknown message } return null; }
From source file:org.traccar.protocol.L100FrameDecoder.java
License:Apache License
private Object decodeOld(ByteBuf buf) { int header = buf.getByte(buf.readerIndex()); boolean obd = header == 'L' || header == 'H'; int index;//from w w w . ja v a 2 s . c o m if (obd) { index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '*'); } else { index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) 0x02); if (index < 0) { index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) 0x04); if (index < 0) { return null; } } } index += 2; // checksum if (buf.writerIndex() >= index) { if (!obd) { buf.skipBytes(2); // header } ByteBuf frame = buf.readRetainedSlice(index - buf.readerIndex() - 2); buf.skipBytes(2); // footer return frame; } return null; }
From source file:org.traccar.protocol.L100FrameDecoder.java
License:Apache License
private Object decodeNew(ByteBuf buf) { int index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '@'); if (index < 0) { return null; }//from ww w . j av a 2 s.c o m if (buf.writerIndex() >= index + 1) { ByteBuf frame = buf.readRetainedSlice(index - buf.readerIndex()); buf.skipBytes(1); // delimiter return frame; } return null; }
From source file:org.traccar.protocol.MegastekFrameDecoder.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 a va 2 s .c o m*/ if (Character.isDigit(buf.getByte(buf.readerIndex()))) { int length = 4 + Integer.parseInt(buf.toString(buf.readerIndex(), 4, StandardCharsets.US_ASCII)); if (buf.readableBytes() >= length) { return buf.readRetainedSlice(length); } } else { while (buf.getByte(buf.readerIndex()) == '\r' || buf.getByte(buf.readerIndex()) == '\n') { buf.skipBytes(1); } int delimiter = BufferUtil.indexOf("\r\n", buf); if (delimiter == -1) { delimiter = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '!'); } if (delimiter != -1) { ByteBuf result = buf.readRetainedSlice(delimiter - buf.readerIndex()); buf.skipBytes(1); return result; } } return null; }
From source file:org.traccar.protocol.MeiligaoFrameDecoder.java
License:Apache License
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { // Strip not '$' (0x24) bytes from the beginning while (buf.isReadable() && buf.getUnsignedByte(buf.readerIndex()) != 0x24) { buf.readByte();/*from w ww.ja va 2 s . co m*/ } // Check length and return buffer if (buf.readableBytes() >= MESSAGE_HEADER) { int length = buf.getUnsignedShort(buf.readerIndex() + 2); if (buf.readableBytes() >= length) { return buf.readRetainedSlice(length); } } return null; }