Search in sources :

Example 16 with ByteBufOutputStream

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

the class JSONSerializer method serialize.

/**
     * Serialize an object into a given byte buffer.
     *
     * @param o The object to serialize.
     * @param b The bytebuf to serialize it into.
     */
@Override
public void serialize(Object o, ByteBuf b) {
    String className = o == null ? "null" : o.getClass().getName();
    if (className.endsWith(ICorfuSMR.CORFUSMR_SUFFIX)) {
        String SMRClass = className.split("\\$")[0];
        className = "CorfuObject";
        byte[] classNameBytes = className.getBytes();
        b.writeShort(classNameBytes.length);
        b.writeBytes(classNameBytes);
        byte[] SMRClassNameBytes = SMRClass.getBytes();
        b.writeShort(SMRClassNameBytes.length);
        b.writeBytes(SMRClassNameBytes);
        UUID id = ((ICorfuSMR) o).getCorfuStreamID();
        log.trace("Serializing a CorfuObject of type {} as a stream pointer to {}", SMRClass, id);
        b.writeLong(id.getMostSignificantBits());
        b.writeLong(id.getLeastSignificantBits());
    } else {
        byte[] classNameBytes = className.getBytes();
        b.writeShort(classNameBytes.length);
        b.writeBytes(classNameBytes);
        if (o == null) {
            return;
        }
        try (ByteBufOutputStream bbos = new ByteBufOutputStream(b)) {
            try (OutputStreamWriter osw = new OutputStreamWriter(bbos)) {
                gson.toJson(o, o.getClass(), osw);
            }
        } catch (IOException ie) {
            log.error("Exception during serialization!", ie);
        }
    }
}
Also used : ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) UUID(java.util.UUID) ICorfuSMR(org.corfudb.runtime.object.ICorfuSMR)

Example 17 with ByteBufOutputStream

use of io.netty.buffer.ByteBufOutputStream in project hbase by apache.

the class NettyRpcDuplexHandler method writeRequest.

private void writeRequest(ChannelHandlerContext ctx, Call call, ChannelPromise promise) throws IOException {
    id2Call.put(call.id, call);
    ByteBuf cellBlock = cellBlockBuilder.buildCellBlock(codec, compressor, call.cells, ctx.alloc());
    CellBlockMeta cellBlockMeta;
    if (cellBlock != null) {
        CellBlockMeta.Builder cellBlockMetaBuilder = CellBlockMeta.newBuilder();
        cellBlockMetaBuilder.setLength(cellBlock.writerIndex());
        cellBlockMeta = cellBlockMetaBuilder.build();
    } else {
        cellBlockMeta = null;
    }
    RequestHeader requestHeader = IPCUtil.buildRequestHeader(call, cellBlockMeta);
    int sizeWithoutCellBlock = IPCUtil.getTotalSizeWhenWrittenDelimited(requestHeader, call.param);
    int totalSize = cellBlock != null ? sizeWithoutCellBlock + cellBlock.writerIndex() : sizeWithoutCellBlock;
    ByteBuf buf = ctx.alloc().buffer(sizeWithoutCellBlock + 4);
    buf.writeInt(totalSize);
    ByteBufOutputStream bbos = new ByteBufOutputStream(buf);
    requestHeader.writeDelimitedTo(bbos);
    if (call.param != null) {
        call.param.writeDelimitedTo(bbos);
    }
    if (cellBlock != null) {
        ChannelPromise withoutCellBlockPromise = ctx.newPromise();
        ctx.write(buf, withoutCellBlockPromise);
        ChannelPromise cellBlockPromise = ctx.newPromise();
        ctx.write(cellBlock, cellBlockPromise);
        PromiseCombiner combiner = new PromiseCombiner();
        combiner.addAll(withoutCellBlockPromise, cellBlockPromise);
        combiner.finish(promise);
    } else {
        ctx.write(buf, promise);
    }
}
Also used : PromiseCombiner(io.netty.util.concurrent.PromiseCombiner) ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) CellBlockMeta(org.apache.hadoop.hbase.shaded.protobuf.generated.RPCProtos.CellBlockMeta) RequestHeader(org.apache.hadoop.hbase.shaded.protobuf.generated.RPCProtos.RequestHeader) ChannelPromise(io.netty.channel.ChannelPromise) ByteBuf(io.netty.buffer.ByteBuf)

Example 18 with ByteBufOutputStream

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

the class LzmaFrameEncoder method encode.

@Override
protected void encode(ChannelHandlerContext ctx, ByteBuf in, ByteBuf out) throws Exception {
    final int length = in.readableBytes();
    InputStream bbIn = null;
    ByteBufOutputStream bbOut = null;
    try {
        bbIn = new ByteBufInputStream(in);
        bbOut = new ByteBufOutputStream(out);
        bbOut.writeByte(properties);
        bbOut.writeInt(littleEndianDictionarySize);
        bbOut.writeLong(Long.reverseBytes(length));
        encoder.code(bbIn, bbOut, -1, -1, null);
    } finally {
        if (bbIn != null) {
            bbIn.close();
        }
        if (bbOut != null) {
            bbOut.close();
        }
    }
}
Also used : ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) InputStream(java.io.InputStream) ByteBufInputStream(io.netty.buffer.ByteBufInputStream)

Example 19 with ByteBufOutputStream

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

the class ObjectEncoder method encode.

@Override
protected void encode(ChannelHandlerContext ctx, Serializable msg, ByteBuf out) throws Exception {
    int startIdx = out.writerIndex();
    ByteBufOutputStream bout = new ByteBufOutputStream(out);
    ObjectOutputStream oout = null;
    try {
        bout.write(LENGTH_PLACEHOLDER);
        oout = new CompactObjectOutputStream(bout);
        oout.writeObject(msg);
        oout.flush();
    } finally {
        if (oout != null) {
            oout.close();
        } else {
            bout.close();
        }
    }
    int endIdx = out.writerIndex();
    out.setInt(startIdx, endIdx - startIdx - 4);
}
Also used : ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) ObjectOutputStream(java.io.ObjectOutputStream)

Example 20 with ByteBufOutputStream

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

the class ObjectEncoderOutputStream method writeObject.

@Override
public void writeObject(Object obj) throws IOException {
    ByteBuf buf = Unpooled.buffer(estimatedLength);
    try {
        ObjectOutputStream oout = new CompactObjectOutputStream(new ByteBufOutputStream(buf));
        try {
            oout.writeObject(obj);
            oout.flush();
        } finally {
            oout.close();
        }
        int objectSize = buf.readableBytes();
        writeInt(objectSize);
        buf.getBytes(0, this, objectSize);
    } finally {
        buf.release();
    }
}
Also used : ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) ByteBuf(io.netty.buffer.ByteBuf) ObjectOutputStream(java.io.ObjectOutputStream)

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