use of io.netty.buffer.ByteBufOutputStream in project netty-socketio by mrniko.
the class EncoderHandler method sendError.
private void sendError(HttpErrorMessage errorMsg, ChannelHandlerContext ctx, ChannelPromise promise) throws IOException {
final ByteBuf encBuf = encoder.allocateBuffer(ctx.alloc());
ByteBufOutputStream out = new ByteBufOutputStream(encBuf);
encoder.getJsonSupport().writeValue(out, errorMsg.getData());
sendMessage(errorMsg, ctx.channel(), encBuf, "application/json", promise, HttpResponseStatus.BAD_REQUEST);
}
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();
}
}
}
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);
}
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 {
// Suppress a warning about resource leak since oout is closed below
ObjectOutputStream oout = new CompactObjectOutputStream(// lgtm[java/output-resource-leak]
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();
}
}
use of io.netty.buffer.ByteBufOutputStream in project WSPerfLab by Netflix-Skunkworks.
the class MockResponse method generateJson.
/**
* @param id
* ID from client used to assert correct client/server interaction
* @param delay
* How long to delay delivery to simulate server-side latency
* @param itemSize
* Length of each item String.
* @param numItems
* Number of items in response.
*
* @return String json
*/
public static Observable<ByteBuf> generateJson(long id, int delay, int itemSize, int numItems) {
return Observable.create((Subscriber<? super ByteBuf> subscriber) -> {
Worker worker = Schedulers.computation().createWorker();
subscriber.add(worker);
worker.schedule(() -> {
try {
ByteBuf buffer = Unpooled.buffer();
ByteBufOutputStream jsonAsBytes = new ByteBufOutputStream(buffer);
JsonGenerator json = jsonFactory.createJsonGenerator(jsonAsBytes);
json.writeStartObject();
// manipulate the ID such that we can know the response is from the server (client will know the logic)
long responseKey = getResponseKey(id);
json.writeNumberField("responseKey", responseKey);
json.writeNumberField("delay", delay);
if (itemSize > MAX_ITEM_LENGTH) {
throw new IllegalArgumentException("itemSize can not be larger than: " + MAX_ITEM_LENGTH);
}
json.writeNumberField("itemSize", itemSize);
json.writeNumberField("numItems", numItems);
json.writeArrayFieldStart("items");
for (int i = 0; i < numItems; i++) {
json.writeString(RAW_ITEM_LONG.substring(0, itemSize));
}
json.writeEndArray();
json.writeEndObject();
json.close();
subscriber.onNext(buffer);
subscriber.onCompleted();
} catch (Exception e) {
subscriber.onError(e);
}
}, delay, TimeUnit.MILLISECONDS);
});
}
Aggregations