Example usage for io.netty.buffer ByteBufOutputStream ByteBufOutputStream

List of usage examples for io.netty.buffer ByteBufOutputStream ByteBufOutputStream

Introduction

In this page you can find the example usage for io.netty.buffer ByteBufOutputStream ByteBufOutputStream.

Prototype

public ByteBufOutputStream(ByteBuf buffer) 

Source Link

Document

Creates a new stream which writes data to the specified buffer .

Usage

From source file:com.linecorp.armeria.internal.grpc.ArmeriaMessageFramer.java

License:Apache License

private ByteBuf writeCompressed(ByteBuf message) throws IOException {
    final CompositeByteBuf compressed = alloc.compositeBuffer();
    try (OutputStream compressingStream = compressor.compress(new ByteBufOutputStream(compressed))) {
        compressingStream.write(ByteBufUtil.getBytes(message));
    } finally {/*  w  w  w .ja va  2s.c om*/
        message.release();
    }

    return write(compressed, true);
}

From source file:com.linecorp.armeria.internal.grpc.GrpcMessageMarshaller.java

License:Apache License

public ByteBuf serializeRequest(I message) throws IOException {
    switch (requestType) {
    case PROTOBUF:
        return serializeProto((Message) message);
    default:/*  w ww  .j  a v  a  2  s . c o m*/
        final CompositeByteBuf out = alloc.compositeBuffer();
        try (ByteBufOutputStream os = new ByteBufOutputStream(out)) {
            ByteStreams.copy(method.streamRequest(message), os);
        }
        return out;
    }
}

From source file:com.linecorp.armeria.internal.grpc.GrpcMessageMarshaller.java

License:Apache License

public ByteBuf serializeResponse(O message) throws IOException {
    switch (responseType) {
    case PROTOBUF:
        return serializeProto((Message) message);
    default:// w  ww.  j ava2 s .  c  o m
        final CompositeByteBuf out = alloc.compositeBuffer();
        try (ByteBufOutputStream os = new ByteBufOutputStream(out)) {
            ByteStreams.copy(method.streamResponse(message), os);
        }
        return out;
    }
}

From source file:com.linecorp.armeria.internal.grpc.GrpcMessageMarshaller.java

License:Apache License

private ByteBuf serializeProto(Message message) throws IOException {
    if (GrpcSerializationFormats.isProto(serializationFormat)) {
        final ByteBuf buf = alloc.buffer(message.getSerializedSize());
        boolean success = false;
        try {/*  w  ww  .j a  va  2s  .  c om*/
            message.writeTo(CodedOutputStream.newInstance(buf.nioBuffer(0, buf.writableBytes())));
            buf.writerIndex(buf.capacity());
            success = true;
        } finally {
            if (!success) {
                buf.release();
            }
        }
        return buf;
    }

    if (GrpcSerializationFormats.isJson(serializationFormat)) {
        final ByteBuf buf = alloc.buffer();
        boolean success = false;
        try (ByteBufOutputStream os = new ByteBufOutputStream(buf)) {
            jsonMarshaller.writeValue(message, os);
            success = true;
        } finally {
            if (!success) {
                buf.release();
            }
        }
        return buf;
    }
    throw new IllegalStateException("Unknown serialization format: " + serializationFormat);
}

From source file:com.linecorp.armeria.internal.grpc.GrpcTestUtil.java

License:Apache License

