Search in sources :

Example 6 with ByteBufOutputStream

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()));
}
Also used : ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) ByteBuf(io.netty.buffer.ByteBuf) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 7 with ByteBufOutputStream

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();
    }
}
Also used : ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) ObjectOutputStream(java.io.ObjectOutputStream)

Example 8 with ByteBufOutputStream

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);
}
Also used : ByteBufAllocator(io.netty.buffer.ByteBufAllocator) ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) OutputStream(java.io.OutputStream) ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) ByteBuf(io.netty.buffer.ByteBuf) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 9 with ByteBufOutputStream

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);
    }
}
Also used : ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) OutputStream(java.io.OutputStream) ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) Writable(groovy.lang.Writable) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) MimeTypes(ratpack.file.MimeTypes) ByteBuf(io.netty.buffer.ByteBuf) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) MarkupTemplate(ratpack.groovy.template.MarkupTemplate) Template(groovy.text.Template)

Example 10 with ByteBufOutputStream

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;
    }
}
Also used : ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) ByteBuf(io.netty.buffer.ByteBuf)

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