Search in sources :

Example 16 with ByteBufOutputStream

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

the class PgBackendProtocol method initBuffer.

private void initBuffer(int estimatedLength) {
    this.dataOut = Unpooled.buffer(estimatedLength).order(ByteOrder.BIG_ENDIAN);
    ByteBufOutputStream cbos = new ByteBufOutputStream(this.dataOut);
    this.writer = new OutputStreamWriter(cbos, this.encoding);
}
Also used : ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) OutputStreamWriter(java.io.OutputStreamWriter)

Example 17 with ByteBufOutputStream

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

the class StreamFetchHandler method fetch.

/**
 * Handler for the HTTP API {@code /streams/[stream_name]/events?start=[start_ts]&end=[end_ts]&limit=[event_limit]}
 * <p>
 * Responds with:
 * <ul>
 * <li>404 if stream does not exist</li>
 * <li>204 if no event in the given start/end time range exists</li>
 * <li>200 if there is are one or more events</li>
 * </ul>
 * </p>
 * <p>
 * Response body is a JSON array of the StreamEvent object.
 * </p>
 *
 * @see StreamEventTypeAdapter StreamEventTypeAdapter for the format of the StreamEvent object
 */
@GET
@Path("/{stream}/events")
public void fetch(HttpRequest request, final HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("stream") String stream, @QueryParam("start") @DefaultValue("0") String start, @QueryParam("end") @DefaultValue("9223372036854775807") String end, @QueryParam("limit") @DefaultValue("2147483647") final int limitEvents) throws Exception {
    long startTime = TimeMathParser.parseTime(start, TimeUnit.MILLISECONDS);
    long endTime = TimeMathParser.parseTime(end, TimeUnit.MILLISECONDS);
    StreamId streamId = new StreamId(namespaceId, stream);
    if (!verifyGetEventsRequest(streamId, startTime, endTime, limitEvents, responder)) {
        return;
    }
    // Make sure the user has READ permission on the stream since getConfig doesn't check for the same.
    authorizationEnforcer.enforce(streamId, authenticationContext.getPrincipal(), Action.READ);
    final StreamConfig streamConfig = streamAdmin.getConfig(streamId);
    long now = System.currentTimeMillis();
    startTime = Math.max(startTime, now - streamConfig.getTTL());
    endTime = Math.min(endTime, now);
    final long streamStartTime = startTime;
    final long streamEndTime = endTime;
    impersonator.doAs(streamId, new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            int limit = limitEvents;
            // Create the stream event reader
            try (FileReader<StreamEventOffset, Iterable<StreamFileOffset>> reader = createReader(streamConfig, streamStartTime)) {
                TimeRangeReadFilter readFilter = new TimeRangeReadFilter(streamStartTime, streamEndTime);
                List<StreamEvent> events = Lists.newArrayListWithCapacity(100);
                // Reads the first batch of events from the stream.
                int eventsRead = readEvents(reader, events, limit, readFilter);
                // If empty already, return 204 no content
                if (eventsRead <= 0) {
                    responder.sendStatus(HttpResponseStatus.NO_CONTENT);
                    return null;
                }
                // Send with chunk response, as we don't want to buffer all events in memory to determine the content-length.
                ChunkResponder chunkResponder = responder.sendChunkStart(HttpResponseStatus.OK, new DefaultHttpHeaders().set(HttpHeaderNames.CONTENT_TYPE, "application/json; charset=utf-8"));
                ByteBuf buffer = Unpooled.buffer();
                JsonWriter jsonWriter = new JsonWriter(new OutputStreamWriter(new ByteBufOutputStream(buffer), StandardCharsets.UTF_8));
                // Response is an array of stream event
                jsonWriter.beginArray();
                while (limit > 0 && eventsRead > 0) {
                    limit -= eventsRead;
                    for (StreamEvent event : events) {
                        GSON.toJson(event, StreamEvent.class, jsonWriter);
                        jsonWriter.flush();
                        // If exceeded chunk size limit, send a new chunk.
                        if (buffer.readableBytes() >= CHUNK_SIZE) {
                            // If the connect is closed, sendChunk will throw IOException.
                            // No need to handle the exception as it will just propagated back to the netty-http library
                            // and it will handle it.
                            // Need to copy the buffer because the buffer will get reused and send chunk is an async operation
                            chunkResponder.sendChunk(buffer.copy());
                            buffer.clear();
                        }
                    }
                    events.clear();
                    if (limit > 0) {
                        eventsRead = readEvents(reader, events, limit, readFilter);
                    }
                }
                jsonWriter.endArray();
                jsonWriter.close();
                // Send the last chunk that still has data
                if (buffer.isReadable()) {
                    // No need to copy the last chunk, since the buffer will not be reused
                    chunkResponder.sendChunk(buffer);
                }
                Closeables.closeQuietly(chunkResponder);
            }
            return null;
        }
    });
}
Also used : StreamId(co.cask.cdap.proto.id.StreamId) ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) StreamEvent(co.cask.cdap.api.flow.flowlet.StreamEvent) StreamConfig(co.cask.cdap.data2.transaction.stream.StreamConfig) ByteBuf(io.netty.buffer.ByteBuf) JsonWriter(com.google.gson.stream.JsonWriter) IOException(java.io.IOException) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) FileReader(co.cask.cdap.data.file.FileReader) MultiLiveStreamFileReader(co.cask.cdap.data.stream.MultiLiveStreamFileReader) List(java.util.List) OutputStreamWriter(java.io.OutputStreamWriter) TimeRangeReadFilter(co.cask.cdap.data.stream.TimeRangeReadFilter) StreamFileOffset(co.cask.cdap.data.stream.StreamFileOffset) ChunkResponder(co.cask.http.ChunkResponder) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 18 with ByteBufOutputStream

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

