List of usage examples for io.netty.buffer ByteBuf clear
public abstract ByteBuf clear();
From source file:org.l2junity.loginserver.network.client.crypt.Crypt.java
License:Open Source License
@Override public void decrypt(ByteBuf buf) { // Packet size must be multiple of 8 if ((buf.readableBytes() % 8) != 0) { buf.clear(); return;//from w ww. ja v a 2 s . com } final byte[] block = new byte[8]; while (buf.isReadable(8)) { buf.readBytes(block); _blowfishEngine.decryptBlock(block, 0); buf.setBytes(buf.readerIndex() - block.length, block); } // verify checksum also dont forget! }
From source file:org.l2junity.network.codecs.PacketEncoder.java
License:Open Source License
@Override protected void encode(ChannelHandlerContext ctx, IOutgoingPacket packet, ByteBuf out) { if (out.order() != _byteOrder) { out = out.order(_byteOrder);//from w w w .j a va 2 s . co m } try { if (packet.write(new PacketWriter(out))) { if (out.writerIndex() > _maxPacketSize) { throw new IllegalStateException("Packet (" + packet + ") size (" + out.writerIndex() + ") is bigger than the limit (" + _maxPacketSize + ")"); } } else { // Avoid sending the packet out.clear(); } } catch (Throwable e) { LOGGER.warn("Failed sending Packet({})", packet, e); // Avoid sending the packet if some exception happened out.clear(); } }
From source file:org.neo4j.bolt.transport.TransportSelectionHandler.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { // Will use the first five bytes to detect a protocol. if (in.readableBytes() < 5) { return;//from w w w . j a v a2 s . co m } if (detectSsl(in)) { enableSsl(ctx); } else if (isHttp(in)) { switchToWebsocket(ctx); } else if (isBoltPreamble(in)) { switchToSocket(ctx); } else { // TODO: send a alert_message for a ssl connection to terminate the handshake in.clear(); ctx.close(); } }
From source file:org.opendaylight.bgp.concepts.NextHopUtilTest.java
License:Open Source License
@Test public void testSerializeNextHop() { CNextHop hop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(new Ipv4NextHopBuilder().setGlobal(ipv4).build()) .build();//from w ww .ja v a2 s .co m final ByteBuf buffer = Unpooled.buffer(); NextHopUtil.serializeNextHop(hop, buffer); assertArrayEquals(ipv4B, ByteArray.readAllBytes(buffer)); hop = new Ipv6NextHopCaseBuilder().setIpv6NextHop(new Ipv6NextHopBuilder().setGlobal(ipv6).build()).build(); buffer.clear(); NextHopUtil.serializeNextHop(hop, buffer); assertArrayEquals(ipv6B, ByteArray.readAllBytes(buffer)); hop = new Ipv6NextHopCaseBuilder() .setIpv6NextHop(new Ipv6NextHopBuilder().setGlobal(ipv6).setLinkLocal(ipv6l).build()).build(); buffer.clear(); NextHopUtil.serializeNextHop(hop, buffer); assertArrayEquals(ipv6lB, ByteArray.readAllBytes(buffer)); buffer.clear(); hop = new Ipv6NextHopCaseBuilder().setIpv6NextHop(new Ipv6NextHopBuilder().setLinkLocal(ipv6l).build()) .build(); buffer.clear(); try { NextHopUtil.serializeNextHop(hop, buffer); fail("Exception should happen"); } catch (final IllegalArgumentException e) { assertEquals("Ipv6 Next Hop is missing Global address.", e.getMessage()); } }
From source file:org.opendaylight.ocpjava.protocol.impl.core.OCPEncoder.java
License:Open Source License
@Override protected void encode(ChannelHandlerContext ctx, MessageListenerWrapper wrapper, ByteBuf out) throws Exception { LOG.trace("Encoding"); try {//w w w. j av a 2s.co m serializationFactory.messageToBuffer((short) 1, out, wrapper.getMsg()); statisticsCounters.incrementCounter(CounterEventTypes.DS_ENCODE_SUCCESS); } catch (Exception e) { LOG.warn("Message serialization failed ", e); statisticsCounters.incrementCounter(CounterEventTypes.DS_ENCODE_FAIL); Future<Void> newFailedFuture = ctx.newFailedFuture(e); wrapper.getListener().operationComplete(newFailedFuture); out.clear(); return; } }
From source file:org.opendaylight.openflowjava.protocol.impl.core.OFEncoder.java
License:Open Source License
@Override protected void encode(ChannelHandlerContext ctx, OfHeader msg, ByteBuf out) throws Exception { LOGGER.trace("Encoding"); try {/*from w ww . j a v a 2s . c o m*/ SerializationFactory.messageToBuffer(msg.getVersion(), out, msg); } catch (Exception e) { LOGGER.error("Message serialization failed"); LOGGER.error(e.getMessage(), e); out.clear(); return; } if (out.readableBytes() > 0) { out.retain(); ctx.writeAndFlush(out); } else { LOGGER.warn("Translated buffer is empty"); } }
From source file:org.opendaylight.protocol.bgp.parser.impl.BgpExtendedMessageCapabilityHandlerTest.java
License:Open Source License
@Test public void testBgpExtendedMessageCapabilityHandler() throws BGPDocumentedException, BGPParsingException { final BgpExtendedMessageCapabilityHandler handler = new BgpExtendedMessageCapabilityHandler(); final byte[] bgpExeBytes = { (byte) 0x06, (byte) 0x00 }; final ByteBuf buffer = Unpooled.buffer(bgpExeBytes.length); handler.serializeCapability(BgpExtendedMessageUtil.EXTENDED_MESSAGE_CAPABILITY, buffer); Assert.assertArrayEquals(bgpExeBytes, buffer.array()); Assert.assertEquals(handler.parseCapability(Unpooled.wrappedBuffer(bgpExeBytes)), BgpExtendedMessageUtil.EXTENDED_MESSAGE_CAPABILITY); final byte[] bgpExeBytes2 = { (byte) 0x40, (byte) 0x06 }; buffer.clear(); handler.serializeCapability(BgpExtendedMessageUtil.EXTENDED_MESSAGE_CAPABILITY, buffer); Assert.assertNotSame(bgpExeBytes2, buffer.array()); }
From source file:org.opendaylight.protocol.bgp.parser.impl.GracefulCapabilityHandlerTest.java
License:Open Source License
@Test public void testGracefulCapabilityHandler() throws BGPDocumentedException, BGPParsingException { final GracefulCapabilityHandler handler = new GracefulCapabilityHandler(this.ctx.getAddressFamilyRegistry(), this.ctx.getSubsequentAddressFamilyRegistry()); final byte[] capaBytes = { (byte) 0x40, (byte) 0x06, (byte) 0x81, (byte) 0xf4, (byte) 0x00, (byte) 0x01, (byte) 0x01, (byte) 0x80 }; final GracefulRestartCapabilityBuilder capaBuilder = new GracefulRestartCapabilityBuilder(); capaBuilder.setRestartFlags(new RestartFlags(true)); capaBuilder.setRestartTime(500);//from www. j av a 2 s. c o m final TablesBuilder tablesBuilder = new TablesBuilder(); tablesBuilder.setAfiFlags(new AfiFlags(true)); tablesBuilder.setAfi(Ipv4AddressFamily.class); tablesBuilder.setSafi(UnicastSubsequentAddressFamily.class); capaBuilder.setTables(Lists.newArrayList(tablesBuilder.build())); final ByteBuf buffer = Unpooled.buffer(capaBytes.length); handler.serializeCapability(new CParametersBuilder() .addAugmentation(CParameters1.class, new CParameters1Builder().setGracefulRestartCapability(capaBuilder.build()).build()) .build(), buffer); Assert.assertArrayEquals(capaBytes, buffer.array()); Assert.assertEquals( new CParametersBuilder() .addAugmentation(CParameters1.class, new CParameters1Builder().setGracefulRestartCapability(capaBuilder.build()).build()) .build(), handler.parseCapability(Unpooled.wrappedBuffer(capaBytes).slice(2, capaBytes.length - 2))); final byte[] capaBytes2 = { (byte) 0x40, (byte) 0x06, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x01, (byte) 0x00 }; capaBuilder.setRestartFlags(new RestartFlags(false)); capaBuilder.setRestartTime(0); tablesBuilder.setAfiFlags(new AfiFlags(false)); capaBuilder.setTables(Lists.newArrayList(tablesBuilder.build())); buffer.clear(); handler.serializeCapability(new CParametersBuilder() .addAugmentation(CParameters1.class, new CParameters1Builder().setGracefulRestartCapability(capaBuilder.build()).build()) .build(), buffer); Assert.assertArrayEquals(capaBytes2, buffer.array()); Assert.assertEquals( new CParametersBuilder() .addAugmentation(CParameters1.class, new CParameters1Builder().setGracefulRestartCapability(capaBuilder.build()).build()) .build(), handler.parseCapability(Unpooled.wrappedBuffer(capaBytes2).slice(2, capaBytes2.length - 2))); capaBuilder.setRestartFlags(null); tablesBuilder.setAfiFlags(null); capaBuilder.setRestartTime(null); capaBuilder.setTables(Lists.newArrayList(tablesBuilder.build())); buffer.clear(); handler.serializeCapability(new CParametersBuilder() .addAugmentation(CParameters1.class, new CParameters1Builder().setGracefulRestartCapability(capaBuilder.build()).build()) .build(), buffer); Assert.assertArrayEquals(capaBytes2, buffer.array()); final byte[] capaBytes3 = { (byte) 0x40, (byte) 0x06, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 }; capaBuilder.setRestartFlags(new RestartFlags(false)); capaBuilder.setRestartTime(0); capaBuilder.setTables(Collections.<Tables>emptyList()); Assert.assertEquals( new CParametersBuilder() .addAugmentation(CParameters1.class, new CParameters1Builder().setGracefulRestartCapability(capaBuilder.build()).build()) .build(), handler.parseCapability(Unpooled.wrappedBuffer(capaBytes3).slice(2, capaBytes3.length - 2))); final byte[] capaBytes4 = { (byte) 0x40, (byte) 0x06, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00 }; Assert.assertEquals( new CParametersBuilder() .addAugmentation(CParameters1.class, new CParameters1Builder().setGracefulRestartCapability(capaBuilder.build()).build()) .build(), handler.parseCapability(Unpooled.wrappedBuffer(capaBytes4).slice(2, capaBytes4.length - 2))); final byte[] capaBytes5 = { (byte) 0x40, (byte) 0x02, (byte) 0x00, (byte) 0x00 }; final ByteBuf b = Unpooled.buffer(capaBytes5.length); handler.serializeCapability(new CParametersBuilder() .addAugmentation(CParameters1.class, new CParameters1Builder() .setGracefulRestartCapability(new GracefulRestartCapabilityBuilder().build()).build()) .build(), b); Assert.assertArrayEquals(capaBytes5, b.array()); Assert.assertEquals( new CParametersBuilder() .addAugmentation(CParameters1.class, new CParameters1Builder() .setGracefulRestartCapability(new GracefulRestartCapabilityBuilder() .setRestartFlags(new RestartFlags(Boolean.FALSE)).setRestartTime(0) .setTables(Collections.<Tables>emptyList()).build()) .build()) .build(), handler.parseCapability(Unpooled.wrappedBuffer(capaBytes5).slice(2, capaBytes5.length - 2))); }
From source file:org.opendaylight.protocol.bgp.parser.impl.ParserTest.java
License:Open Source License
@Test public void testNotificationMsg() throws BGPParsingException, BGPDocumentedException { Notification notMsg = new NotifyBuilder().setErrorCode(BGPError.OPT_PARAM_NOT_SUPPORTED.getCode()) .setErrorSubcode(BGPError.OPT_PARAM_NOT_SUPPORTED.getSubcode()).setData(new byte[] { 4, 9 }) .build();// w w w .j a v a 2s . c o m final ByteBuf bytes = Unpooled.buffer(); ParserTest.reg.serializeMessage(notMsg, bytes); assertArrayEquals(notificationBMsg, ByteArray.subByte(bytes.array(), 0, bytes.writerIndex())); Notification m = ParserTest.reg.parseMessage(Unpooled.copiedBuffer(bytes)); assertTrue(m instanceof Notify); assertEquals(BGPError.OPT_PARAM_NOT_SUPPORTED, BGPError.forValue(((Notify) m).getErrorCode(), ((Notify) m).getErrorSubcode())); assertArrayEquals(new byte[] { 4, 9 }, ((Notify) m).getData()); notMsg = new NotifyBuilder().setErrorCode(BGPError.CONNECTION_NOT_SYNC.getCode()) .setErrorSubcode(BGPError.CONNECTION_NOT_SYNC.getSubcode()).build(); bytes.clear(); ParserTest.reg.serializeMessage(notMsg, bytes); m = ParserTest.reg.parseMessage(Unpooled.copiedBuffer(bytes)); assertTrue(m instanceof Notify); assertEquals(BGPError.CONNECTION_NOT_SYNC, BGPError.forValue(((Notify) m).getErrorCode(), ((Notify) m).getErrorSubcode())); assertNull(((Notify) m).getData()); }
From source file:org.opendaylight.protocol.pcep.spi.UtilsTest.java
License:Open Source License
@Test public void testXROSubobjectUtil() { byte[] expected = { (byte) 0x82, 6, 0, 1, 2, 3 }; final ByteBuf out = Unpooled.buffer(); final ByteBuf body = Unpooled.copiedBuffer(new byte[] { 0, 1, 2, 3 }); body.markReaderIndex();/*from ww w. j a va 2 s. co m*/ XROSubobjectUtil.formatSubobject(2, true, body, out); assertArrayEquals(expected, ByteArray.getAllBytes(out)); expected = new byte[] { 2, 6, 0, 1, 2, 3 }; out.clear(); body.resetReaderIndex(); XROSubobjectUtil.formatSubobject(2, false, body, out); assertArrayEquals(expected, ByteArray.getAllBytes(out)); out.clear(); body.resetReaderIndex(); XROSubobjectUtil.formatSubobject(2, null, body, out); assertArrayEquals(expected, ByteArray.getAllBytes(out)); }