Search in sources :

Example 6 with ByteBufInputStream

use of io.netty.buffer.ByteBufInputStream in project cdap by caskdata.

the class StoreHandler method createStoreRequest.

/**
 * Creates a {@link StoreRequest} instance based on the given {@link HttpRequest}.
 */
private StoreRequest createStoreRequest(TopicId topicId, FullHttpRequest request) throws Exception {
    // Currently only support avro
    if (!"avro/binary".equals(request.headers().get(HttpHeaderNames.CONTENT_TYPE))) {
        throw new BadRequestException("Only avro/binary content type is supported.");
    }
    Decoder decoder = DecoderFactory.get().directBinaryDecoder(new ByteBufInputStream(request.content()), null);
    DatumReader<GenericRecord> datumReader = new GenericDatumReader<>(Schemas.V1.PublishRequest.SCHEMA);
    return new GenericRecordStoreRequest(topicId, datumReader.read(null, decoder));
}
Also used : GenericDatumReader(org.apache.avro.generic.GenericDatumReader) BadRequestException(co.cask.cdap.common.BadRequestException) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) Decoder(org.apache.avro.io.Decoder) GenericRecord(org.apache.avro.generic.GenericRecord)

Example 7 with ByteBufInputStream

use of io.netty.buffer.ByteBufInputStream in project cdap by caskdata.

the class DatasetInstanceHandler method getDatasetProperties.

private Map<String, String> getDatasetProperties(FullHttpRequest request) {
    Map<String, String> properties = new HashMap<>();
    ByteBuf content = request.content();
    if (!content.isReadable()) {
        return properties;
    }
    try (Reader reader = new InputStreamReader(new ByteBufInputStream(request.content()), StandardCharsets.UTF_8)) {
        properties = GSON.fromJson(reader, new TypeToken<Map<String, String>>() {
        }.getType());
    } catch (IOException e) {
    // no-op since is happens during closing of the reader
    }
    return properties;
}
Also used : InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) IOException(java.io.IOException) ByteBuf(io.netty.buffer.ByteBuf) HashMap(java.util.HashMap) Map(java.util.Map)

Example 8 with ByteBufInputStream

use of io.netty.buffer.ByteBufInputStream in project cdap by caskdata.

the class ConversionHelpers method getInstanceConfiguration.

static DatasetInstanceConfiguration getInstanceConfiguration(FullHttpRequest request) throws BadRequestException {
    Reader reader = new InputStreamReader(new ByteBufInputStream(request.content()), StandardCharsets.UTF_8);
    try {
        DatasetInstanceConfiguration config = GSON.fromJson(reader, DatasetInstanceConfiguration.class);
        Preconditions.checkNotNull(config.getTypeName(), "The typeName must be specified.");
        return config;
    } catch (JsonSyntaxException | NullPointerException e) {
        throw new BadRequestException(e.getMessage());
    }
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) InputStreamReader(java.io.InputStreamReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BadRequestException(co.cask.cdap.common.BadRequestException) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) DatasetInstanceConfiguration(co.cask.cdap.proto.DatasetInstanceConfiguration)

Example 9 with ByteBufInputStream

use of io.netty.buffer.ByteBufInputStream in project teiid by teiid.

the class ObjectDecoder method decode.

