Search in sources :

Example 11 with ByteBufOutputStream

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

the class ServerSentEventEncoder method encode.

public ByteBuf encode(Event<?> event, ByteBufAllocator bufferAllocator) throws Exception {
    ByteBuf buffer = bufferAllocator.buffer();
    OutputStream outputStream = new ByteBufOutputStream(buffer);
    Writer writer = new OutputStreamWriter(outputStream, CharsetUtil.encoder(UTF_8));
    String eventId = event.getId();
    if (eventId != null) {
        outputStream.write(EVENT_ID_PREFIX);
        writer.append(eventId).flush();
        outputStream.write(NEWLINE);
    }
    String eventType = event.getEvent();
    if (eventType != null) {
        outputStream.write(EVENT_TYPE_PREFIX);
        writer.append(eventType).flush();
        outputStream.write(NEWLINE);
    }
    String eventData = event.getData();
    if (eventData != null) {
        outputStream.write(EVENT_DATA_PREFIX);
        for (Character character : Lists.charactersOf(eventData)) {
            if (character == '\n') {
                outputStream.write(NEWLINE);
                outputStream.write(EVENT_DATA_PREFIX);
            } else {
                writer.append(character).flush();
            }
        }
        outputStream.write(NEWLINE);
    }
    outputStream.write(NEWLINE);
    writer.flush();
    writer.close();
    return buffer;
}
Also used : ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) OutputStream(java.io.OutputStream) ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) ByteBuf(io.netty.buffer.ByteBuf) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 12 with ByteBufOutputStream

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

the class MetricRegistryJsonMapper method apply.

@Override
public ByteBuf apply(MetricRegistry metricRegistry) throws Exception {
    ByteBuf byteBuf = byteBufAllocator.ioBuffer();
    try {
        OutputStream out = new ByteBufOutputStream(byteBuf);
        mapper.writeValue(out, metricRegistry);
        return byteBuf;
    } catch (Exception e) {
        byteBuf.release();
        throw e;
    }
}
Also used : ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) OutputStream(java.io.OutputStream) ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) ByteBuf(io.netty.buffer.ByteBuf)

Example 13 with ByteBufOutputStream

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

the class RailcraftPacket method getPacket.

public FMLProxyPacket getPacket() {
    ByteBuf byteBuf = Unpooled.buffer();
    try (ByteBufOutputStream out = new ByteBufOutputStream(byteBuf);
        RailcraftOutputStream data = new RailcraftOutputStream(out)) {
        data.writeByte(getID());
        writeData(data);
        return new FMLProxyPacket(new PacketBuffer(byteBuf), CHANNEL_NAME);
    } catch (IOException e) {
        Game.logThrowable("Error constructing packet: {0}", e, getClass());
        if (Game.DEVELOPMENT_ENVIRONMENT)
            throw new RuntimeException(e);
    }
    PacketBuffer buffer = new PacketBuffer(byteBuf);
    buffer.writeByte(-1);
    return new FMLProxyPacket(buffer, CHANNEL_NAME);
}
Also used : ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) FMLProxyPacket(net.minecraftforge.fml.common.network.internal.FMLProxyPacket) IOException(java.io.IOException) ByteBuf(io.netty.buffer.ByteBuf) PacketBuffer(net.minecraft.network.PacketBuffer)

Example 14 with ByteBufOutputStream

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

the class RailcraftTileEntity method getUpdateTag.

@Override
public final NBTTagCompound getUpdateTag() {
    NBTTagCompound nbt = super.getUpdateTag();
    ByteBuf byteBuf = Unpooled.buffer();
    try (ByteBufOutputStream out = new ByteBufOutputStream(byteBuf);
        RailcraftOutputStream data = new RailcraftOutputStream(out)) {
        writePacketData(data);
    } catch (IOException e) {
        Game.logThrowable("Error constructing tile packet: {0}", e, getClass());
        if (Game.DEVELOPMENT_ENVIRONMENT)
            throw new RuntimeException(e);
    }
    nbt.setByteArray("sync", byteBuf.array());
    return nbt;
}
Also used : ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) RailcraftOutputStream(mods.railcraft.common.util.network.RailcraftOutputStream) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) IOException(java.io.IOException) ByteBuf(io.netty.buffer.ByteBuf)

Example 15 with ByteBufOutputStream

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

the class EntityLocomotive method writeSpawnData.

@Override
public void writeSpawnData(ByteBuf data) {
    try {
        DataOutputStream byteStream = new DataOutputStream(new ByteBufOutputStream(data));
        byteStream.writeUTF(hasCustomName() ? getName() : "");
        byteStream.writeUTF(model);
    } catch (IOException ignored) {
    }
}
Also used : ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) DataOutputStream(java.io.DataOutputStream) IOException(java.io.IOException)

Aggregations

ByteBufOutputStream (io.netty.buffer.ByteBufOutputStream)26 ByteBuf (io.netty.buffer.ByteBuf)21 IOException (java.io.IOException)7 ObjectOutputStream (java.io.ObjectOutputStream)7 OutputStream (java.io.OutputStream)5 OutputStreamWriter (java.io.OutputStreamWriter)4 ByteBufInputStream (io.netty.buffer.ByteBufInputStream)3 Writer (java.io.Writer)3 ByteBufAllocator (io.netty.buffer.ByteBufAllocator)2 ObjectInputStream (java.io.ObjectInputStream)2 Test (org.junit.Test)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)1 CodedOutputStream (com.google.protobuf.CodedOutputStream)1 Writable (groovy.lang.Writable)1 Template (groovy.text.Template)1 CompositeByteBuf (io.netty.buffer.CompositeByteBuf)1 ChannelId (io.netty.channel.ChannelId)1 ChannelPromise (io.netty.channel.ChannelPromise)1