Search in sources :

Example 36 with ByteBufOutputStream

use of io.netty.buffer.ByteBufOutputStream in project pravega by pravega.

the class CommandEncoder method writeMessage.

@SneakyThrows(IOException.class)
private void writeMessage(AppendBlock block, int blockSize) {
    int startIdx = buffer.writerIndex();
    ByteBufOutputStream bout = new ByteBufOutputStream(buffer);
    bout.writeInt(block.getType().getCode());
    bout.write(LENGTH_PLACEHOLDER);
    block.writeFields(bout);
    bout.flush();
    bout.close();
    int endIdx = buffer.writerIndex();
    int fieldsSize = endIdx - startIdx - TYPE_PLUS_LENGTH_SIZE;
    buffer.setInt(startIdx + TYPE_SIZE, fieldsSize + blockSize);
}
Also used : ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) SneakyThrows(lombok.SneakyThrows)

Example 37 with ByteBufOutputStream

use of io.netty.buffer.ByteBufOutputStream in project pravega by pravega.

the class CommandEncoder method writeMessage.

@SneakyThrows(IOException.class)
@VisibleForTesting
static int writeMessage(WireCommand msg, ByteBuf destination) {
    int startIdx = destination.writerIndex();
    ByteBufOutputStream bout = new ByteBufOutputStream(destination);
    bout.writeInt(msg.getType().getCode());
    bout.write(LENGTH_PLACEHOLDER);
    msg.writeFields(bout);
    bout.flush();
    bout.close();
    int endIdx = destination.writerIndex();
    int fieldsSize = endIdx - startIdx - TYPE_PLUS_LENGTH_SIZE;
    destination.setInt(startIdx + TYPE_SIZE, fieldsSize);
    return endIdx - startIdx;
}
Also used : ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) VisibleForTesting(com.google.common.annotations.VisibleForTesting) SneakyThrows(lombok.SneakyThrows)

Example 38 with ByteBufOutputStream

use of io.netty.buffer.ByteBufOutputStream in project vert.x by eclipse.

the class JacksonCodec method toBuffer.

@Override
public Buffer toBuffer(Object object, boolean pretty) throws EncodeException {
    ByteBuf buf = Unpooled.buffer();
    // There is no need to use a try with resources here as jackson
    // is a well-behaved and always calls the closes all streams in the
    // "finally" block bellow.
    ByteBufOutputStream out = new ByteBufOutputStream(buf);
    JsonGenerator generator = createGenerator(out, pretty);
    try {
        encodeJson(object, generator);
        generator.flush();
        return Buffer.buffer(buf);
    } catch (IOException e) {
        throw new EncodeException(e.getMessage(), e);
    } finally {
        close(generator);
    }
}
Also used : ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) EncodeException(io.vertx.core.json.EncodeException) IOException(java.io.IOException) ByteBuf(io.netty.buffer.ByteBuf)

Example 39 with ByteBufOutputStream

use of io.netty.buffer.ByteBufOutputStream in project traccar by tananaev.

the class GlobalstarProtocolDecoder method sendResponse.

private void sendResponse(Channel channel, String messageId) throws TransformerException {
    Document document = documentBuilder.newDocument();
    Element rootElement = document.createElement("stuResponseMsg");
    rootElement.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
    rootElement.setAttribute("xsi:noNamespaceSchemaLocation", "http://cody.glpconnect.com/XSD/StuResponse_Rev1_0.xsd");
    rootElement.setAttribute("deliveryTimeStamp", new SimpleDateFormat("dd/MM/yyyy hh:mm:ss z").format(new Date()));
    rootElement.setAttribute("messageID", "00000000000000000000000000000000");
    rootElement.setAttribute("correlationID", messageId);
    document.appendChild(rootElement);
    Element state = document.createElement("state");
    state.appendChild(document.createTextNode("pass"));
    rootElement.appendChild(state);
    Element stateMessage = document.createElement("stateMessage");
    stateMessage.appendChild(document.createTextNode("Store OK"));
    rootElement.appendChild(stateMessage);
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    ByteBuf content = Unpooled.buffer();
    transformer.transform(new DOMSource(document), new StreamResult(new ByteBufOutputStream(content)));
    if (channel != null) {
        FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, content);
        response.headers().add(HttpHeaderNames.CONTENT_LENGTH, content.readableBytes()).add(HttpHeaderNames.CONTENT_TYPE, "text/xml");
        channel.writeAndFlush(new NetworkMessage(response, channel.remoteAddress()));
    }
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) StreamResult(javax.xml.transform.stream.StreamResult) Element(org.w3c.dom.Element) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) Document(org.w3c.dom.Document) ByteBuf(io.netty.buffer.ByteBuf) NetworkMessage(org.traccar.NetworkMessage) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 40 with ByteBufOutputStream

use of io.netty.buffer.ByteBufOutputStream in project Almura by AlmuraDev.

the class ClientboundBiomeInformationPacket method writeTo.

@Override
public void writeTo(final ChannelBuf buf) {
    final int size = this.biomes.size();
    buf.writeInteger(size);
    if (size > 0) {
        for (final BiomeConfig biomeConfig : this.biomes.values()) {
            ByteBufOutputStream byteBufOutputStream = null;
            ObjectOutputStream objectOutputStream = null;
            try {
                final ByteBuf objBuf = Unpooled.buffer();
                byteBufOutputStream = new ByteBufOutputStream(objBuf);
                objectOutputStream = new ObjectOutputStream(byteBufOutputStream);
                objectOutputStream.writeObject(biomeConfig);
                buf.writeInteger(objBuf.array().length);
                buf.writeBytes(objBuf.array());
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (byteBufOutputStream != null) {
                    try {
                        byteBufOutputStream.close();
                    } catch (IOException ignored) {
                    }
                }
                if (objectOutputStream != null) {
                    try {
                        objectOutputStream.close();
                    } catch (IOException ignored) {
                    }
                }
            }
        }
    }
}
Also used : ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) BiomeConfig(com.almuradev.almura.feature.biome.BiomeConfig) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

ByteBufOutputStream (io.netty.buffer.ByteBufOutputStream)52 ByteBuf (io.netty.buffer.ByteBuf)37 IOException (java.io.IOException)18 ObjectOutputStream (java.io.ObjectOutputStream)11 OutputStreamWriter (java.io.OutputStreamWriter)6 ByteBufInputStream (io.netty.buffer.ByteBufInputStream)5 OutputStream (java.io.OutputStream)5 Test (org.junit.jupiter.api.Test)5 ObjectInputStream (java.io.ObjectInputStream)4 SneakyThrows (lombok.SneakyThrows)4 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)4 ByteBufAllocator (io.netty.buffer.ByteBufAllocator)3 Writer (java.io.Writer)3 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 CodedOutputStream (com.google.protobuf.CodedOutputStream)2 Bootstrap (io.netty.bootstrap.Bootstrap)2 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)2