public static ByteBuf protoByteBuf(MessageLite msg) {
    ByteBuf buf = Unpooled.buffer();// w w w .j ava  2s. c  o  m
    try (ByteBufOutputStream os = new ByteBufOutputStream(buf)) {
        msg.writeTo(os);
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
    return buf;
}

From source file:com.linecorp.armeria.internal.grpc.GrpcTestUtil.java

License:Apache License

public static byte[] compressedFrame(ByteBuf uncompressed) {
    ByteBuf compressed = Unpooled.buffer();
    try (ByteBufInputStream is = new ByteBufInputStream(uncompressed, true);
            GZIPOutputStream os = new GZIPOutputStream(new ByteBufOutputStream(compressed))) {
        ByteStreams.copy(is, os);//from  w  w  w.j a  v a 2s .  co  m
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
    ByteBuf buf = Unpooled.buffer();
    buf.writeByte(1);
    buf.writeInt(compressed.readableBytes());
    buf.writeBytes(compressed);
    compressed.release();
    byte[] result = ByteBufUtil.getBytes(buf);
    buf.release();
    return result;
}

From source file:com.mastfrog.acteur.resources.ClasspathResources.java

License:Open Source License

static void gzip(ByteBuf in, ByteBuf out) throws IOException {
    try (GZIPOutputStream outStream = new GZIPOutputStream(new ByteBufOutputStream(out), 9)) {
        try (ByteBufInputStream inStream = new ByteBufInputStream(in)) {
            Streams.copy(inStream, outStream, 512);
        }/*from  ww  w.  j  av a2s  .c  o m*/
    }
}

From source file:com.mastfrog.netty.http.client.RequestBuilder.java

License:Open Source License

@Override
public HttpRequestBuilder setBody(Object o, MediaType contentType) throws IOException {
    if (o instanceof CharSequence) {
        CharSequence seq = (CharSequence) o;
        setBody(seq.toString().getBytes(CharsetUtil.UTF_8), contentType);
    } else if (o instanceof byte[]) {
        byte[] b = (byte[]) o;
        ByteBuf buffer = alloc.buffer(b.length).writeBytes(b);
        setBody(buffer, contentType);/*from  w w w  .j ava2s  .c  o m*/
    } else if (o instanceof ByteBuf) {
        body = (ByteBuf) o;
        if (send100Continue) {
            addHeader(Headers.stringHeader(HttpHeaders.Names.EXPECT), HttpHeaders.Values.CONTINUE);
        }
        addHeader(Headers.CONTENT_LENGTH, (long) body.readableBytes());
        addHeader(Headers.CONTENT_TYPE, contentType);
    } else if (o instanceof InputStream) {
        ByteBuf buf = newByteBuf();
        try (ByteBufOutputStream out = new ByteBufOutputStream(buf)) {
            try (InputStream in = (InputStream) o) {
                Streams.copy(in, out, 1024);
            }
        }
        setBody(buf, contentType);
    } else if (o instanceof RenderedImage) {
        ByteBuf buf = newByteBuf();
        try (ByteBufOutputStream out = new ByteBufOutputStream(buf)) {
            String type = contentType.subtype();
            if ("jpeg".equals(type)) {
                type = "jpg";
            }
            ImageIO.write((RenderedImage) o, type, out);
        }
        setBody(buf, contentType);
    } else {
        try {
            setBody(new ObjectMapper().writeValueAsBytes(o), contentType);
        } catch (Exception ex) {
            throw new IllegalArgumentException(ex);
        }
    }
    return this;
}

From source file:com.mastfrog.scamper.compression.CompressingCodec.java

License:Open Source License

protected void compress(ByteBuf in, ByteBuf out) throws IOException {
    try (GZIPOutputStream outStream = new GZIPOutputStream(new ByteBufOutputStream(out), level)) {
        try (ByteBufInputStream inStream = new ByteBufInputStream(in)) {
            Streams.copy(inStream, outStream, 512);
        }/*w ww . jav a 2s .  c o  m*/
    }
}

From source file:com.mastfrog.scamper.compression.CompressingCodec.java

License:Open Source License

protected void uncompress(ByteBuf in, ByteBuf out) throws IOException {
    try (GZIPInputStream ins = new GZIPInputStream(new ByteBufInputStream(in))) {
        try (ByteBufOutputStream outs = new ByteBufOutputStream(out)) {
            Streams.copy(ins, outs, 512);
        }//w w w.  j  a v  a  2  s. co m
    }
}