List of usage examples for io.netty.buffer ByteBuf isReadable
public abstract boolean isReadable();
From source file:org.onosproject.ovsdb.lib.utils.JsonRpcReaderUtil.java
License:Apache License
/** * Filter the invalid characters before decoding. * @param in input of bytes//w ww. j a v a2 s . c o m * @param lastReadBytes the bytes for last decoding incomplete record */ private static void fliterCharaters(ByteBuf in) { while (in.isReadable()) { int ch = in.getByte(in.readerIndex()); if ((ch != ' ') && (ch != '\n') && (ch != '\t') && (ch != '\r')) { break; } else { in.readByte(); } } }
From source file:org.opendaylight.capwap.binding_802_11.MsgElem802_11Factory.java
License:Open Source License
static public WTP_Radio_Information decodeWtpRadioInfoElm(ByteBuf buf, int length) { if (buf == null) { LOG.error("ByteBuf null WtpRadioInfoElm "); return null; }//from www .j a v a 2s . c om if (!buf.isReadable()) { LOG.error("ByteBuf not readable WtpRadioInfoElm"); return null; } WTP_Radio_Information radioInfo = new WTP_Radio_Information(); radioInfo.setRadioId(buf.readByte()); buf.skipBytes(3); radioInfo.setRadioType(buf.readByte()); return radioInfo; }
From source file:org.opendaylight.capwap.binding_802_11.MsgElem802_11Factory.java
License:Open Source License
static public AddWlan decodeAddWlan(ByteBuf buf, int length) { if (buf == null) { LOG.error("ByteBuf null AddWlan "); return null; }/*from w w w .ja va 2 s . c om*/ if (!buf.isReadable()) { LOG.error("ByteBuf not readable AddWlan"); return null; } int startIndex = buf.readerIndex(); AddWlan addWlan = new AddWlan(); addWlan.setRadioId(buf.readByte()); addWlan.setWlanId(buf.readByte()); byte[] capability = new byte[] { 0, 0 }; buf.readBytes(capability); addWlan.setCapability(ByteManager.byteArrayToUnsingedShort(capability)); addWlan.setKeyIndex(buf.readByte()); addWlan.setKeyStatus(buf.readByte()); byte[] keyLength = new byte[] { 0, 0 }; buf.readBytes(keyLength); addWlan.setKeyLength(ByteManager.byteArrayToUnsingedShort(keyLength)); byte[] key = new byte[addWlan.getKeyLength()]; buf.readBytes(key); addWlan.setKey(key); byte[] groupTsc = new byte[6]; buf.readBytes(groupTsc); addWlan.setGroupTsc(groupTsc); addWlan.setQos(buf.readByte()); addWlan.setAuthType(buf.readByte()); addWlan.setMacMode(buf.readByte()); addWlan.setTunnelMode(buf.readByte()); addWlan.setSuppressSSID(buf.readByte()); int currentReadBytes = buf.readerIndex() - startIndex; System.out.println(" decode AddWlan - current bytes = " + currentReadBytes); byte[] ssId = new byte[length - currentReadBytes]; buf.readBytes(ssId); addWlan.setSsId(ssId); return (addWlan); //radioInfo.setRadioType(buf.readByte()); //return radioInfo; }
From source file:org.opendaylight.capwap.ODLCapwapMessageElementFactory.java
License:Open Source License
public static ArrayList<ODLCapwapMessageElement> decodeFromByteBuf(ByteBuf bbuf) { ArrayList<ODLCapwapMessageElement> tmp = new ArrayList<ODLCapwapMessageElement>(); while (bbuf.isReadable()) { ODLCapwapMessageElement element = ODLCapwapMessageElement.decodeFromByteBuf(bbuf); if (element == null) { System.out.println("Error in message element parsing"); tmp = null; // TODO - Check return tmp; }//www . j a v a 2s . c o m tmp.add(element); } return tmp; }
From source file:org.opendaylight.controller.netconf.nettyutil.handler.ChunkedFramingMechanismEncoder.java
License:Open Source License
@Override protected void encode(final ChannelHandlerContext ctx, final ByteBuf msg, final ByteBuf out) { do {/* w ww . j a v a 2s. c o m*/ final int xfer = Math.min(chunkSize, msg.readableBytes()); out.writeBytes(NetconfMessageConstants.START_OF_CHUNK); out.writeBytes(String.valueOf(xfer).getBytes(Charsets.US_ASCII)); out.writeByte('\n'); out.writeBytes(msg, xfer); } while (msg.isReadable()); out.writeBytes(NetconfMessageConstants.END_OF_CHUNK); }
From source file:org.opendaylight.controller.netconf.nettyutil.handler.NetconfChunkAggregator.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws IllegalStateException { while (in.isReadable()) { switch (state) { case HEADER_ONE: { final byte b = in.readByte(); checkNewLine(b, "Malformed chunk header encountered (byte 0)"); state = State.HEADER_TWO; initChunk();/* w w w .ja v a 2 s . c om*/ break; } case HEADER_TWO: { final byte b = in.readByte(); checkHash(b, "Malformed chunk header encountered (byte 1)"); state = State.HEADER_LENGTH_FIRST; break; } case HEADER_LENGTH_FIRST: { final byte b = in.readByte(); chunkSize = processHeaderLengthFirst(b); state = State.HEADER_LENGTH_OTHER; break; } case HEADER_LENGTH_OTHER: { final byte b = in.readByte(); if (b == '\n') { state = State.DATA; break; } if (b < '0' || b > '9') { LOG.debug(GOT_PARAM_WHILE_WAITING_FOR_PARAM_PARAM, b, (byte) '0', (byte) '9'); throw new IllegalStateException("Invalid chunk size encountered"); } chunkSize *= 10; chunkSize += b - '0'; checkChunkSize(); break; } case DATA: /* * FIXME: this gathers all data into one big chunk before passing * it on. Make sure the pipeline can work with partial data * and then change this piece to pass the data on as it * comes through. */ if (in.readableBytes() < chunkSize) { LOG.debug("Buffer has {} bytes, need {} to complete chunk", in.readableBytes(), chunkSize); in.discardReadBytes(); return; } aggregateChunks(in.readBytes((int) chunkSize)); state = State.FOOTER_ONE; break; case FOOTER_ONE: { final byte b = in.readByte(); checkNewLine(b, "Malformed chunk footer encountered (byte 0)"); state = State.FOOTER_TWO; chunkSize = 0; break; } case FOOTER_TWO: { final byte b = in.readByte(); checkHash(b, "Malformed chunk footer encountered (byte 1)"); state = State.FOOTER_THREE; break; } case FOOTER_THREE: { final byte b = in.readByte(); // In this state, either header-of-new-chunk or message-end is expected // Depends on the next character extractNewChunkOrMessageEnd(b); break; } case FOOTER_FOUR: { final byte b = in.readByte(); checkNewLine(b, "Malformed chunk footer encountered (byte 3)"); state = State.HEADER_ONE; out.add(chunk); chunk = null; break; } } } in.discardReadBytes(); }
From source file:org.opendaylight.controller.netconf.nettyutil.handler.NetconfEXIToMessageDecoder.java
License:Open Source License
@Override protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out) throws EXIOptionsException, IOException, SAXException, TransformerConfigurationException { /*//from ww w .j a v a 2s . com * Note that we could loop here and process all the messages, but we can't do that. * The reason is <stop-exi> operation, which has the contract of immediately stopping * the use of EXI, which means the next message needs to be decoded not by us, but rather * by the XML decoder. */ // If empty Byte buffer is passed to r.parse, EOFException is thrown if (!in.isReadable()) { LOG.debug("No more content in incoming buffer."); return; } if (LOG.isTraceEnabled()) { LOG.trace("Received to decode: {}", ByteBufUtil.hexDump(in)); } final TransformerHandler handler = FACTORY.newTransformerHandler(); reader.setContentHandler(handler); final DOMResult domResult = new DOMResult(); handler.setResult(domResult); try (final InputStream is = new ByteBufInputStream(in)) { // Performs internal reset before doing anything reader.parse(new InputSource(is)); } out.add(new NetconfMessage((Document) domResult.getNode())); }
From source file:org.opendaylight.controller.netconf.nettyutil.handler.NetconfXMLToMessageDecoder.java
License:Open Source License
@Override public void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out) throws IOException, SAXException { if (in.isReadable()) { if (LOG.isTraceEnabled()) { LOG.trace("Received to decode: {}", ByteBufUtil.hexDump(in)); }//from w w w . j a v a 2s . co m out.add(new NetconfMessage(XmlUtil.readXmlToDocument(new ByteBufInputStream(in)))); } else { LOG.debug("No more content in incoming buffer."); } }
From source file:org.opendaylight.controller.netconf.util.handler.NetconfChunkAggregator.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { while (in.isReadable()) { switch (state) { case HEADER_ONE: { final byte b = in.readByte(); if (b != '\n') { logger.debug("Got byte {} while waiting for {}", b, (byte) '\n'); throw new IllegalStateException("Malformed chunk header encountered (byte 0)"); }/*from w w w . ja v a 2 s .co m*/ state = State.HEADER_TWO; initChunk(); break; } case HEADER_TWO: { final byte b = in.readByte(); if (b != '#') { logger.debug("Got byte {} while waiting for {}", b, (byte) '#'); throw new IllegalStateException("Malformed chunk header encountered (byte 1)"); } state = State.HEADER_LENGTH_FIRST; break; } case HEADER_LENGTH_FIRST: { final byte b = in.readByte(); chunkSize = processHeaderLengthFirst(b); state = State.HEADER_LENGTH_OTHER; break; } case HEADER_LENGTH_OTHER: { final byte b = in.readByte(); if (b == '\n') { state = State.DATA; break; } if (b < '0' || b > '9') { logger.debug("Got byte {} while waiting for {}-{}", b, (byte) '0', (byte) '9'); throw new IllegalStateException("Invalid chunk size encountered"); } chunkSize *= 10; chunkSize += b - '0'; if (chunkSize > maxChunkSize) { logger.debug("Parsed chunk size {}, maximum allowed is {}", chunkSize, maxChunkSize); throw new IllegalStateException("Maximum chunk size exceeded"); } break; } case DATA: /* * FIXME: this gathers all data into one big chunk before passing * it on. Make sure the pipeline can work with partial data * and then change this piece to pass the data on as it * comes through. */ if (in.readableBytes() < chunkSize) { logger.debug("Buffer has {} bytes, need {} to complete chunk", in.readableBytes(), chunkSize); in.discardReadBytes(); return; } aggregateChunks(in.readBytes((int) chunkSize)); state = State.FOOTER_ONE; break; case FOOTER_ONE: { final byte b = in.readByte(); if (b != '\n') { logger.debug("Got byte {} while waiting for {}", b, (byte) '\n'); throw new IllegalStateException("Malformed chunk footer encountered (byte 0)"); } state = State.FOOTER_TWO; chunkSize = 0; break; } case FOOTER_TWO: { final byte b = in.readByte(); if (b != '#') { logger.debug("Got byte {} while waiting for {}", b, (byte) '#'); throw new IllegalStateException("Malformed chunk footer encountered (byte 1)"); } state = State.FOOTER_THREE; break; } case FOOTER_THREE: { final byte b = in.readByte(); // In this state, either header-of-new-chunk or message-end is expected // Depends on the next character if (isHeaderLengthFirst(b)) { // Extract header length#1 from new chunk chunkSize = processHeaderLengthFirst(b); // Proceed with next chunk processing state = State.HEADER_LENGTH_OTHER; } else if (b == '#') { state = State.FOOTER_FOUR; } else { logger.debug("Got byte {} while waiting for {} or {}-{}", b, (byte) '#', (byte) '1', (byte) '9'); throw new IllegalStateException("Malformed chunk footer encountered (byte 2)"); } break; } case FOOTER_FOUR: { final byte b = in.readByte(); if (b != '\n') { logger.debug("Got byte {} while waiting for {}", b, (byte) '\n'); throw new IllegalStateException("Malformed chunk footer encountered (byte 3)"); } state = State.HEADER_ONE; out.add(chunk); chunk = null; break; } } } in.discardReadBytes(); }
From source file:org.opendaylight.controller.netconf.util.handler.NetconfMessageChunkDecoder.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { ByteBuf byteBufMsg = Unpooled.buffer(in.readableBytes()); int chunkSize = -1; boolean isParsed = false; while (in.isReadable()) { try {/*w w w . j av a2s. c o m*/ if (!isParsed) { chunkSize = readHeader(in); isParsed = true; } if (chunkSize != -1 && isParsed) { in.readBytes(byteBufMsg, chunkSize); isParsed = false; } else { throw new DeserializerException("Unable to parse chunked data or header."); } } catch (Exception e) { logger.error("Failed to decode chunked message.", e); this.exceptionCaught(ctx, e); } } out.add(byteBufMsg); isParsed = false; }