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:com.eucalyptus.ws.handlers.IoInternalHmacHandler.java

License:Open Source License

private SignableRequest<?> wrapRequest(final FullHttpRequest request) {
    return new SignableRequest() {
        @Override//from w w  w  .j  a  va  2  s  .  c om
        public Map<String, String> getHeaders() {
            return request.headers().entries().stream()
                    .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
        }

        @Override
        public String getResourcePath() {
            return request.getUri();
        }

        @Override
        public Map<String, List<String>> getParameters() {
            return Collections.emptyMap();
        }

        @Override
        public URI getEndpoint() {
            return URI.create("http://" + request.headers().get(HttpHeaders.Names.HOST));
        }

        @Override
        public HttpMethodName getHttpMethod() {
            return HttpMethodName.fromValue(request.getMethod().name());
        }

        @Override
        public int getTimeOffset() {
            return 0;
        }

        @Override
        public InputStream getContent() {
            return new ByteBufInputStream(request.content().slice());
        }

        @Override
        public InputStream getContentUnwrapped() {
            return getContent();
        }

        @Override
        public ReadLimitInfo getReadLimitInfo() {
            return () -> request.content().readableBytes();
        }

        @Override
        public Object getOriginalRequestObject() {
            throw new RuntimeException("Not supported");
        }

        @Override
        public void addHeader(final String s, final String s1) {
            request.headers().set(s, s1);
        }

        @Override
        public void addParameter(final String s, final String s1) {
            throw new RuntimeException("Not supported");
        }

        @Override
        public void setContent(final InputStream inputStream) {
            throw new RuntimeException("Not supported");
        }
    };
}

From source file:com.github.sparkfy.network.buffer.NettyManagedBuffer.java

License:Apache License

@Override
public InputStream createInputStream() throws IOException {
    return new ByteBufInputStream(buf);
}

From source file:com.github.sparkfy.network.buffer.NioManagedBuffer.java

License:Apache License

@Override
public InputStream createInputStream() throws IOException {
    return new ByteBufInputStream(Unpooled.wrappedBuffer(buf));
}

From source file:com.heliosapm.streams.collector.groovy.ByteBufReaderSource.java

License:Apache License

/**
 * {@inheritDoc}/*from  w w w.j  av a  2 s.  c o m*/
 * @see org.codehaus.groovy.control.io.ReaderSource#getReader()
 */
@Override
public Reader getReader() throws IOException {
    final InputStream is = new ByteBufInputStream(prejectedBuffer.duplicate());
    return new InputStreamReader(is, UTF8);
}

From source file:com.heliosapm.streams.json.JSONOps.java

License:Apache License

/**
 * Deserializes a JSON formatted byte array to a specific class type
 * <b>Note:</b> If you get mapping exceptions you may need to provide a 
 * TypeReference//from  ww w. j  a  v a2  s.  c  om
 * @param json The buffer to deserialize from
 * @param pojo The class type of the object used for deserialization
 * @return An object of the {@code pojo} type
 * @throws IllegalArgumentException if the data or class was null or parsing 
 * failed
 * @throws JSONException if the data could not be parsed
 */
public static final <T> T parseToObject(final ByteBuf json, final Class<T> pojo) {
    if (json == null)
        throw new IllegalArgumentException("Incoming buffer was null");
    if (pojo == null)
        throw new IllegalArgumentException("Missing class type");
    InputStream is = new ByteBufInputStream(json);
    try {
        return jsonMapper.readValue(is, pojo);
    } catch (JsonParseException e) {
        throw new IllegalArgumentException(e);
    } catch (JsonMappingException e) {
        throw new IllegalArgumentException(e);
    } catch (IOException e) {
        throw new JSONException(e);
    } finally {
        if (is != null)
            try {
                is.close();
            } catch (Exception x) {
                /* No Op */}
    }
}

From source file:com.heliosapm.streams.json.JSONOps.java

License:Apache License

/**
 * Parses the passed channel buffer into a JsonNode
 * @param buff The buffer to parse//w w  w.ja va 2s  . c  o  m
 * @param nullIfNoContent If true, returns null if no content is available to parse
 * @return the parsed JsonNode
 */
public static JsonNode parseToNode(final ByteBuf buff, final boolean nullIfNoContent) {
    if (buff == null || buff.readableBytes() < 1) {
        if (nullIfNoContent)
            return null;
        throw new IllegalArgumentException("Incoming data was null");
    }
    final InputStream is = new ByteBufInputStream(buff);
    try {
        return parseToNode(is);
    } catch (Exception e) {
        if (nullIfNoContent)
            return null;
        throw new JSONException(e);
    } finally {
        try {
            is.close();
        } catch (Exception x) {
            /* No Op */}
    }
}

