use of io.netty.buffer.ByteBufOutputStream in project netty by netty.
the class DefaultChannelIdTest method testSerialization.
@Test
public void testSerialization() throws Exception {
ChannelId a = DefaultChannelId.newInstance();
ChannelId b;
ByteBuf buf = Unpooled.buffer();
ObjectOutputStream out = new ObjectOutputStream(new ByteBufOutputStream(buf));
try {
out.writeObject(a);
out.flush();
} finally {
out.close();
}
ObjectInputStream in = new ObjectInputStream(new ByteBufInputStream(buf, true));
try {
b = (ChannelId) in.readObject();
} finally {
in.close();
}
assertThat(a, is(b));
assertThat(a, is(not(sameInstance(b))));
assertThat(a.asLongText(), is(b.asLongText()));
}
use of io.netty.buffer.ByteBufOutputStream in project netty by netty.
the class CompatibleObjectEncoder method encode.
@Override
protected void encode(ChannelHandlerContext ctx, Serializable msg, ByteBuf out) throws Exception {
ObjectOutputStream oos = newObjectOutputStream(new ByteBufOutputStream(out));
try {
if (resetInterval != 0) {
// Resetting will prevent OOM on the receiving side.
writtenObjects++;
if (writtenObjects % resetInterval == 0) {
oos.reset();
}
}
oos.writeObject(msg);
oos.flush();
} finally {
oos.close();
}
}
use of io.netty.buffer.ByteBufOutputStream in project ratpack by ratpack.
the class JsonRenderer method render.
@Override
public void render(Context ctx, JsonRender object) throws Exception {
ObjectWriter writer = object.getObjectWriter();
if (writer == null) {
writer = ctx.maybeGet(ObjectWriter.class).orElseGet(() -> ctx.get(ObjectMapper.class).writer());
}
Class<?> viewClass = object.getViewClass();
if (viewClass != null) {
writer = writer.withView(viewClass);
}
ByteBuf buffer = ctx.get(ByteBufAllocator.class).buffer();
OutputStream outputStream = new ByteBufOutputStream(buffer);
try {
writer.writeValue(outputStream, object.getObject());
} catch (JsonProcessingException e) {
buffer.release();
ctx.error(e);
return;
}
ctx.getResponse().contentTypeIfNotSet(HttpHeaderConstants.JSON).send(buffer);
}
use of io.netty.buffer.ByteBufOutputStream in project ratpack by ratpack.
the class MarkupTemplateRenderer method render.
@Override
public void render(Context ctx, MarkupTemplate template) throws Exception {
String contentType = template.getContentType();
contentType = contentType == null ? ctx.get(MimeTypes.class).getContentType(template.getName()) : contentType;
try {
Template compiledTemplate = engine.createTemplateByPath(template.getName());
Writable boundTemplate = compiledTemplate.make(template.getModel());
ByteBuf byteBuf = byteBufAllocator.directBuffer();
try {
OutputStream outputStream = new ByteBufOutputStream(byteBuf);
Writer writer = new OutputStreamWriter(outputStream, CharsetUtil.encoder(StandardCharsets.UTF_8));
boundTemplate.writeTo(writer);
} catch (Exception e) {
byteBuf.release();
throw e;
}
ctx.getResponse().send(contentType, byteBuf);
} catch (IOException e) {
ctx.error(e);
}
}
use of io.netty.buffer.ByteBufOutputStream in project ratpack by ratpack.
the class DefaultSession method serialize.
private ByteBuf serialize() throws Exception {
SerializedForm serializable = new SerializedForm();
serializable.entries = entries;
ByteBuf buffer = bufferAllocator.buffer();
OutputStream outputStream = new ByteBufOutputStream(buffer);
try {
defaultSerializer.serialize(SerializedForm.class, serializable, outputStream);
outputStream.close();
return buffer;
} catch (Throwable e) {
buffer.release();
throw e;
}
}
Aggregations