Example usage for io.netty.buffer ByteBuf isReadable

List of usage examples for io.netty.buffer ByteBuf isReadable

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf isReadable.

Prototype

public abstract boolean isReadable();

Source Link

Document

Returns true if and only if (this.writerIndex - this.readerIndex) is greater than 0 .

Usage

From source file:org.opendaylight.groupbasedpolicy.jsonrpc.JsonRpcDecoder.java

License:Open Source License

private static void skipSpaces(ByteBuf b) throws IOException {
    while (b.isReadable()) {
        int ch = b.getByte(b.readerIndex()) & 0xFF;
        if (!(ch == ' ' || ch == '\r' || ch == '\n' || ch == '\t')) {
            return;
        }//from  w  w  w. j  a  v a 2  s. c  o m
        b.readByte(); //move the read index
    }
}

From source file:org.opendaylight.netconf.nettyutil.handler.NetconfChunkAggregator.java

License:Open Source License

@Override
protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final 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();//from  ww w  .j ava2s .c o m
            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.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));
        }/*w  w  w.  ja  va  2 s  .c  o m*/

        /* According to the XML 1.0 specifications, when there is an XML declaration
         * at the beginning of an XML document, it is invalid to have
         * white spaces before that declaration (reminder: a XML declaration looks like:
         * <?xml version="1.0" encoding="UTF-8"?>). In contrast, when there is no XML declaration,
         * it is valid to have white spaces at the beginning of the document.
         *
         * When they send a NETCONF message, several NETCONF servers start with a new line (either
         * LF or CRLF), presumably to improve readability in interactive sessions with a human being.
         * Some NETCONF servers send an XML declaration, some others do not.
         *
         * If a server starts a NETCONF message with white spaces and follows with an XML
         * declaration, XmlUtil.readXmlToDocument() will fail because this is invalid XML.
         * But in the spirit of the "NETCONF over SSH" RFC 4742 and to improve interoperability, we want
         * to accept those messages.
         *
         * To do this, the following code strips the leading bytes before the start of the XML messages.
         */

        // Skip all leading whitespaces by moving the reader index to the first non whitespace character
        while (in.isReadable()) {
            if (!isWhitespace(in.readByte())) {
                // return reader index to the first non whitespace character
                in.readerIndex(in.readerIndex() - 1);
                break;
            }
        }

        // Warn about leading whitespaces
        if (in.readerIndex() != 0 && LOG.isWarnEnabled()) {
            final byte[] strippedBytes = new byte[in.readerIndex()];
            in.getBytes(0, strippedBytes, 0, in.readerIndex());
            LOG.warn("XML message with unwanted leading bytes detected. Discarded the {} leading byte(s): '{}'",
                    in.readerIndex(), ByteBufUtil.hexDump(Unpooled.wrappedBuffer(strippedBytes)));
        }
    }
    if (in.isReadable()) {
        out.add(new NetconfMessage(XmlUtil.readXmlToDocument(new ByteBufInputStream(in))));
    } else {
        LOG.debug("No more content in incoming buffer.");
    }
}

From source file:org.opendaylight.ovsdb.lib.jsonrpc.JsonRpcDecoder.java

License:Open Source License

private static void skipSpaces(ByteBuf byteBuf) throws IOException {
    while (byteBuf.isReadable()) {
        int ch = byteBuf.getByte(byteBuf.readerIndex()) & 0xFF;
        if (!(ch == ' ' || ch == '\r' || ch == '\n' || ch == '\t')) {
            return;
        } else {//w  w w .  j  av a  2  s.com
            byteBuf.readByte(); //move the read index
        }
    }
}

From source file:org.opendaylight.protocol.bgp.evpn.impl.attributes.PMSITunnelAttributeHandler.java

License:Open Source License

