List of usage examples for io.netty.buffer ByteBuf resetReaderIndex
public abstract ByteBuf resetReaderIndex();
From source file:org.opendaylight.netconf.nettyutil.handler.ssh.client.AsyncSshHandlerWriter.java
License:Open Source License
private static Buffer toBuffer(final ByteBuf msg) { // TODO Buffer vs ByteBuf translate, Can we handle that better ? msg.resetReaderIndex(); final byte[] temp = new byte[msg.readableBytes()]; msg.readBytes(temp, 0, msg.readableBytes()); return new Buffer(temp); }
From source file:org.opendaylight.protocol.pcep.spi.UtilsTest.java
License:Open Source License
@Test public void testLabelUtil() { final byte[] expected = { (byte) 0x81, 0x04, 0x01, 0x02, 0x03, 0x04 }; final ByteBuf out = Unpooled.buffer(); final ByteBuf body = Unpooled.copiedBuffer(new byte[] { 1, 2, 3, 4 }); LabelUtil.formatLabel(4, true, true, body, out); assertArrayEquals(expected, ByteArray.readAllBytes(out)); final byte[] ex = { 0, 0x05, 0x01, 0x02, 0x03, 0x04 }; body.resetReaderIndex(); LabelUtil.formatLabel(5, null, null, body, out); assertArrayEquals(ex, ByteArray.getAllBytes(out)); }
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();/*w w w . ja v a 2s. com*/ 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)); }
From source file:org.opendaylight.protocol.pcep.spi.UtilsTest.java
License:Open Source License
@Test public void testEROSubobjectUtil() { 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();/* w ww. j a v a 2s . com*/ EROSubobjectUtil.formatSubobject(2, true, body, out); assertArrayEquals(expected, ByteArray.getAllBytes(out)); expected = new byte[] { 2, 6, 0, 1, 2, 3 }; out.clear(); body.resetReaderIndex(); EROSubobjectUtil.formatSubobject(2, false, body, out); assertArrayEquals(expected, ByteArray.getAllBytes(out)); }
From source file:org.opendaylight.sxp.core.messaging.MessageFactory.java
License:Open Source License
/** * Decode received message into specific message type * * @param version Version used for decoding * @param request ByteBuf containing data to be decoded * @return Decoded message/*from w w w . j a v a 2 s . c o m*/ * @throws ErrorMessageException If version Mismatch occurs * @throws UnknownSxpMessageTypeException If data contains unsupported message * @throws AddressLengthException If address length of some attribute is incorrect * @throws UnknownHostException If some attribute have incorrect or none address * @throws AttributeLengthException If length of some attribute is incorrect * @throws TlvNotFoundException If Tvl isn't found * @throws UnknownPrefixException If some attribute has incorrect or none Prefix * @throws UnknownNodeIdException If NodeId isn't found or is incorrect */ public static Notification parse(Version version, ByteBuf request) throws ErrorMessageException, UnknownSxpMessageTypeException, AddressLengthException, UnknownHostException, AttributeLengthException, TlvNotFoundException, UnknownPrefixException, UnknownNodeIdException, AttributeVariantException { request.resetReaderIndex(); byte[] headerLength, headerType, payload; int messageLength; try { headerLength = new byte[MESSAGE_HEADER_LENGTH_LENGTH]; request = request.readBytes(headerLength); headerType = new byte[MESSAGE_HEADER_TYPE_LENGTH]; request = request.readBytes(headerType); messageLength = ArraysUtil.bytes2int(headerLength); int payloadLength = messageLength - (MESSAGE_HEADER_LENGTH_LENGTH + Configuration.getConstants().getMessageHeaderTypeLength()); payload = new byte[payloadLength]; request = request.readBytes(payload); } catch (IndexOutOfBoundsException | NegativeArraySizeException e) { throw new ErrorMessageException(ErrorCode.MessageHeaderError, e); } validate(headerLength.length + headerType.length, payload.length, messageLength); return decode(version, headerType, payload); }
From source file:org.opendaylight.sxp.core.messaging.MessageFactory.java
License:Open Source License
/** * @param message Notification to be proceed * @return Gets String representation of ByteBuf *///w w w .j a va 2 s . co m public static String toString(ByteBuf message) { message.resetReaderIndex(); byte[] _message = new byte[message.readableBytes()]; message.readBytes(_message); message.resetReaderIndex(); return toString(_message); }
From source file:org.quartzpowered.network.codec.FrameCodec.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { // check for length field readability in.markReaderIndex();//from w w w .j a v a 2 s .c o m if (!readableVarInt(in)) { return; } // check for contents readability Buffer buffer = new Buffer(in); int length = buffer.readVarInt(); if (in.readableBytes() < length) { in.resetReaderIndex(); return; } // read contents into buf out.add(in.readBytes(length)); }
From source file:org.ratpackframework.groovy.templating.internal.TemplateParser.java
License:Apache License
public void parse(ByteBuf input, ByteBuf output) throws IOException { startScript(output);//from ww w.j av a 2 s. c o m byte c; while (input.isReadable()) { c = input.readByte(); if (c == '<') { input.markReaderIndex(); c = input.readByte(); if (c != '%') { output.writeBytes(LESS_THAN); input.resetReaderIndex(); } else { input.markReaderIndex(); c = input.readByte(); if (c == '=') { groovyExpression(input, output); } else { input.resetReaderIndex(); groovySection(input, output); } } continue; // at least '<' is consumed ... read next chars. } if (c == '$') { input.markReaderIndex(); c = input.readByte(); if (c != '{') { output.writeBytes(DOLLAR); input.resetReaderIndex(); } else { input.markReaderIndex(); processGSstring(input, output); } continue; // at least '$' is consumed ... read next chars. } if (c == '\"') { output.writeBytes(BACKSLASH); } // Handle raw new line characters. if (c == '\n' || c == '\r') { if (c == '\r') { // on Windows, "\r\n" is a new line. input.markReaderIndex(); c = input.readByte(); if (c != '\n') { input.resetReaderIndex(); } } output.writeByte('\n'); continue; } output.writeByte(c); } endScript(output); }
From source file:org.ratpackframework.http.internal.DefaultRequest.java
License:Apache License
@Override public void writeBodyTo(OutputStream destination) throws IOException { ByteBuf buffer = getBuffer(); buffer.resetReaderIndex(); buffer.readBytes(destination, buffer.writerIndex()); }
From source file:org.royaldev.enterprise.proxy.ProxyBackendHandler.java
License:Apache License
@Override public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exception { this.showHierarchy(msg.getClass()); if (msg instanceof ByteBuf) { final ByteBuf bf = (ByteBuf) msg; /*final GenericPacket gp = new GenericPacket(bf); switch (gp.getID()) {/* w ww.j a v a 2 s . c om*/ case PacketType.Server.CHAT: final ServerPacket04Chat spc = new ServerPacket04Chat(gp); break; }*/ bf.resetReaderIndex(); } inboundChannel.writeAndFlush(msg).addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) ctx.channel().read(); else future.channel().close(); } }); }