Example usage for io.netty.buffer ByteBufOutputStream ByteBufOutputStream

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

Introduction

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

Prototype

public ByteBufOutputStream(ByteBuf buffer) 

Source Link

Document

Creates a new stream which writes data to the specified buffer .

Usage

From source file:org.lanternpowered.pingy.PingyProperties.java

License:MIT License

public void loadFavicon(Path directory) throws IOException {
    if (this.favicon.isEmpty()) {
        return;//from w  w  w .j  ava2s  .c  om
    }
    final Path faviconPath = directory.resolve(this.favicon);
    if (!Files.exists(faviconPath)) {
        throw new IOException("Favicon file does not exist.");
    }
    final BufferedImage image;
    try {
        image = ImageIO.read(faviconPath.toFile());
    } catch (IOException e) {
        throw new IOException("Unable to read the favicon file.");
    }
    if (image.getWidth() != 64) {
        throw new IOException("Favicon must be 64 pixels wide.");
    }
    if (image.getHeight() != 64) {
        throw new IOException("Favicon must be 64 pixels high.");
    }
    final ByteBuf buf = Unpooled.buffer();
    try {
        ImageIO.write(image, "PNG", new ByteBufOutputStream(buf));
        final ByteBuf base64 = Base64.encode(buf);

        try {
            this.faviconData = "data:image/png;base64," + base64.toString(StandardCharsets.UTF_8);
        } finally {
            base64.release();
        }
    } finally {
        buf.release();
    }
}

From source file:org.lanternpowered.server.network.buffer.LanternByteBuffer.java

License:MIT License

@Override
public OutputStream asOutputStream() {
    return new ByteBufOutputStream(this.buf);
}

From source file:org.lanternpowered.server.network.buffer.LanternByteBuffer.java

License:MIT License

@Override
public LanternByteBuffer writeDataView(@Nullable DataView data) {
    if (data == null) {
        this.buf.writeByte(0);
        return this;
    }// www .java  2s  . c  om
    try {
        NbtStreamUtils.write(data, new ByteBufOutputStream(this.buf), false);
    } catch (IOException e) {
        throw new CodecException(e);
    }
    return this;
}

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

License:MIT License

/**
 * Encodes the buffered image into the encoded favicon string.
 * //from www.  j  a  v a  2s.co m
 * @param image the buffered image
 * @return the favicon string
 */
private static String encode(BufferedImage image) throws IOException {
    checkArgument(image.getWidth() == 64, "favicon must be 64 pixels wide");
    checkArgument(image.getHeight() == 64, "favicon must be 64 pixels high");

    ByteBuf buf = Unpooled.buffer();

    try {
        ImageIO.write(image, "PNG", new ByteBufOutputStream(buf));
        ByteBuf base64 = Base64.encode(buf);

        try {
            return FAVICON_PREFIX + base64.toString(StandardCharsets.UTF_8);
        } finally {
            base64.release();
        }
    } finally {
        buf.release();
    }
}

From source file:org.msgpack.rpc.loop.netty.NettyTcpClientTransport.java

License:Apache License

@Override
protected ByteBufOutputStream newPendingBuffer() {
    return new ByteBufOutputStream(Unpooled.buffer());
}

From source file:org.opendaylight.controller.netconf.nettyutil.handler.NetconfMessageToEXIEncoder.java

License:Open Source License

@Override
protected void encode(final ChannelHandlerContext ctx, final NetconfMessage msg, final ByteBuf out)
        throws EXIOptionsException, IOException, TransformerException, TransmogrifierException {
    LOG.trace("Sent to encode : {}", msg);

    // TODO Workaround for bug 2679, recreate transmogrifier every time
    // If the transmogrifier is reused, encoded xml can become non valid according to EXI decoder
    // Seems like a bug in the nagasena library (try newer version of the library or fix the bug inside of it)
    // Related bugs 2459: reuse nagasena resources, 2458: upgrade nagasena to newest version
    final Transmogrifier transmogrifier = codec.getTransmogrifier();

    try (final OutputStream os = new ByteBufOutputStream(out)) {
        transmogrifier.setOutputStream(os);
        final ContentHandler handler = transmogrifier.getSAXTransmogrifier();
        final Transformer transformer = ThreadLocalTransformers.getDefaultTransformer();
        transformer.transform(new DOMSource(msg.getDocument()), new SAXResult(handler));
    } finally {/*from  w ww  .ja  v a 2  s .c  om*/
        // Make sure we do not retain any reference to state by removing
        // the output stream reference and resetting internal state.
        transmogrifier.setOutputStream(null);
        transmogrifier.getSAXTransmogrifier();
    }
}