@Override
public void parseAttribute(@Nonnull final ByteBuf buffer, @Nonnull final AttributesBuilder builder) {
    if (!buffer.isReadable()) {
        return;//from  ww  w . ja  v a 2  s .co  m
    }
    final PmsiTunnelBuilder pmsiTunnelBuilder = new PmsiTunnelBuilder();
    pmsiTunnelBuilder.setLeafInformationRequired(buffer.readBoolean());
    final int tunnelType = buffer.readUnsignedByte();
    parseMpls(pmsiTunnelBuilder, buffer);
    final TunnelIdentifier tunnelIdentifier = this.tunnelIdentifierHandler.parse(tunnelType, buffer);
    if (tunnelIdentifier != null) {
        pmsiTunnelBuilder.setTunnelIdentifier(tunnelIdentifier);
    }
    builder.addAugmentation(PmsiTunnelAugmentation.class,
            new PmsiTunnelAugmentationBuilder().setPmsiTunnel(pmsiTunnelBuilder.build()).build());
}

From source file:org.opendaylight.protocol.bgp.evpn.impl.attributes.tunnel.identifier.OpaqueUtil.java

License:Open Source License

static List<OpaqueValue> parseOpaqueList(final ByteBuf byteBuf) {
    final List<OpaqueValue> opaqueValues = new ArrayList<>();
    while (byteBuf.isReadable()) {
        final Opaque opaque = parseOpaque(byteBuf);
        if (opaque != null) {
            opaqueValues.add((OpaqueValue) opaque);
        }/*ww w .  j a v  a 2s. c o m*/
    }
    return opaqueValues;
}

From source file:org.opendaylight.protocol.bgp.evpn.impl.attributes.tunnel.identifier.TunnelIdentifierHandler.java

License:Open Source License

public TunnelIdentifier parse(final int tunnelType, final ByteBuf buffer) {
    final TunnelIdentifierParser parser = this.handlers.getParser(tunnelType);
    if (!buffer.isReadable() || parser == null) {
        LOG.debug(SKIP_PARSE, tunnelType);
        return null;
    }/*from w  ww  .j  a v  a  2s  .com*/
    return parser.parse(buffer);
}

From source file:org.opendaylight.protocol.bgp.evpn.impl.nlri.EvpnNlriParser.java

License:Open Source License

@Override
public void parseNlri(final ByteBuf nlri, final MpUnreachNlriBuilder builder) throws BGPParsingException {
    if (!nlri.isReadable()) {
        return;/*w ww .ja v  a  2s  .c  o m*/
    }
    final List<EvpnDestination> dst = parseNlri(nlri);

    builder.setWithdrawnRoutes(new WithdrawnRoutesBuilder().setDestinationType(
            new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev160321.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationEvpnCaseBuilder()
                    .setDestinationEvpn(
                            new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev160321.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.destination.evpn._case.DestinationEvpnBuilder()
                                    .setEvpnDestination(dst).build())
                    .build())
            .build());
}

From source file:org.opendaylight.protocol.bgp.evpn.impl.nlri.EvpnNlriParser.java

License:Open Source License

@Override
public void parseNlri(final ByteBuf nlri, final MpReachNlriBuilder builder) throws BGPParsingException {
    if (!nlri.isReadable()) {
        return;//from  ww w.  ja v  a 2  s . c om
    }
    final List<EvpnDestination> dst = parseNlri(nlri);

    builder.setAdvertizedRoutes(new AdvertizedRoutesBuilder()
            .setDestinationType(new DestinationEvpnCaseBuilder()
                    .setDestinationEvpn(new DestinationEvpnBuilder().setEvpnDestination(dst).build()).build())
            .build());
}

From source file:org.opendaylight.protocol.bgp.evpn.impl.nlri.EvpnNlriParser.java

License:Open Source License

@Nullable
private List<EvpnDestination> parseNlri(final ByteBuf nlri) {
    if (!nlri.isReadable()) {
        return null;
    }/* w  w  w . j a v  a2s .  com*/
    final List<EvpnDestination> dests = new ArrayList<>();

    while (nlri.isReadable()) {
        final EvpnDestinationBuilder builder = new EvpnDestinationBuilder();
        final NlriType type = NlriType.forValue(nlri.readUnsignedByte());
        final int length = nlri.readUnsignedByte();
        final ByteBuf nlriBuf = nlri.readSlice(length);
        builder.setRouteDistinguisher(parseRouteDistinguisher(nlriBuf));
        builder.setEvpnChoice(SimpleEvpnNlriRegistry.getInstance().parseEvpn(type, nlriBuf));
        dests.add(builder.build());
    }
    return dests;
}