Search in sources :

Example 41 with ByteBufOutputStream

use of io.netty.buffer.ByteBufOutputStream in project SpongeCommon by SpongePowered.

the class SpongeFavicon method encode.

private static String encode(BufferedImage favicon) throws IOException {
    checkArgument(favicon.getWidth() == 64, "favicon must be 64 pixels wide");
    checkArgument(favicon.getHeight() == 64, "favicon must be 64 pixels high");
    ByteBuf buf = Unpooled.buffer();
    try {
        ImageIO.write(favicon, "PNG", new ByteBufOutputStream(buf));
        ByteBuf base64 = Base64.encode(buf);
        try {
            return SpongeFavicon.FAVICON_PREFIX + base64.toString(Charsets.UTF_8);
        } finally {
            base64.release();
        }
    } finally {
        buf.release();
    }
}
Also used : ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) ByteBuf(io.netty.buffer.ByteBuf)

Example 42 with ByteBufOutputStream

use of io.netty.buffer.ByteBufOutputStream in project Fracture by HellFirePvP.

the class PktSyncData method toBytes.

@Override
public void toBytes(ByteBuf buf) {
    buf.writeInt(data.size());
    for (String key : data.keySet()) {
        AbstractData dat = data.get(key);
        NBTTagCompound cmp = new NBTTagCompound();
        if (shouldSyncAll) {
            dat.writeAllDataToPacket(cmp);
        } else {
            dat.writeToPacket(cmp);
        }
        ByteBufUtils.writeString(buf, key);
        byte providerId = dat.getProviderID();
        buf.writeByte(providerId);
        try {
            CompressedStreamTools.write(cmp, new ByteBufOutputStream(buf));
        } catch (IOException ignored) {
        }
    }
}
Also used : AbstractData(hellfirepvp.fracture.common.data.AbstractData) ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) IOException(java.io.IOException)

Example 43 with ByteBufOutputStream

use of io.netty.buffer.ByteBufOutputStream in project LanternServer by LanternPowered.

the class LanternFavicon method encode.

/**
 * Encodes the buffered image into the encoded favicon string.
 *
 * @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();
    }
}
Also used : ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) ByteBuf(io.netty.buffer.ByteBuf)

Example 44 with ByteBufOutputStream

use of io.netty.buffer.ByteBufOutputStream in project flink by apache.

the class MessageSerializer method serializeRequestFailure.

/**
 * Serializes the exception containing the failure message sent to the {@link
 * org.apache.flink.queryablestate.network.Client} in case of protocol related errors.
 *
 * @param alloc The {@link ByteBufAllocator} used to allocate the buffer to serialize the
 *     message into.
 * @param requestId The id of the request to which the message refers to.
 * @param cause The exception thrown at the server.
 * @return A {@link ByteBuf} containing the serialized message.
 */
public static ByteBuf serializeRequestFailure(final ByteBufAllocator alloc, final long requestId, final Throwable cause) throws IOException {
    final ByteBuf buf = alloc.ioBuffer();
    // Frame length is set at the end
    buf.writeInt(0);
    writeHeader(buf, MessageType.REQUEST_FAILURE);
    buf.writeLong(requestId);
    try (ByteBufOutputStream bbos = new ByteBufOutputStream(buf);
        ObjectOutput out = new ObjectOutputStream(bbos)) {
        out.writeObject(cause);
    }
    // Set frame length
    int frameLength = buf.readableBytes() - Integer.BYTES;
    buf.setInt(0, frameLength);
    return buf;
}
Also used : ByteBufOutputStream(org.apache.flink.shaded.netty4.io.netty.buffer.ByteBufOutputStream) ObjectOutput(java.io.ObjectOutput) ByteBuf(org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf) ObjectOutputStream(java.io.ObjectOutputStream)

Example 45 with ByteBufOutputStream

use of io.netty.buffer.ByteBufOutputStream in project flink by apache.

the class MessageSerializer method serializeServerFailure.

/**
 * Serializes the failure message sent to the {@link
 * org.apache.flink.queryablestate.network.Client} in case of server related errors.
 *
 * @param alloc The {@link ByteBufAllocator} used to allocate the buffer to serialize the
 *     message into.
 * @param cause The exception thrown at the server.
 * @return The failure message.
 */
public static ByteBuf serializeServerFailure(final ByteBufAllocator alloc, final Throwable cause) throws IOException {
    final ByteBuf buf = alloc.ioBuffer();
    // Frame length is set at end
    buf.writeInt(0);
    writeHeader(buf, MessageType.SERVER_FAILURE);
    try (ByteBufOutputStream bbos = new ByteBufOutputStream(buf);
        ObjectOutput out = new ObjectOutputStream(bbos)) {
        out.writeObject(cause);
    }
    // Set frame length
    int frameLength = buf.readableBytes() - Integer.BYTES;
    buf.setInt(0, frameLength);
    return buf;
}
Also used : ByteBufOutputStream(org.apache.flink.shaded.netty4.io.netty.buffer.ByteBufOutputStream) ObjectOutput(java.io.ObjectOutput) ByteBuf(org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf) ObjectOutputStream(java.io.ObjectOutputStream)

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