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