List of usage examples for io.netty.buffer ByteBuf getUnsignedMedium
public abstract int getUnsignedMedium(int index);
From source file:com.cc.nettytest.proxy.decoder.CCLengthFieldBasedFrameDecoder.java
License:Apache License
@Override public Object decode(ChannelHandlerContext ctx, ByteBuf inBuffer) throws Exception { if (discardingTooLongFrame) { long bytesToDiscard = this.bytesToDiscard; int localBytesToDiscard = (int) Math.min(bytesToDiscard, inBuffer.readableBytes()); inBuffer.skipBytes(localBytesToDiscard); bytesToDiscard -= localBytesToDiscard; this.bytesToDiscard = bytesToDiscard; failIfNecessary(ctx, false);// w ww. jav a 2 s. c o m return null; } if (inBuffer.readableBytes() < lengthFieldEndOffset) { return null; } int actualLengthFieldOffset = inBuffer.readerIndex() + lengthFieldOffset; long frameLength; switch (lengthFieldLength) { case 1: frameLength = inBuffer.getUnsignedByte(actualLengthFieldOffset); break; case 2: frameLength = inBuffer.getUnsignedShort(actualLengthFieldOffset); break; case 3: frameLength = inBuffer.getUnsignedMedium(actualLengthFieldOffset); break; case 4: frameLength = ByteBufUtil.swapInt((int) inBuffer.getUnsignedInt(actualLengthFieldOffset)); //SWAP FOR UIMANAGER break; case 8: frameLength = inBuffer.getLong(actualLengthFieldOffset); break; default: throw new Error("should not reach here"); } if (frameLength < 0) { inBuffer.skipBytes(lengthFieldEndOffset); throw new CorruptedFrameException("negative pre-adjustment length field: " + frameLength); } frameLength += lengthAdjustment + lengthFieldEndOffset; if (frameLength < lengthFieldEndOffset) { inBuffer.skipBytes(lengthFieldEndOffset); throw new CorruptedFrameException("Adjusted frame length (" + frameLength + ") is less " + "than lengthFieldEndOffset: " + lengthFieldEndOffset); } if (frameLength > maxFrameLength) { // Enter the discard mode and discard everything received so far. discardingTooLongFrame = true; tooLongFrameLength = frameLength; bytesToDiscard = frameLength - inBuffer.readableBytes(); inBuffer.skipBytes(inBuffer.readableBytes()); failIfNecessary(ctx, true); return null; } // never overflows because it's less than maxFrameLength int frameLengthInt = (int) frameLength; if (inBuffer.readableBytes() < frameLengthInt) { return null; } if (initialBytesToStrip > frameLengthInt) { inBuffer.skipBytes(frameLengthInt); throw new CorruptedFrameException("Adjusted frame length (" + frameLength + ") is less " + "than initialBytesToStrip: " + initialBytesToStrip); } inBuffer.skipBytes(initialBytesToStrip); // extract frame int readerIndex = inBuffer.readerIndex(); int actualFrameLength = frameLengthInt - initialBytesToStrip; ByteBuf frame = extractFrame(inBuffer, readerIndex, actualFrameLength); inBuffer.readerIndex(readerIndex + actualFrameLength); return frame; }
From source file:com.chat.common.netty.handler.decode.LengthFieldBasedFrameDecoder.java
License:Apache License
/** * Decodes the specified region of the buffer into an unadjusted frame length. The default implementation is * capable of decoding the specified region into an unsigned 8/16/24/32/64 bit integer. Override this method to * decode the length field encoded differently. Note that this method must not modify the state of the specified * buffer (e.g. {@code readerIndex}, {@code writerIndex}, and the content of the buffer.) * * @throws DecoderException if failed to decode the specified region *///from ww w . ja va 2 s . c om protected long getUnadjustedFrameLength(ByteBuf buf, int offset, int length, ByteOrder order) { buf = buf.order(order); long frameLength; switch (length) { case 1: frameLength = buf.getUnsignedByte(offset); break; case 2: frameLength = buf.getUnsignedShort(offset); break; case 3: frameLength = buf.getUnsignedMedium(offset); break; case 4: frameLength = buf.getUnsignedInt(offset); break; case 8: frameLength = buf.getLong(offset); break; default: throw new DecoderException( "unsupported lengthFieldLength: " + lengthFieldLength + " (expected: 1, 2, 3, 4, or 8)"); } return frameLength; }
From source file:com.netty.test.LengthFieldBasedExFrameDecoder.java
License:Apache License
private long getFrameLength(ByteBuf in, int actualLengthFieldOffset) { in = in.order(byteOrder);// www .j a va2 s . co m long frameLength; switch (lengthFieldLength) { case 1: frameLength = in.getUnsignedByte(actualLengthFieldOffset); break; case 2: frameLength = in.getUnsignedShort(actualLengthFieldOffset); break; case 3: frameLength = in.getUnsignedMedium(actualLengthFieldOffset); break; case 4: frameLength = in.getUnsignedInt(actualLengthFieldOffset); break; case 8: frameLength = in.getLong(actualLengthFieldOffset); break; default: throw new Error("should not reach here"); } return frameLength; }
From source file:com.tesora.dve.sql.LargeMaxPktTest.java
License:Open Source License
@Test public void testComQueryMessageContinuationOverlap() throws Exception { int defaultMaxPacket = 0xFFFFFF; int payloadSize = (defaultMaxPacket * 4) + (4 * 40); //four full packets and a little change, divisible by 4. ByteBuf source = Unpooled.buffer(payloadSize, payloadSize); Random rand = new Random(239873L); while (source.isWritable()) source.writeInt(rand.nextInt()); Assert.assertEquals(source.writableBytes(), 0, "Oops, I intended to fill up the source buffer"); ByteBuf dest = Unpooled.buffer(payloadSize); MSPComQueryRequestMessage outboundMessage = MSPComQueryRequestMessage.newMessage(source.array()); Packet.encodeFullMessage((byte) 0, outboundMessage, dest); int lengthOfNonUserdata = 5 + 4 + 4 + 4 + 4; Assert.assertEquals(dest.readableBytes(), payloadSize + lengthOfNonUserdata, "Number of bytes in destination buffer is wrong"); Assert.assertEquals(dest.getUnsignedMedium(0), defaultMaxPacket, "First length should be maximum"); Assert.assertEquals(dest.getByte(3), (byte) 0, "First sequenceID should be zero"); Assert.assertEquals(dest.getByte(4), (byte) MSPComQueryRequestMessage.TYPE_IDENTIFIER, "First byte of payload should be MSPComQueryRequestMessage.TYPE_IDENTIFIER"); ByteBuf sliceFirstPayload = dest.slice(5, (0xFFFFFF - 1)); Assert.assertEquals(sliceFirstPayload, source.slice(0, 0xFFFFFF - 1)); }
From source file:com.tesora.dve.sql.LargeMaxPktTest.java
License:Open Source License
@Test public void testComQueryMessageContinuationExact() throws Exception { int defaultMaxPacket = 0xFFFFFF; int payloadSize = (defaultMaxPacket * 4); //four full packets exactly, requires an empty trailing packet. ByteBuf source = Unpooled.buffer(payloadSize, payloadSize); Random rand = new Random(239873L); while (source.isWritable()) source.writeInt(rand.nextInt()); Assert.assertEquals(source.writableBytes(), 0, "Oops, I intended to fill up the source buffer"); ByteBuf dest = Unpooled.buffer(payloadSize); MSPComQueryRequestMessage outboundMessage = MSPComQueryRequestMessage.newMessage(source.array()); Packet.encodeFullMessage((byte) 0, outboundMessage, dest); int lengthOfNonUserdata = 5 + 4 + 4 + 4 + 4;//last packet has zero length payload Assert.assertEquals(dest.readableBytes(), payloadSize + lengthOfNonUserdata, "Number of bytes in destination buffer is wrong"); Assert.assertEquals(dest.getUnsignedMedium(0), defaultMaxPacket, "First length should be maximum"); Assert.assertEquals(dest.getByte(3), (byte) 0, "First sequenceID should be zero"); Assert.assertEquals(dest.getByte(4), (byte) MSPComQueryRequestMessage.TYPE_IDENTIFIER, "First byte of payload should be MSPComQueryRequestMessage.TYPE_IDENTIFIER"); ByteBuf sliceFirstPayload = dest.slice(5, (0xFFFFFF - 1)); Assert.assertEquals(sliceFirstPayload, source.slice(0, 0xFFFFFF - 1)); }
From source file:org.dcache.xrootd.protocol.messages.LocateRequest.java
License:Open Source License
public LocateRequest(ByteBuf buffer) { super(buffer, kXR_locate); options = buffer.getUnsignedMedium(4); }
From source file:org.graylog.plugins.netflow.utils.ByteBufUtils.java
License:Apache License
public static long getUnsignedInteger(final ByteBuf buf, final int offset, final int length) { switch (length) { case 1:/* w ww . j a va 2 s. c om*/ return buf.getUnsignedByte(offset); case 2: return buf.getUnsignedShort(offset); case 3: return buf.getUnsignedMedium(offset); case 4: return buf.getUnsignedInt(offset); case 8: return buf.getLong(offset) & 0x00000000ffffffffL; default: return 0L; } }
From source file:org.opendaylight.protocol.util.MplsLabelUtil.java
License:Open Source License
/** * @param slice with 20 leftmost bits as label * @return value of bottom bit/*from w w w . j ava2s . com*/ */ public static boolean getBottomBit(final ByteBuf slice) { return (slice.getUnsignedMedium(slice.readerIndex()) & BOTTOM_LABEL_BIT) == 1; }
From source file:org.traccar.protocol.PricolProtocolDecoder.java
License:Apache License
@Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg; buf.readUnsignedByte(); // header DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, buf.readSlice(7).toString(StandardCharsets.US_ASCII)); if (deviceSession == null) { return null; }//ww w . ja va 2 s. c o m Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); position.set("eventType", buf.readUnsignedByte()); position.set("packetVersion", buf.readUnsignedByte()); position.set(Position.KEY_STATUS, buf.readUnsignedByte()); position.set(Position.KEY_RSSI, buf.readUnsignedByte()); position.set(Position.KEY_GPS, buf.readUnsignedByte()); position.setTime(new DateBuilder() .setDateReverse(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte()) .setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte()).getDate()); position.setValid(true); double lat = buf.getUnsignedShort(buf.readerIndex()) / 100; lat += (buf.readUnsignedShort() % 100 * 10000 + buf.readUnsignedShort()) / 600000.0; position.setLatitude(buf.readUnsignedByte() == 'S' ? -lat : lat); double lon = buf.getUnsignedMedium(buf.readerIndex()) / 100; lon += (buf.readUnsignedMedium() % 100 * 10000 + buf.readUnsignedShort()) / 600000.0; position.setLongitude(buf.readUnsignedByte() == 'W' ? -lon : lon); position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte())); position.set(Position.KEY_INPUT, buf.readUnsignedShort()); position.set(Position.KEY_OUTPUT, buf.readUnsignedByte()); position.set("analogAlerts", buf.readUnsignedByte()); position.set("customAlertTypes", buf.readUnsignedShort()); for (int i = 1; i <= 5; i++) { position.set(Position.PREFIX_ADC + i, buf.readUnsignedShort()); } position.set(Position.KEY_ODOMETER, buf.readUnsignedMedium()); position.set(Position.KEY_RPM, buf.readUnsignedShort()); if (channel != null) { channel.writeAndFlush( new NetworkMessage(Unpooled.copiedBuffer("ACK", StandardCharsets.US_ASCII), remoteAddress)); } return position; }