Example usage for io.netty.buffer ByteBufInputStream ByteBufInputStream

List of usage examples for io.netty.buffer ByteBufInputStream ByteBufInputStream

Introduction

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

Prototype

public ByteBufInputStream(ByteBuf buffer) 

Source Link

Document

Creates a new stream which reads data from the specified buffer starting at the current readerIndex and ending at the current writerIndex .

Usage

From source file:org.lanternpowered.server.network.status.LanternFavicon.java

License:MIT License

/**
 * Decodes the buffered image from the encoded favicon string.
 * /*  w w  w.ja v  a2  s.c o  m*/
 * @param encoded the encoded string
 * @return the buffered image
 */
private static BufferedImage decode(String encoded) throws IOException {
    checkArgument(encoded.startsWith(FAVICON_PREFIX), "unknown favicon format");

    ByteBuf base64 = Unpooled.copiedBuffer(encoded.substring(FAVICON_PREFIX.length()), StandardCharsets.UTF_8);
    try {
        ByteBuf buf = Base64.decode(base64);
        try {
            BufferedImage result = ImageIO.read(new ByteBufInputStream(buf));
            checkState(result.getWidth() == 64, "favicon must be 64 pixels wide");
            checkState(result.getHeight() == 64, "favicon must be 64 pixels high");
            return result;
        } finally {
            buf.release();
        }
    } finally {
        base64.release();
    }
}

From source file:org.nosceon.titanite.body.Raw.java

License:Apache License

public <T> T map(BodyReader<T> reader) {
    if (content.readableBytes() > 0) {
        Callable<T> callable = () -> reader.readFrom(new ByteBufInputStream(content));
        return callUnchecked(callable);
    } else {/*  ww  w.  j av a  2  s  .com*/
        return null;
    }
}

From source file:org.nosceon.titanite.body.RawBodyParser.java

License:Apache License

private ByteBufInputStream stream() {
    return new ByteBufInputStream(content());
}

From source file:org.nosceon.titanite.MultiPart.java

License:Apache License

public InputStream content() {
    return callUnchecked(() -> {
        if (fileUpload.isInMemory()) {
            return new ByteBufInputStream(fileUpload.content());
        } else {/*from   ww w .j  a  v a  2s . c  o m*/
            return new FileInputStream(fileUpload.getFile());

        }
    });
}

From source file:org.onosproject.ovsdb.lib.utils.JsonRpcReaderUtil.java

License:Apache License

/**
 * Decode the bytes to Json object./*from  ww w  . j a v a2s  . c o m*/
 * @param in input of bytes
 * @param out ouput of Json object list
 * @param jrContext context for the last decoding process
 */
public static void readToJsonNode(ByteBuf in, List<Object> out, JsonReadContext jrContext) throws Exception {
    int lastReadBytes = jrContext.getLastReadBytes();
    if (lastReadBytes == 0) {
        if (in.readableBytes() < 4) {
            return;
        }
        checkEncoding(in);
    }

    int i = lastReadBytes + in.readerIndex();
    Stack<Byte> bufStack = jrContext.getBufStack();
    for (; i < in.writerIndex(); i++) {
        byte b = in.getByte(i);
        switch (b) {
        case '{':
            if (!isDoubleQuote(bufStack)) {
                bufStack.push(b);
                jrContext.setStartMatch(true);
            }
            break;
        case '}':
            if (!isDoubleQuote(bufStack)) {
                bufStack.pop();
            }
            break;
        case '"':
            if (in.getByte(i - 1) != '\\') {
                if (!bufStack.isEmpty() && bufStack.peek() != '"') {
                    bufStack.push(b);
                } else {
                    bufStack.pop();
                }
            }
            break;
        default:
            break;
        }

        if (jrContext.isStartMatch() && bufStack.isEmpty()) {
            ByteBuf buf = in.readSlice(i - in.readerIndex() + 1);
            JsonParser jf = new MappingJsonFactory().createParser(new ByteBufInputStream(buf));
            JsonNode jsonNode = jf.readValueAsTree();
            out.add(jsonNode);
            lastReadBytes = 0;
            jrContext.setLastReadBytes(lastReadBytes);
            break;
        }
    }

    if (i >= in.writerIndex()) {
        lastReadBytes = in.readableBytes();
        jrContext.setLastReadBytes(lastReadBytes);
    }
}