From source file:org.opendaylight.controller.netconf.nettyutil.handler.NetconfMessageToXMLEncoder.java

License:Open Source License

@Override
@VisibleForTesting//from w  w w . ja v a  2 s . c om
public void encode(final ChannelHandlerContext ctx, final NetconfMessage msg, final ByteBuf out)
        throws IOException, TransformerException {
    LOG.trace("Sent to encode : {}", msg);

    if (clientId.isPresent()) {
        Comment comment = msg.getDocument().createComment("clientId:" + clientId.get());
        msg.getDocument().appendChild(comment);
    }

    try (OutputStream os = new ByteBufOutputStream(out)) {
        // Wrap OutputStreamWriter with BufferedWriter as suggested in javadoc for OutputStreamWriter
        StreamResult result = new StreamResult(new BufferedWriter(new OutputStreamWriter(os)));
        DOMSource source = new DOMSource(msg.getDocument());
        ThreadLocalTransformers.getPrettyTransformer().transform(source, result);
    }
}

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

License:Open Source License

@Override
protected void encode(final ChannelHandlerContext ctx, final NetconfMessage msg, final ByteBuf out)
        throws Exception {
    LOG.trace("Sent to encode : {}", XmlUtil.toString(msg.getDocument()));

    try (final OutputStream os = new ByteBufOutputStream(out)) {
        final Transmogrifier transmogrifier = codec.getTransmogrifier();
        transmogrifier.setOutputStream(os);

        final Transformer transformer = saxTransformerFactory.newTransformer();
        transformer.transform(new DOMSource(msg.getDocument()),
                new SAXResult(transmogrifier.getSAXTransmogrifier()));
    }//from  www. j  a  v  a 2s  .co  m
}

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

License:Open Source License

@Override
@VisibleForTesting//ww  w . j  av  a 2  s. c o  m
public void encode(ChannelHandlerContext ctx, NetconfMessage msg, ByteBuf out)
        throws IOException, TransformerException {
    LOG.debug("Sent to encode : {}", msg);

    if (clientId.isPresent()) {
        Comment comment = msg.getDocument().createComment("clientId:" + clientId.get());
        msg.getDocument().appendChild(comment);
    }

    try (OutputStream os = new ByteBufOutputStream(out)) {
        Transformer transformer = FACTORY.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");

        StreamResult result = new StreamResult(new OutputStreamWriter(os));
        DOMSource source = new DOMSource(msg.getDocument());
        transformer.transform(source, result);
    }
}

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

License:Open Source License

@Override
protected void encode(final ChannelHandlerContext ctx, final NetconfMessage msg, final ByteBuf out)
        throws EXIOptionsException, IOException, TransformerException, TransmogrifierException {
    LOG.trace("Sent to encode : {}", msg);

    try (final OutputStream os = new ByteBufOutputStream(out)) {
        transmogrifier.setOutputStream(os);
        final ContentHandler handler = transmogrifier.getSAXTransmogrifier();
        final Transformer transformer = ThreadLocalTransformers.getDefaultTransformer();
        transformer.transform(new DOMSource(msg.getDocument()), new SAXResult(handler));
    } finally {//  w ww.j av  a  2s  . c  o  m
        // Make sure we do not retain any reference to state by removing
        // the output stream reference and resetting internal state.
        transmogrifier.setOutputStream(null);
        transmogrifier.getSAXTransmogrifier();
    }
}