Example usage for io.netty.buffer ByteBufInputStream ByteBufInputStream

List of usage examples for io.netty.buffer ByteBufInputStream ByteBufInputStream

Introduction

In this page you can find the example usage for io.netty.buffer ByteBufInputStream ByteBufInputStream.

Prototype

public ByteBufInputStream(ByteBuf buffer) 

Source Link

Document

Creates a new stream which reads data from the specified buffer starting at the current readerIndex and ending at the current writerIndex .

Usage

From source file:io.liveoak.common.codec.DefaultLazyResourceState.java

License:Open Source License

/**
 * Get content as InputStream.//  ww w  .  j  ava2 s. com
 *
 * @return InputStream
 */
@Override
public InputStream contentAsStream() {
    if (fileUpload != null) {
        return new FileUploadInputStream(fileUpload);
    }
    if (buffer != null) {
        return new ByteBufInputStream(buffer);
    }
    return new InputStream() {
        @Override
        public int read() throws IOException {
            return -1;
        }
    };
}

From source file:io.liveoak.common.codec.json.JSONDecoder.java

License:Open Source License

@Override
public ResourceState decode(ByteBuf resource) throws IOException {
    return decode(() -> factory().createParser(new ByteBufInputStream(resource)));
}

From source file:io.mycat.netty.ProtocolTransport.java

License:Apache License

public ProtocolTransport(Channel channel, ByteBuf in) {
    this.channel = channel;
    this.in = in;
    this.out = channel.alloc().buffer(DEFAULT_BUFFER_SIZE);
    this.input = new ByteBufInputStream(in);
    this.output = new ByteBufOutputStream(out);
}

From source file:io.pravega.shared.protocol.netty.CommandDecoder.java

License:Open Source License

@VisibleForTesting
public static WireCommand parseCommand(ByteBuf in) throws IOException {
    @Cleanup/*from w  ww  .ja  v  a  2s . c om*/
    ByteBufInputStream is = new ByteBufInputStream(in);
    int readableBytes = in.readableBytes();
    if (readableBytes < WireCommands.TYPE_PLUS_LENGTH_SIZE) {
        throw new InvalidMessageException("Not enough bytes to read.");
    }
    WireCommandType type = readType(is);
    int length = readLength(is, readableBytes);
    WireCommand command = type.readFrom(is, length);
    return command;
}

From source file:io.soliton.protobuf.json.JsonRpcClientHandler.java

License:Apache License

@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, HttpResponse response)
        throws Exception {
    if (!(response instanceof HttpContent)) {
        clientLogger.logServerError(null, null, new Exception("Returned response has no content"));
        logger.severe("Returned response has no content");
        return;/*from  w  w w.  ja  va 2s .  c  o m*/
    }

    HttpContent content = (HttpContent) response;

    if (!response.headers().get(HttpHeaders.Names.CONTENT_TYPE).equals(JsonRpcProtocol.CONTENT_TYPE)) {
        logger.warning("Incorrect Content-Type: " + response.headers().get(HttpHeaders.Names.CONTENT_TYPE));
    }

    JsonElement root;
    try {
        ByteBufInputStream stream = new ByteBufInputStream(content.content());
        JsonReader reader = new JsonReader(new InputStreamReader(stream, Charsets.UTF_8));
        root = new JsonParser().parse(reader);
    } catch (JsonSyntaxException jsonException) {
        clientLogger.logServerError(null, null, jsonException);
        logger.warning("JSON response cannot be decoded");
        return;
    }
    if (!root.isJsonObject()) {
        logger.warning("JSON response is not a JSON object: " + root.toString());
        return;
    }

    JsonRpcResponse jsonRpcResponse = JsonRpcResponse.fromJson(root.getAsJsonObject());

    JsonElement requestId = jsonRpcResponse.id();
    if (requestId == null || !requestId.isJsonPrimitive()) {
        clientLogger.logServerError(null, null,
                new Exception("Received response identifier is not JSON primitive"));
        logger.warning("Received response identifier is not JSON primitive: " + requestId.toString());
        return;
    }

    JsonResponseFuture<? extends Message> future = inFlightRequests.remove(requestId.getAsLong());
    if (future == null) {
        clientLogger.logServerError(null, null, new Exception("Response received for unknown identifier"));
        logger.severe("Response received for unknown identifier: " + requestId.getAsLong());
        return;
    }

    clientLogger.logSuccess(future.method());
    future.setResponse(jsonRpcResponse);
}

From source file:io.soliton.protobuf.json.JsonRpcServerHandler.java

License:Apache License

