Search in sources :

Example 46 with ByteBufOutputStream

use of org.apache.flink.shaded.netty4.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);
}
Also used : ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) ByteBuf(io.netty.buffer.ByteBuf)

Example 47 with ByteBufOutputStream

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

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

use of org.apache.flink.shaded.netty4.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();
    }
}
Also used : ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) ByteBuf(io.netty.buffer.ByteBuf) ObjectOutputStream(java.io.ObjectOutputStream)

Example 50 with ByteBufOutputStream

use of org.apache.flink.shaded.netty4.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);
    });
}
Also used : ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) Subscriber(rx.Subscriber) Worker(rx.Scheduler.Worker) JsonGenerator(org.codehaus.jackson.JsonGenerator) ByteBuf(io.netty.buffer.ByteBuf) JsonGenerationException(org.codehaus.jackson.JsonGenerationException) IOException(java.io.IOException)

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