the class EntityLocomotive method writeSpawnData.

@Override
public void writeSpawnData(ByteBuf data) {
    try {
        DataOutputStream byteStream = new DataOutputStream(new ByteBufOutputStream(data));
        byteStream.writeUTF(hasCustomName() ? getName() : "");
        byteStream.writeUTF(model);
    } catch (IOException ignored) {
    }
}
Also used : ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) DataOutputStream(java.io.DataOutputStream) IOException(java.io.IOException)

Example 19 with ByteBufOutputStream

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

the class TileRailcraft method getUpdateTag.

@Override
public final NBTTagCompound getUpdateTag() {
    NBTTagCompound nbt = super.getUpdateTag();
    ByteBuf byteBuf = Unpooled.buffer();
    try (ByteBufOutputStream out = new ByteBufOutputStream(byteBuf);
        RailcraftOutputStream data = new RailcraftOutputStream(out)) {
        writePacketData(data);
        byte[] syncData = new byte[byteBuf.readableBytes()];
        byteBuf.readBytes(syncData);
        nbt.setByteArray("sync", syncData);
    } catch (IOException e) {
        Game.log().throwable("Error constructing tile packet: {0}", e, getClass());
        if (Game.DEVELOPMENT_VERSION)
            throw new RuntimeException(e);
    } finally {
        byteBuf.release();
    }
    return nbt;
}
Also used : ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) RailcraftOutputStream(mods.railcraft.common.util.network.RailcraftOutputStream) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) IOException(java.io.IOException) ByteBuf(io.netty.buffer.ByteBuf)

Example 20 with ByteBufOutputStream

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

the class RailcraftPacket method getPacket.

public FMLProxyPacket getPacket() {
    ByteBuf byteBuf = Unpooled.buffer();
    try (ByteBufOutputStream out = new ByteBufOutputStream(byteBuf);
        RailcraftOutputStream data = new RailcraftOutputStream(out)) {
        data.writeByte(getID());
        writeData(data);
        return new FMLProxyPacket(new PacketBuffer(byteBuf), CHANNEL_NAME);
    } catch (IOException e) {
        Game.log().throwable("Error constructing packet: {0}", e, getClass());
        if (Game.DEVELOPMENT_VERSION)
            throw new RuntimeException(e);
    }
    PacketBuffer buffer = new PacketBuffer(byteBuf);
    buffer.writeByte(-1);
    return new FMLProxyPacket(buffer, CHANNEL_NAME);
}
Also used : ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) FMLProxyPacket(net.minecraftforge.fml.common.network.internal.FMLProxyPacket) IOException(java.io.IOException) ByteBuf(io.netty.buffer.ByteBuf) PacketBuffer(net.minecraft.network.PacketBuffer)

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