List of usage examples for io.netty.buffer ByteBuf readByte
public abstract byte readByte();
From source file:io.hekate.cluster.seed.multicast.MulticastSeedNodeProvider.java
License:Apache License
private SimpleChannelInboundHandler<DatagramPacket> createListenerHandler(SeedNode thisNode, ByteBuf seedNodeInfo) {// ww w.j a v a 2 s .c o m return new SimpleChannelInboundHandler<DatagramPacket>() { @Override public void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception { ByteBuf buf = msg.content(); if (buf.readableBytes() > 4 && buf.readInt() == Utils.MAGIC_BYTES) { MessageTYpe msgType = MessageTYpe.values()[buf.readByte()]; if (msgType == MessageTYpe.DISCOVERY) { String cluster = decodeUtf(buf); InetSocketAddress address = decodeAddress(buf); if (thisNode.cluster().equals(cluster) && !address.equals(thisNode.address())) { onDiscoveryMessage(address); DatagramPacket response = new DatagramPacket(seedNodeInfo.copy(), msg.sender()); ctx.writeAndFlush(response); } } } } }; }
From source file:io.hekate.cluster.seed.multicast.MulticastSeedNodeProvider.java
License:Apache License
private InetSocketAddress decodeAddress(ByteBuf buf) throws UnknownHostException { byte[] addrBytes = new byte[buf.readByte()]; buf.readBytes(addrBytes);//from ww w .ja va 2 s.c o m int port = buf.readInt(); return new InetSocketAddress(InetAddress.getByAddress(addrBytes), port); }
From source file:io.hydramq.core.type.converters.MessagePropertiesConverter.java
License:Open Source License
@Override public MessageProperties read(final ConversionContext context, final ByteBuf buffer) { MessageProperties properties = new MessageProperties(); int flags = buffer.readInt(); if ((flags & HAS_STRING_PROPERTIES) == HAS_STRING_PROPERTIES) { int count = buffer.readInt(); for (int i = 0; i < count; i++) { properties.setString(context.read(String.class, buffer), context.read(String.class, buffer)); }//from w ww . j a v a 2s . co m } if ((flags & HAS_BOOLEAN_PROPERTIES) == HAS_BOOLEAN_PROPERTIES) { int count = buffer.readInt(); for (int i = 0; i < count; i++) { properties.setBoolean(context.read(String.class, buffer), buffer.readBoolean()); } } if ((flags & HAS_INTEGER_PROPERTIES) == HAS_INTEGER_PROPERTIES) { int count = buffer.readInt(); for (int i = 0; i < count; i++) { properties.setInteger(context.read(String.class, buffer), buffer.readInt()); } } if ((flags & HAS_LONG_PROPERTIES) == HAS_LONG_PROPERTIES) { int count = buffer.readInt(); for (int i = 0; i < count; i++) { properties.setLong(context.read(String.class, buffer), buffer.readLong()); } } if ((flags & HAS_FLOAT_PROPERTIES) == HAS_FLOAT_PROPERTIES) { int count = buffer.readInt(); for (int i = 0; i < count; i++) { properties.setFloat(context.read(String.class, buffer), buffer.readFloat()); } } if ((flags & HAS_DOUBLE_PROPERTIES) == HAS_DOUBLE_PROPERTIES) { int count = buffer.readInt(); for (int i = 0; i < count; i++) { properties.setDouble(context.read(String.class, buffer), buffer.readDouble()); } } if ((flags & HAS_BYTES_PROPERTIES) == HAS_BYTES_PROPERTIES) { int count = buffer.readInt(); for (int i = 0; i < count; i++) { properties.setBytes(context.read(String.class, buffer), context.read(byte[].class, buffer)); } } if ((flags & HAS_SHORT_PROPERTIES) == HAS_SHORT_PROPERTIES) { int count = buffer.readInt(); for (int i = 0; i < count; i++) { properties.setShort(context.read(String.class, buffer), buffer.readShort()); } } if ((flags & HAS_BYTE_PROPERTIES) == HAS_BYTE_PROPERTIES) { int count = buffer.readInt(); for (int i = 0; i < count; i++) { properties.setByte(context.read(String.class, buffer), buffer.readByte()); } } return properties; }
From source file:io.lettuce.core.protocol.RedisStateMachine.java
License:Apache License
private State.Type readReplyType(ByteBuf buffer) { byte b = buffer.readByte(); switch (b) {/*from ww w .j a v a 2 s . c o m*/ case '+': return SINGLE; case '-': return ERROR; case ':': return INTEGER; case '$': return BULK; case '*': return MULTI; default: throw new RedisException("Invalid first byte: " + Byte.toString(b)); } }
From source file:io.liveoak.stomp.common.StompFrameDecoder.java
License:Open Source License
protected ByteBuf readUntilNull(ByteBuf buffer) { int nonNullBytes = buffer.bytesBefore((byte) 0x00); ByteBuf content = null;/* w w w . j a v a2s. c o m*/ if (nonNullBytes == 0) { content = Unpooled.EMPTY_BUFFER; } else { content = buffer.readBytes(nonNullBytes); } buffer.readByte(); return content; }
From source file:io.liveoak.stomp.common.StompFrameDecoder.java
License:Open Source License
protected ByteBuf readUntil(ByteBuf buffer, int len) { if (buffer.readableBytes() < (len + 1)) { return null; }/*from w w w. j a va2 s. c om*/ ByteBuf content = buffer.readBytes(len); buffer.readByte(); return content; }
From source file:io.liveoak.stomp.common.StompFrameDecoder.java
License:Open Source License
protected FrameHeader decodeHeader(ByteBuf buffer) { FrameHeader header = null;//from w w w . j a va 2 s. c o m while (header == null || buffer.isReadable()) { int nonNewLineBytes = buffer.bytesBefore((byte) '\n'); if (nonNewLineBytes == 0) { buffer.readByte(); break; } if (nonNewLineBytes >= 0) { ByteBuf line = buffer.readBytes(nonNewLineBytes); buffer.readByte(); header = processHeaderLine(header, line.toString(UTF_8)); } } return header; }
From source file:io.moquette.parser.netty.ConnAckEncoderTest.java
License:Open Source License
@Test public void testHeaderEncode() throws Exception { ConnAckMessage msg = new ConnAckMessage(); //Exercise/*from www . ja v a2s .c o m*/ ByteBuf out = Unpooled.buffer(); //Exercise m_encoder.encode(m_mockedContext, msg, out); //Verify assertEquals(0x20, out.readByte()); //1 byte assertEquals(0x02, out.readByte()); //2 byte, length assertEquals(ConnAckMessage.CONNECTION_ACCEPTED, out.skipBytes(1).readByte()); }
From source file:io.moquette.parser.netty.ConnectEncoderTest.java
License:Open Source License
@Test public void testBaseHeader() throws Exception { ConnectMessage msg = new ConnectMessage(); msg.setWillRetain(true);//from w w w . jav a2 s . c om msg.setWillQos((byte) 2); msg.setWillFlag(false); msg.setCleanSession(true); msg.setKeepAlive(512); ByteBuf out = Unpooled.buffer(); //Exercise m_encoder.encode(m_mockedContext, msg, out); //Verify assertEquals(0x10, out.readByte()); //1 byte assertEquals(12, out.readByte()); //remaining length TestUtils.verifyString("MQIsdp", out); assertEquals(0x03, out.readByte()); //protocol version assertEquals(0x32, out.readByte()); //flags assertEquals(2, out.readByte()); //keepAliveTimer msb assertEquals(0, out.readByte()); //keepAliveTimer lsb }
From source file:io.moquette.parser.netty.ConnectEncoderTest.java
License:Open Source License
@Test public void testCompleteHeader() throws Exception { ConnectMessage msg = new ConnectMessage(); msg.setWillRetain(true);//from w w w . ja v a 2 s .c om msg.setWillQos((byte) 2); msg.setWillFlag(true); msg.setCleanSession(true); msg.setKeepAlive(512); //variable part msg.setClientID("ABCDEF"); msg.setWillTopic("Topic"); msg.setWillMessage("Message".getBytes()); ByteBuf out = Unpooled.buffer(); //Exercise m_encoder.encode(m_mockedContext, msg, out); //Verify assertEquals(0x10, out.readByte()); //1 byte assertEquals(36, out.readByte()); //remaining length TestUtils.verifyString("MQIsdp", out); assertEquals(0x03, out.readByte()); //protocol version assertEquals(0x36, out.readByte()); //flags assertEquals(2, out.readByte()); //keepAliveTimer msb assertEquals(0, out.readByte()); //keepAliveTimer lsb //Variable part TestUtils.verifyString("ABCDEF", out); TestUtils.verifyString("Topic", out); TestUtils.verifyString("Message", out); }