@Override
protected Object decode(ChannelHandlerContext ctx, ByteBuf buffer) throws Exception {
    if (result == null) {
        ByteBuf frame = null;
        try {
            frame = (ByteBuf) super.decode(ctx, buffer);
        } catch (TooLongFrameException e) {
            throw new IOException(RuntimePlugin.Util.gs(RuntimePlugin.Event.TEIID40166), e);
        }
        if (frame == null) {
            return null;
        }
        CompactObjectInputStream cois = new CompactObjectInputStream(new ByteBufInputStream(frame), classLoader);
        result = cois.readObject();
        streams = ExternalizeUtil.readList(cois, StreamFactoryReference.class);
        streamIndex = 0;
    }
    while (streamIndex < streams.size()) {
        // read the new chunk size
        if (streamDataToRead == -1) {
            if (buffer.readableBytes() < 2) {
                return null;
            }
            streamDataToRead = buffer.readUnsignedShort();
        }
        if (stream == null) {
            // $NON-NLS-1$
            store = storageManager.createFileStore("temp-stream");
            StreamFactoryReference sfr = streams.get(streamIndex);
            sfr.setStreamFactory(new FileStoreInputStreamFactory(store, Streamable.ENCODING));
            this.stream = new BufferedOutputStream(store.createOutputStream());
        }
        // end of stream
        if (streamDataToRead == 0) {
            stream.close();
            stream = null;
            streamIndex++;
            streamDataToRead = -1;
            continue;
        }
        if (store.getLength() + streamDataToRead > maxLobSize) {
            if (error == null) {
                error = new StreamCorruptedException(// $NON-NLS-1$ //$NON-NLS-2$
                "lob too big: " + (store.getLength() + streamDataToRead) + " (max: " + maxLobSize + ')');
            }
        }
        int toRead = Math.min(buffer.readableBytes(), streamDataToRead);
        if (toRead == 0) {
            return null;
        }
        if (error == null) {
            buffer.readBytes(this.stream, toRead);
        } else {
            buffer.skipBytes(toRead);
        }
        streamDataToRead -= toRead;
        if (streamDataToRead == 0) {
            // get the next chunk
            streamDataToRead = -1;
        }
    }
    Object toReturn = result;
    result = null;
    streams = null;
    stream = null;
    store = null;
    if (error != null) {
        StreamCorruptedException sce = error;
        error = null;
        throw sce;
    }
    return toReturn;
}
Also used : CompactObjectInputStream(org.teiid.netty.handler.codec.serialization.CompactObjectInputStream) TooLongFrameException(io.netty.handler.codec.TooLongFrameException) FileStoreInputStreamFactory(org.teiid.common.buffer.FileStoreInputStreamFactory) StreamCorruptedException(java.io.StreamCorruptedException) IOException(java.io.IOException) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) StreamFactoryReference(org.teiid.core.types.InputStreamFactory.StreamFactoryReference) ByteBuf(io.netty.buffer.ByteBuf) BufferedOutputStream(java.io.BufferedOutputStream)

Example 10 with ByteBufInputStream

use of io.netty.buffer.ByteBufInputStream in project LanternServer by LanternPowered.

the class LanternFavicon method decode.

/**
 * Decodes the buffered image from the encoded favicon string.
 *
 * @param encoded the encoded string
 * @return the buffered image
 */
private static BufferedImage decode(String encoded) throws IOException {
    checkArgument(encoded.startsWith(FAVICON_PREFIX), "unknown favicon format");
    ByteBuf base64 = Unpooled.copiedBuffer(encoded.substring(FAVICON_PREFIX.length()), StandardCharsets.UTF_8);
    try {
        ByteBuf buf = Base64.decode(base64);
        try {
            BufferedImage result = ImageIO.read(new ByteBufInputStream(buf));
            checkState(result.getWidth() == 64, "favicon must be 64 pixels wide");
            checkState(result.getHeight() == 64, "favicon must be 64 pixels high");
            return result;
        } finally {
            buf.release();
        }
    } finally {
        base64.release();
    }
}
Also used : ByteBufInputStream(io.netty.buffer.ByteBufInputStream) ByteBuf(io.netty.buffer.ByteBuf) BufferedImage(java.awt.image.BufferedImage)

Aggregations

ByteBufInputStream (io.netty.buffer.ByteBufInputStream)69 ByteBuf (io.netty.buffer.ByteBuf)22 IOException (java.io.IOException)22 InputStreamReader (java.io.InputStreamReader)18 BadRequestException (co.cask.cdap.common.BadRequestException)16 Reader (java.io.Reader)16 JsonSyntaxException (com.google.gson.JsonSyntaxException)11 InputStream (java.io.InputStream)10 Path (javax.ws.rs.Path)9 ObjectInputStream (java.io.ObjectInputStream)8 NamespaceId (co.cask.cdap.proto.id.NamespaceId)6 POST (javax.ws.rs.POST)6 Test (org.junit.jupiter.api.Test)5 AuditPolicy (co.cask.cdap.common.security.AuditPolicy)4 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)4 ByteBufOutputStream (io.netty.buffer.ByteBufOutputStream)4 RpcException (org.apache.drill.exec.rpc.RpcException)4 UnsupportedTypeException (co.cask.cdap.api.data.schema.UnsupportedTypeException)3 DatasetManagementException (co.cask.cdap.api.dataset.DatasetManagementException)3 ObjectOutputStream (java.io.ObjectOutputStream)3