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);
}
}
}
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);
}
}
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);
}
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;
}
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();
}
}
}
Aggregations