Search in sources :

Example 11 with ByteBufOutputStream

use of org.apache.flink.shaded.netty4.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 12 with ByteBufOutputStream

use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBufOutputStream in project minecolonies by Minecolonies.

the class SaveScanMessage method toBytes.

@Override
public void toBytes(@NotNull final ByteBuf buf) {
    final NBTTagCompound wrapperCompound = new NBTTagCompound();
    wrapperCompound.setString(TAG_MILLIS, fileName);
    wrapperCompound.setTag(TAG_SCHEMATIC, nbttagcompound);
    final PacketBuffer buffer = new PacketBuffer(buf);
    try (ByteBufOutputStream stream = new ByteBufOutputStream(buffer)) {
        CompressedStreamTools.writeCompressed(wrapperCompound, stream);
    } catch (final IOException e) {
        Log.getLogger().info("Problem at retrieving structure on server.", e);
    }
}
Also used : ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) IOException(java.io.IOException) PacketBuffer(net.minecraft.network.PacketBuffer)

Example 13 with ByteBufOutputStream

use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBufOutputStream in project ratpack by ratpack.

the class MetricsPrometheusHandler method handle.

@Override
public void handle(Context ctx) throws Exception {
    final ByteBufAllocator byteBufAllocator = ctx.get(ByteBufAllocator.class);
    ByteBuf buf = byteBufAllocator.ioBuffer();
    try (Writer w = new OutputStreamWriter(new ByteBufOutputStream(buf))) {
        TextFormat.write004(w, ctx.get(CollectorRegistry.class).metricFamilySamples());
    } catch (IOException e) {
        buf.release();
        throw e;
    }
    ctx.getResponse().contentType(TextFormat.CONTENT_TYPE_004).status(200).send(buf);
}
Also used : ByteBufAllocator(io.netty.buffer.ByteBufAllocator) ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) ByteBuf(io.netty.buffer.ByteBuf) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 14 with ByteBufOutputStream

use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBufOutputStream in project cdap by caskdata.

the class StoreHandler method encodeRollbackDetail.

/**
 * Encodes the {@link RollbackDetail} object as avro record based on the {@link Schemas.V1.PublishResponse#SCHEMA}.
 */
private ByteBuf encodeRollbackDetail(RollbackDetail rollbackDetail) throws IOException {
    Schema schema = Schemas.V1.PublishResponse.SCHEMA;
    // Constructs the response object as GenericRecord
    GenericRecord response = new GenericData.Record(schema);
    response.put("transactionWritePointer", rollbackDetail.getTransactionWritePointer());
    GenericRecord rollbackRange = new GenericData.Record(schema.getField("rollbackRange").schema());
    rollbackRange.put("startTimestamp", rollbackDetail.getStartTimestamp());
    rollbackRange.put("startSequenceId", rollbackDetail.getStartSequenceId());
    rollbackRange.put("endTimestamp", rollbackDetail.getEndTimestamp());
    rollbackRange.put("endSequenceId", rollbackDetail.getEndSequenceId());
    response.put("rollbackRange", rollbackRange);
    // For V1 PublishResponse, it contains an union(long, null) and then 2 longs and 2 integers,
    // hence the max size is 38
    // (union use 1 byte, long max size is 9 bytes, integer max size is 5 bytes in avro binary encoding)
    ByteBuf buffer = Unpooled.buffer(38);
    Encoder encoder = EncoderFactory.get().directBinaryEncoder(new ByteBufOutputStream(buffer), null);
    DatumWriter<GenericRecord> datumWriter = new GenericDatumWriter<>(schema);
    datumWriter.write(response, encoder);
    return buffer;
}
Also used : ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) Encoder(org.apache.avro.io.Encoder) Schema(org.apache.avro.Schema) GenericRecord(org.apache.avro.generic.GenericRecord) GenericDatumWriter(org.apache.avro.generic.GenericDatumWriter) GenericRecord(org.apache.avro.generic.GenericRecord) ByteBuf(io.netty.buffer.ByteBuf)

Example 15 with ByteBufOutputStream

use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBufOutputStream in project teiid by teiid.

the class ObjectEncoder method write.

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    ByteBuf out = allocateBuffer(ctx, this.estimatedLength, this.preferDirect);
    int startIdx = out.writerIndex();
    ByteBufOutputStream bout = new ByteBufOutputStream(out);
    bout.write(LENGTH_PLACEHOLDER);
    final CompactObjectOutputStream oout = new CompactObjectOutputStream(bout);
    try {
        oout.writeObject(msg);
        ExternalizeUtil.writeCollection(oout, oout.getReferences());
        oout.flush();
        oout.close();
        int endIdx = out.writerIndex();
        out.setInt(startIdx, endIdx - startIdx - 4);
        if (out.isReadable()) {
            ctx.write(out, promise);
            for (InputStream is : oout.getStreams()) {
                ctx.write(new AnonymousChunkedStream(new BufferedInputStream(is, CHUNK_SIZE)), promise);
            }
        } else {
            out.release();
            ctx.write(Unpooled.EMPTY_BUFFER, promise);
        }
        ctx.flush();
        out = null;
    } catch (Throwable t) {
        throw new FailedWriteException(msg, t);
    } finally {
        if (out != null) {
            out.release();
        }
    }
}
Also used : ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) ObjectInputStream(java.io.ObjectInputStream) ObjectDecoderInputStream(org.teiid.netty.handler.codec.serialization.ObjectDecoderInputStream) InputStream(java.io.InputStream) ByteBuf(io.netty.buffer.ByteBuf) CompactObjectOutputStream(org.teiid.netty.handler.codec.serialization.CompactObjectOutputStream)

Aggregations

ByteBufOutputStream (io.netty.buffer.ByteBufOutputStream)52 ByteBuf (io.netty.buffer.ByteBuf)37 IOException (java.io.IOException)18 ObjectOutputStream (java.io.ObjectOutputStream)11 OutputStreamWriter (java.io.OutputStreamWriter)6 ByteBufInputStream (io.netty.buffer.ByteBufInputStream)5 OutputStream (java.io.OutputStream)5 Test (org.junit.jupiter.api.Test)5 ObjectInputStream (java.io.ObjectInputStream)4 SneakyThrows (lombok.SneakyThrows)4 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)4 ByteBufAllocator (io.netty.buffer.ByteBufAllocator)3 Writer (java.io.Writer)3 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 CodedOutputStream (com.google.protobuf.CodedOutputStream)2 Bootstrap (io.netty.bootstrap.Bootstrap)2 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)2