List of usage examples for io.netty.buffer ByteBuf readUnsignedShort
public abstract int readUnsignedShort();
From source file:net.epsilony.utils.codec.modbus.handler.ModbusSlaveResponseEncoderTest.java
License:Open Source License
@Test public void test() { ModbusSlaveResponseEncoder encoder = new ModbusSlaveResponseEncoder(); EmbeddedChannel channel = new EmbeddedChannel(encoder); ModbusResponse response = new ModbusResponse() { {/*from www. j a v a 2s.c o m*/ transectionId = 0xabcd; unitId = 0x83; } @Override public void writePduCore(ByteBuf out) { out.writeShort(0xabcd); out.writeShort(0xcdef); transectionId++; } @Override public int getPduCoreLength() { return 4; } @Override public int getFunctionCode() { // TODO Auto-generated method stub return 6; } }; ByteBuf buf = null; for (int i = 0; i < 3; i++) { channel.writeOutbound(response); buf = (ByteBuf) channel.readOutbound(); int[] buffer = new int[] { 0xab, 0xcd + i, 0x00, 0x00, 0x00, 0x06, 0x83, 0x06, 0xab, 0xcd, 0xcd, 0xef }; assertEquals(buffer.length, buf.readableBytes()); for (int b : buffer) { assertEquals(b, buf.readUnsignedByte()); } assertTrue(!buf.isReadable()); ReferenceCountUtil.release(buf); } for (int i = 0; i < 3; i++) { channel.writeOutbound(response); } for (int i = 0; i < 3; i++) { buf = (ByteBuf) channel.readOutbound(); int[] buffer = new int[] { 0xab, 0xcd + i + 3, 0x00, 0x00, 0x00, 0x06, 0x83, 0x06, 0xab, 0xcd, 0xcd, 0xef }; assertEquals(buffer.length, buf.readableBytes()); for (int b : buffer) { assertEquals(b, buf.readUnsignedByte()); } assertTrue(!buf.isReadable()); ReferenceCountUtil.release(buf); } encoder.setWithCheckSum(true); for (int i = 0; i < 3; i++) { channel.writeOutbound(response); } for (int i = 0; i < 3; i++) { buf = (ByteBuf) channel.readOutbound(); int[] buffer = new int[] { 0xab, 0xcd + i + 6, 0x00, 0x00, 0x00, 0x06, 0x83, 0x06, 0xab, 0xcd, 0xcd, 0xef }; assertEquals(buffer.length, buf.readableBytes() - 2); for (int b : buffer) { assertEquals(b, buf.readUnsignedByte()); } int calcCrc = Utils.crc(buf, buf.readerIndex() - buffer.length, buffer.length); assertEquals(calcCrc, buf.readUnsignedShort()); assertTrue(!buf.isReadable()); ReferenceCountUtil.release(buf); } }
From source file:net.tomp2p.message.MessageHeaderCodec.java
License:Apache License
/** * Decodes a message object.// w w w.j a va 2s . c om * * The format looks as follows: 28bit p2p version - 4bit message type - 32bit message id - 8bit message command - 160bit * sender id - 16bit sender tcp port - 16bit sender udp port - 160bit recipient id - 32bit content types - 8bit options. It total, * the header is of size 58 bytes. * * @param buffer * The buffer to decode from * @param recipientSocket * The recipient of the message * @param senderSocket * The sender of the packet, which has been set in the socket class * @return The partial message where only the header fields are set */ public static Message decodeHeader(final ByteBuf buffer, final InetSocketAddress recipientSocket, final InetSocketAddress senderSocket) { LOG.debug("Decode message. Recipient: {}, Sender:{}.", recipientSocket, senderSocket); final Message message = new Message(); final int versionAndType = buffer.readInt(); message.version(versionAndType >>> 4); message.type(Type.values()[(versionAndType & Utils.MASK_0F)]); message.messageId(buffer.readInt()); final int command = buffer.readUnsignedByte(); message.command((byte) command); final Number160 senderID = readID(buffer); final int tcpPort = buffer.readUnsignedShort(); final int udpPort = buffer.readUnsignedShort(); final Number160 recipientID = readID(buffer); message.recipient(new PeerAddress(recipientID, recipientSocket)); final int contentTypes = buffer.readInt(); message.hasContent(contentTypes != 0); message.contentTypes(decodeContentTypes(contentTypes, message)); // set the address as we see it, important for port forwarding // identification final int options = buffer.readUnsignedByte(); // three bits for the message options, 5 bits for the sender options message.options(options & 0x7); final int senderOptions = options >>> 3; final PeerAddress peerAddress = new PeerAddress(senderID, senderSocket.getAddress(), tcpPort, udpPort, senderOptions); message.sender(peerAddress); message.senderSocket(senderSocket); message.recipientSocket(recipientSocket); return message; }
From source file:net.tomp2p.peers.PeerSocketAddress.java
License:Apache License
/** * Decodes a PeerSocketAddress from a Netty buffer. * /*from ww w . ja v a 2 s . co m*/ * @param buf * The Netty buffer * @param isIPv4 * Whether the address is IPv4 or IPv6 * @return the PeerSocketAddress and the new offset */ public static PeerSocketAddress create(final ByteBuf buf, final boolean isIPv4) { final int tcpPort = buf.readUnsignedShort(); final int udpPort = buf.readUnsignedShort(); final InetAddress address; final byte[] me; if (isIPv4) { me = new byte[Utils.IPV4_BYTES]; buf.readBytes(me); address = Utils.inet4FromBytes(me, 0); } else { me = new byte[Utils.IPV6_BYTES]; buf.readBytes(me); address = Utils.inet6FromBytes(me, 0); } return new PeerSocketAddress(address, tcpPort, udpPort, buf.readerIndex()); }
From source file:net.tomp2p.rpc.SimpleBloomFilter.java
License:Apache License
/** * Constructs a SimpleBloomFilter out of existing data. * //w ww .ja v a2 s. co m * @param channelBuffer * The byte buffer with the data */ public SimpleBloomFilter(final ByteBuf channelBuffer) { this.byteArraySize = channelBuffer.readUnsignedShort() - (SIZE_HEADER_ELEMENTS + SIZE_HEADER_LENGTH); this.bitArraySize = byteArraySize * Byte.SIZE; int expectedElements = channelBuffer.readInt(); this.expectedElements = expectedElements; double hf = (bitArraySize / (double) expectedElements) * Math.log(2.0); this.k = (int) Math.ceil(hf); if (byteArraySize > 0) { byte[] me = new byte[byteArraySize]; channelBuffer.readBytes(me); this.bitSet = RPCUtils.fromByteArray(me); } else { this.bitSet = new BitSet(); } }
From source file:org.aotorrent.common.protocol.tracker.UDPAnnounceReply.java
License:Apache License
public UDPAnnounceReply(ByteBuf in) throws ProtocolException, UnknownHostException { int length = in.readableBytes(); int action = in.readInt(); if (action != UDPAnnounceReply.action || length < 20) { throw new ProtocolException("Invalid packet"); }// ww w . j a v a 2 s . c om transactionId = in.readInt(); interval = in.readInt(); in.readInt(); //leechers in.readInt(); //seeders final int peerCount = in.readableBytes() / 6; peers = new ArrayList<>(peerCount); for (int i = 0; i < peerCount; i++) { int ip = in.readInt(); int port = in.readUnsignedShort(); InetAddress inetAddress = InetAddress.getByAddress(Ints.toByteArray(ip)); peers.add(new InetSocketAddress(inetAddress, port)); } }
From source file:org.apache.cassandra.transport.CBUtil.java
License:Apache License
public static String readString(ByteBuf cb) { try {/*w w w .j av a2 s .com*/ int length = cb.readUnsignedShort(); return readString(cb, length); } catch (IndexOutOfBoundsException e) { throw new ProtocolException( "Not enough bytes to read an UTF8 serialized string preceded by its 2 bytes length"); } }
From source file:org.apache.cassandra.transport.CBUtil.java
License:Apache License
public static byte[] readBytes(ByteBuf cb) { try {/*from w ww . j a v a 2 s.co m*/ int length = cb.readUnsignedShort(); byte[] bytes = new byte[length]; cb.readBytes(bytes); return bytes; } catch (IndexOutOfBoundsException e) { throw new ProtocolException("Not enough bytes to read a byte array preceded by its 2 bytes length"); } }
From source file:org.apache.cassandra.transport.CBUtil.java
License:Apache License
public static Map<String, ByteBuffer> readBytesMap(ByteBuf cb) { int length = cb.readUnsignedShort(); Map<String, ByteBuffer> m = new HashMap<>(length); for (int i = 0; i < length; i++) { String k = readString(cb); ByteBuffer v = readValue(cb); m.put(k, v);// w ww . j a v a 2 s. co m } return m; }
From source file:org.apache.cassandra.transport.CBUtil.java
License:Apache License
public static Map<String, String> readStringMap(ByteBuf cb) { int length = cb.readUnsignedShort(); Map<String, String> m = new HashMap<String, String>(length); for (int i = 0; i < length; i++) { String k = readString(cb); String v = readString(cb); m.put(k, v);//from www. ja v a 2 s.com } return m; }
From source file:org.apache.cassandra.transport.CBUtil.java
License:Apache License
public static List<ByteBuffer> readValueList(ByteBuf cb, int protocolVersion) { int size = cb.readUnsignedShort(); if (size == 0) return Collections.<ByteBuffer>emptyList(); List<ByteBuffer> l = new ArrayList<ByteBuffer>(size); for (int i = 0; i < size; i++) l.add(readBoundValue(cb, protocolVersion)); return l;// w w w .j av a 2 s . c om }