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;
}
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;
}
}
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);
}
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;
}
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) {
}
}
Aggregations