List of usage examples for io.netty.buffer ByteBufInputStream ByteBufInputStream
public ByteBufInputStream(ByteBuf buffer, boolean releaseOnClose)
From source file:org.apache.distributedlog.LogRecord.java
License:Apache License
/** * Return the payload as an {@link InputStream}. * * @return payload as input stream/*from ww w.j a va 2s .com*/ */ public InputStream getPayLoadInputStream() { return new ByteBufInputStream(payload.retainedSlice(), true); }
From source file:org.apache.drill.exec.rpc.RpcDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) throws Exception { if (!ctx.channel().isOpen()) { return;//from w ww.ja va2s .c o m } if (RpcConstants.EXTRA_DEBUGGING) { logger.debug("Inbound rpc message received."); } // now, we know the entire message is in the buffer and the buffer is constrained to this message. Additionally, // this process should avoid reading beyond the end of this buffer so we inform the ByteBufInputStream to throw an // exception if be go beyond readable bytes (as opposed to blocking). final ByteBufInputStream is = new ByteBufInputStream(buffer, buffer.readableBytes()); // read the rpc header, saved in delimited format. checkTag(is, RpcEncoder.HEADER_TAG); final RpcHeader header = RpcHeader.parseDelimitedFrom(is); if (RpcConstants.EXTRA_DEBUGGING) { logger.debug(" post header read index {}", buffer.readerIndex()); } // read the protobuf body into a buffer. checkTag(is, RpcEncoder.PROTOBUF_BODY_TAG); final int pBodyLength = readRawVarint32(is); final ByteBuf pBody = buffer.slice(buffer.readerIndex(), pBodyLength); buffer.skipBytes(pBodyLength); pBody.retain(1); if (RpcConstants.EXTRA_DEBUGGING) { logger.debug("Read protobuf body of length {} into buffer {}.", pBodyLength, pBody); } if (RpcConstants.EXTRA_DEBUGGING) { logger.debug("post protobufbody read index {}", buffer.readerIndex()); } ByteBuf dBody = null; int dBodyLength = 0; // read the data body. if (buffer.readableBytes() > 0) { if (RpcConstants.EXTRA_DEBUGGING) { logger.debug("Reading raw body, buffer has {} bytes available, is available {}.", buffer.readableBytes(), is.available()); } checkTag(is, RpcEncoder.RAW_BODY_TAG); dBodyLength = readRawVarint32(is); if (buffer.readableBytes() != dBodyLength) { throw new CorruptedFrameException(String.format( "Expected to receive a raw body of %d bytes but received a buffer with %d bytes.", dBodyLength, buffer.readableBytes())); } dBody = buffer.slice(); dBody.retain(1); if (RpcConstants.EXTRA_DEBUGGING) { logger.debug("Read raw body of {}", dBody); } } else { if (RpcConstants.EXTRA_DEBUGGING) { logger.debug("No need to read raw body, no readable bytes left."); } } // return the rpc message. InboundRpcMessage m = new InboundRpcMessage(header.getMode(), header.getRpcType(), header.getCoordinationId(), pBody, dBody); // move the reader index forward so the next rpc call won't try to work with it. buffer.skipBytes(dBodyLength); messageCounter.incrementAndGet(); if (RpcConstants.SOME_DEBUGGING) { logger.debug("Inbound Rpc Message Decoded {}.", m); } out.add(m); }
From source file:org.curioswitch.gradle.plugins.gcloud.buildcache.CloudStorageBuildCacheService.java
License:Open Source License
@Override public boolean load(BuildCacheKey buildCacheKey, BuildCacheEntryReader buildCacheEntryReader) { final ByteBuf data; try {/*w ww . j av a 2s .c o m*/ data = cloudStorage.readFile(buildCacheKey.getHashCode()).join(); } catch (Throwable t) { logger.warn("Exception reading from build cache.", t); return false; } if (data == null) { return false; } try (ByteBufInputStream s = new ByteBufInputStream(data, true)) { buildCacheEntryReader.readFrom(s); } catch (Throwable t) { logger.warn("Exception processing cloud storage data.", t); return false; } return true; }
From source file:org.curioswitch.scrapers.instagram.server.util.SharedDataExtractor.java
License:Open Source License
public <T> T extractSharedData(AggregatedHttpResponse page, Class<T> dataType) { final InputStream contentStream; if (page.content() instanceof ByteBufHolder) { contentStream = new ByteBufInputStream(((ByteBufHolder) page.content()).content(), true); } else {//from w ww .j a va 2 s. c o m contentStream = new ByteArrayInputStream(page.content().array()); } final Document doc; try (contentStream) { doc = Jsoup.parse(contentStream, null, "https://www.instagram.com/"); } catch (IOException e) { throw new UncheckedIOException("IOException from memory buffer, can't happen.", e); } var scripts = doc.getElementsByTag("script"); return scripts.stream().filter(script -> script.html().trim().startsWith("window._sharedData = ")).findAny() .map(element -> { String json = element.html().substring("window._sharedData = ".length()); try { return objectMapper.readValue(json, dataType); } catch (IOException e) { throw new UncheckedIOException("Could not parse JSON.", e); } }).orElseThrow(() -> new IllegalArgumentException( "No shared data on page, is this a valid Instagram page?")); }
From source file:org.springframework.core.io.buffer.NettyDataBuffer.java
License:Apache License
@Override public InputStream asInputStream(boolean releaseOnClose) { return new ByteBufInputStream(this.byteBuf, releaseOnClose); }