From source file:org.onosproject.ovsdb.rfc.utils.JsonRpcReaderUtil.java

License:Apache License

/**
 * Decode the bytes to Json object./*from   w  w w . j a v  a2  s .  com*/
 * @param in input of bytes
 * @param out ouput of Json object list
 * @param jrContext context for the last decoding process
 * @throws IOException IOException
 * @throws JsonParseException JsonParseException
 */
public static void readToJsonNode(ByteBuf in, List<Object> out, JsonReadContext jrContext) throws IOException {
    int lastReadBytes = jrContext.getLastReadBytes();
    if (lastReadBytes == 0) {
        if (in.readableBytes() < 4) {
            return;
        }
        checkEncoding(in);
    }

    int i = lastReadBytes + in.readerIndex();
    Stack<Byte> bufStack = jrContext.getBufStack();
    for (; i < in.writerIndex(); i++) {
        byte b = in.getByte(i);
        switch (b) {
        case '{':
            if (!isDoubleQuote(bufStack)) {
                bufStack.push(b);
                jrContext.setStartMatch(true);
            }
            break;
        case '}':
            if (!isDoubleQuote(bufStack)) {
                bufStack.pop();
            }
            break;
        case '"':
            if (in.getByte(i - 1) != '\\') {
                if (!bufStack.isEmpty() && bufStack.peek() != '"') {
                    bufStack.push(b);
                } else {
                    bufStack.pop();
                }
            }
            break;
        default:
            break;
        }

        if (jrContext.isStartMatch() && bufStack.isEmpty()) {
            ByteBuf buf = in.readSlice(i - in.readerIndex() + 1);
            JsonParser jf = new MappingJsonFactory().createParser(new ByteBufInputStream(buf));
            JsonNode jsonNode = jf.readValueAsTree();
            out.add(jsonNode);
            lastReadBytes = 0;
            jrContext.setLastReadBytes(lastReadBytes);
            break;
        }
    }

    if (i >= in.writerIndex()) {
        lastReadBytes = in.readableBytes();
        jrContext.setLastReadBytes(lastReadBytes);
    }
}

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 {
    /*/*  w w w  .ja va  2 s  .  c  o m*/
     * 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 ww  w  .j  ava  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.NetconfEXIToMessageDecoder.java

License:Open Source License

@Override
protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out)
        throws Exception {
    /*/* ww w .jav  a2s  . c  o  m*/
     * 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.readableBytes() == 0) {
        LOG.debug("No more content in incoming buffer.");
        return;
    }

    LOG.trace("Received to decode: {}", ByteBufUtil.hexDump(in));

    final EXIReader r = codec.getReader();

    final SAXTransformerFactory transformerFactory = (SAXTransformerFactory) TransformerFactory.newInstance();
    final TransformerHandler handler = transformerFactory.newTransformerHandler();
    r.setContentHandler(handler);

    final DOMResult domResult = new DOMResult();
    handler.setResult(domResult);

    try (final InputStream is = new ByteBufInputStream(in)) {
        r.parse(new InputSource(is));
    }

    out.add(new NetconfMessage((Document) domResult.getNode()));
}

From source file:org.opendaylight.controller.netconf.util.handler.NetconfXMLToMessageDecoder.java

License:Open Source License

@Override
@VisibleForTesting/*  w  w w.j a  va2s .  c om*/
public void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    if (in.readableBytes() != 0) {
        LOG.trace("Received to decode: {}", ByteBufUtil.hexDump(in));
        out.add(new NetconfMessage(XmlUtil.readXmlToDocument(new ByteBufInputStream(in))));
    } else {
        LOG.debug("No more content in incoming buffer.");
    }
}