@Override
protected void channelRead0(ChannelHandlerContext ctx, HttpRequest request) throws Exception {
    if (!(request instanceof HttpContent)) {
        JsonRpcError error = new JsonRpcError(HttpResponseStatus.BAD_REQUEST, "HTTP request was empty");
        JsonRpcResponse response = JsonRpcResponse.error(error);
        new JsonRpcCallback(null, ctx.channel(), true).onSuccess(response);
        return;// ww  w.j  a  va 2 s .  c  o m
    }

    HttpContent content = (HttpContent) request;

    JsonElement root;
    try {
        root = new JsonParser()
                .parse(new InputStreamReader(new ByteBufInputStream(content.content()), Charsets.UTF_8));
    } catch (JsonSyntaxException jse) {
        JsonRpcError error = new JsonRpcError(HttpResponseStatus.BAD_REQUEST, "Cannot decode JSON payload");
        JsonRpcResponse response = JsonRpcResponse.error(error);
        new JsonRpcCallback(null, ctx.channel(), true).onSuccess(response);
        return;
    }

    JsonElement id;
    if (!root.isJsonObject()) {
        JsonRpcResponse response = JsonRpcResponse.error(
                new JsonRpcError(HttpResponseStatus.BAD_REQUEST, "Received payload is not a JSON Object"));
        new JsonRpcCallback(null, ctx.channel(), true).onSuccess(response);
        return;
    } else {
        id = root.getAsJsonObject().get(JsonRpcProtocol.ID);
    }

    JsonRpcError transportError = validateTransport(request);
    if (transportError != null) {
        JsonRpcResponse response = JsonRpcResponse.error(transportError, id);
        new JsonRpcCallback(id, ctx.channel(), true).onSuccess(response);
        return;
    }

    JsonRpcRequest jsonRpcRequest;
    try {
        jsonRpcRequest = JsonRpcRequest.fromJson(root);
    } catch (JsonRpcError error) {
        serverLogger.logClientError(error);
        JsonRpcResponse response = JsonRpcResponse.error(error, id);
        new JsonRpcCallback(null, ctx.channel(), true).onSuccess(response);
        return;
    }

    Futures.addCallback(invoker.invoke(jsonRpcRequest),
            new JsonRpcCallback(jsonRpcRequest.id(), ctx.channel(), shouldPrettyPrint(request)),
            responseCallbackExecutor);
}

From source file:io.vertx.core.json.Json.java

License:Open Source License

/**
 * Decode a given JSON buffer to a POJO of the given class type.
 * @param buf the JSON buffer./*w w w. ja  va 2  s. c o m*/
 * @param type the type to map to.
 * @param <T> the generic type.
 * @return an instance of T
 * @throws DecodeException when there is a parsing or invalid mapping.
 */
public static <T> T decodeValue(Buffer buf, TypeReference<T> type) throws DecodeException {
    try {
        return mapper.readValue(new ByteBufInputStream(buf.getByteBuf()), type);
    } catch (Exception e) {
        throw new DecodeException("Failed to decode:" + e.getMessage(), e);
    }
}

From source file:io.vertx.core.json.Json.java

License:Open Source License

/**
 * Decode a given JSON buffer to a POJO of the given class type.
 * @param buf the JSON buffer./*  www  . j a v a  2 s  .c  o m*/
 * @param clazz the class to map to.
 * @param <T> the generic type.
 * @return an instance of T
 * @throws DecodeException when there is a parsing or invalid mapping.
 */
public static <T> T decodeValue(Buffer buf, Class<T> clazz) throws DecodeException {
    try {
        return mapper.readValue((InputStream) new ByteBufInputStream(buf.getByteBuf()), clazz);
    } catch (Exception e) {
        throw new DecodeException("Failed to decode:" + e.getMessage(), e);
    }
}

From source file:io.viewserver.network.netty.IncomingMessageHandler.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> objects)
        throws Exception {
    final byte[] array;
    final int offset;
    final int length = byteBuf.readableBytes();
    //        try {
    if (byteBuf.hasArray()) {
        array = byteBuf.array();/*from   w ww.  j  a v a 2 s  .com*/
        offset = byteBuf.arrayOffset() + byteBuf.readerIndex();
        networkMessageWheel.pushToWheel(new NettyChannel(channelHandlerContext.channel()), array, offset,
                length);
    } else {
        //                array = new byte[length];
        //                byteBuf.getBytes(byteBuf.readerIndex(), array, 0, length);
        //                offset = 0;
        networkMessageWheel.pushToWheel(new NettyChannel(channelHandlerContext.channel()),
                new ByteBufInputStream(byteBuf));
    }
    //        } finally {
    //            byteBuf.release();
    //        }
}

From source file:io.werval.server.netty.ByteBufByteSource.java

License:Apache License

@Override
public InputStream asStream() {
    return new ByteBufInputStream(bytebuf);
}