From source file:com.heliosapm.streams.json.JSONOps.java

License:Apache License

/**
 * Serializes the passed object to an off-heap buffer and returns an InputStream to read it back
 * @param obj The object to serialize//from w ww. ja  v a 2s . c o  m
 * @return an InputStream to read back the JSON serialized object 
 */
public static InputStream serializeOffHeapLoopBack(final Object obj) {
    if (obj == null)
        throw new IllegalArgumentException("The passed object was null");
    final ByteBuf cb = byteBufAllocator.buffer();
    final OutputStream os = new ByteBufOutputStream(cb);

    try {
        serialize(obj, os);
        os.flush();
        os.close();
    } catch (Exception ex) {
        throw new RuntimeException("Failed to write object to buffer", ex);
    }

    return new ByteBufInputStream(cb) {
        @Override
        public void close() throws IOException {
            super.close();
            try {
                cb.release();
            } catch (Exception x) {
                /* No Op */}
        }
    };
}

From source file:com.heliosapm.streams.tracing.AbstractMetricWriter.java

License:Apache License

/**
 * {@inheritDoc}/*from   ww  w . j  a  va2s . c  o  m*/
 * @see com.heliosapm.streams.tracing.IMetricWriter#onMetrics(io.netty.buffer.ByteBuf)
 */
@Override
public void onMetrics(final ByteBuf metrics) {
    metrics.readerIndex(0);
    final InputStream is = new ByteBufInputStream(metrics);
    try {
        for (StreamedMetric sm : StreamedMetric.streamedMetrics(is, true, false)) {
            onMetrics(sm);
        }
        //onMetrics(StreamedMetric.read(is));
    } finally {
        try {
            is.close();
        } catch (Exception x) {
            /* No Op */}
    }
}

From source file:com.heliosapm.tsdblite.json.JSON.java

License:Apache License

/**
 * Deserializes a JSON formatted byte array to a specific class type
 * <b>Note:</b> If you get mapping exceptions you may need to provide a 
 * TypeReference//from  ww  w  . j  ava  2 s  .  com
 * @param json The buffer to deserialize from
 * @param pojo The class type of the object used for deserialization
 * @param charset The character set of the content type in the buffer
 * @return An object of the {@code pojo} type
 * @throws IllegalArgumentException if the data or class was null or parsing 
 * failed
 * @throws JSONException if the data could not be parsed
 */
public static final <T> T parseToObject(final ByteBuf json, final Class<T> pojo, final Charset charset) {
    if (json == null)
        throw new IllegalArgumentException("Incoming buffer was null");
    if (pojo == null)
        throw new IllegalArgumentException("Missing class type");
    if (charset == null)
        throw new IllegalArgumentException("Missing charset");
    InputStream is = new ByteBufInputStream(json);
    InputStreamReader isr = new InputStreamReader(is, charset);
    try {
        return jsonMapper.readValue(isr, pojo);
    } catch (JsonParseException e) {
        throw new IllegalArgumentException(e);
    } catch (JsonMappingException e) {
        throw new IllegalArgumentException(e);
    } catch (IOException e) {
        throw new JSONException(e);
    } finally {
        if (is != null)
            try {
                is.close();
            } catch (Exception x) {
                /* No Op */}
        if (isr != null)
            try {
                isr.close();
            } catch (Exception x) {
                /* No Op */}
    }
}

From source file:com.heliosapm.tsdblite.json.JSON.java

License:Apache License

/**
 * Deserializes a JSON formatted ByteBuf to a JsonNode
 * @param json The buffer to deserialize from
 * @param charset The optional character set of the content type in the buffer which defaults to UTF8
 * @return The parsed JsonNode/*from  w  w w.j  a  va 2  s .  c  om*/
 * @throws IllegalArgumentException if buffer was null
 * @throws JSONException if the data could not be parsed
 */
public static final JsonNode parseToNode(final ByteBuf json, final Charset charset) {
    if (json == null)
        throw new IllegalArgumentException("Incoming buffer was null");
    InputStream is = new ByteBufInputStream(json);
    InputStreamReader isr = new InputStreamReader(is, charset == null ? UTF8 : charset);
    try {
        return jsonMapper.readTree(is);
    } catch (JsonParseException e) {
        throw new IllegalArgumentException(e);
    } catch (JsonMappingException e) {
        throw new IllegalArgumentException(e);
    } catch (IOException e) {
        throw new JSONException(e);
    } finally {
        if (is != null)
            try {
                is.close();
            } catch (Exception x) {
                /* No Op */}
        if (isr != null)
            try {
                isr.close();
            } catch (Exception x) {
                /* No Op */}
    }
}