@Override public InputStream getInputstream() throws IOException { return sizeLimit == null ? fileItem.getInputStream() : new BoundedInputStream(fileItem.getInputStream(), sizeLimit); }
@Test public void testReadArray() throws Exception { BoundedInputStream bounded; final byte[] helloWorld = "Hello World".getBytes(); final byte[] hello = "Hello".getBytes(); bounded = new BoundedInputStream(new ByteArrayInputStream(helloWorld)); compare("limit = -1", helloWorld, IOUtils.toByteArray(bounded)); bounded = new BoundedInputStream(new ByteArrayInputStream(helloWorld), 0); compare("limit = 0", new byte[0], IOUtils.toByteArray(bounded)); bounded = new BoundedInputStream(new ByteArrayInputStream(helloWorld), helloWorld.length); compare("limit = length", helloWorld, IOUtils.toByteArray(bounded)); bounded = new BoundedInputStream(new ByteArrayInputStream(helloWorld), helloWorld.length + 1); compare("limit > length", helloWorld, IOUtils.toByteArray(bounded)); bounded = new BoundedInputStream(new ByteArrayInputStream(helloWorld), helloWorld.length - 6); compare("limit < length", hello, IOUtils.toByteArray(bounded)); }
@Override public InputStream getInputstream() throws IOException { return sizeLimit == null ? part.getInputStream() : new BoundedInputStream(part.getInputStream(), sizeLimit); }
jsonResponseString = IOUtils.toString(new BoundedInputStream(response.getEntity().getContent(), 1024 * 1024), StandardCharsets.UTF_8); responseCode = response.getStatusLine().getStatusCode();
inputStream = new BoundedInputStream(inputStream, cropper.getSizeLimit());
textSizeLimit != NO_TEXT_SIZE_LIMIT ? new BoundedInputStream(in, textSizeLimit) : in; try { return CharsetSupport.readToString(possiblyLimitedIn, charset);
@Test public void testReadSingle() throws Exception { BoundedInputStream bounded; final byte[] helloWorld = "Hello World".getBytes(); final byte[] hello = "Hello".getBytes(); // limit = length bounded = new BoundedInputStream(new ByteArrayInputStream(helloWorld), helloWorld.length); for (int i = 0; i < helloWorld.length; i++) { assertEquals("limit = length byte[" + i + "]", helloWorld[i], bounded.read()); } assertEquals("limit = length end", -1, bounded.read()); // limit > length bounded = new BoundedInputStream(new ByteArrayInputStream(helloWorld), helloWorld.length + 1); for (int i = 0; i < helloWorld.length; i++) { assertEquals("limit > length byte[" + i + "]", helloWorld[i], bounded.read()); } assertEquals("limit > length end", -1, bounded.read()); // limit < length bounded = new BoundedInputStream(new ByteArrayInputStream(helloWorld), hello.length); for (int i = 0; i < hello.length; i++) { assertEquals("limit < length byte[" + i + "]", hello[i], bounded.read()); } assertEquals("limit < length end", -1, bounded.read()); }
private InputStream dataInputStream(long offset, long size) throws IOException { InputStream is; // use LZMA stream if the bundle is compressed if (bundle.header().compressed()) { // create initial input stream if required if (lzma == null) { lzma = lzmaInputStream(); } // recreate stream if the offset is behind long lzmaOffset = lzma.getByteCount(); if (lzmaOffset > offset) { lzma.close(); lzma = lzmaInputStream(); } // skip forward if required if (lzmaOffset < offset) { lzma.skip(offset - lzmaOffset); } is = lzma; } else { in.position(bundle.header().headerSize() + offset); is = in.stream(); } return new BoundedInputStream(is, size); }
InputStream headerIn = new BoundedInputStream(in.stream(), header.compressedDataHeaderSize()); DataReader inData; switch(header.dataHeaderCompressionScheme()) {
@Override public InputStream getDataInputStream(long seekOffset) throws IOException { if (remoteFS != null) { FSDataInputStream ins; try { if (pathHandle != null) { ins = remoteFS.open(pathHandle, conf.getInt(IO_FILE_BUFFER_SIZE_KEY, IO_FILE_BUFFER_SIZE_DEFAULT)); } else { ins = remoteFS.open(new Path(getRemoteURI())); } } catch (UnsupportedOperationException e) { throw new IOException("PathHandle specified, but unsuported", e); } ins.seek(fileOffset + seekOffset); return new BoundedInputStream( new FSDataInputStream(ins), getBlockDataLength()); } else { throw new IOException("Remote filesystem for provided replica " + this + " does not exist"); } }
@Override public Charset detect(InputStream input, Metadata metadata) throws IOException { int limit = getMarkLimit(); input.mark(limit); // Never read more than the first META_TAG_BUFFER_SIZE bytes InputStream limitedStream = new BoundedInputStream(input, limit); PreScanner preScanner = new PreScanner(limitedStream); // The order of priority for detection is: // 1. Byte Order Mark Charset detectedCharset = preScanner.detectBOM(); // 2. Transport-level information (Content-Type HTTP header) if (detectedCharset == null) detectedCharset = charsetFromContentType(metadata); // 3. HTML <meta> tag if (detectedCharset == null) detectedCharset = preScanner.scan(); input.reset(); return detectedCharset; }
/** * Instantiates a new piper. * * @param input * the input * @param output * the output */ public Piper(InputStream input, OutputStream output) { this.input = new BoundedInputStream(input, MAX_COMMAND_OUTPUT_LENGTH); this.output = output; }
private static byte[] copyToByteArray(InputStream is, final int recordLength, boolean enforceLength) throws IOException { BoundedInputStream bis = new BoundedInputStream(is, recordLength); byte[] rawContents = IOUtils.toByteArray(bis); if (enforceLength && rawContents.length != recordLength) { LOG.error("Read " + rawContents.length + " bytes but expected " + recordLength + " bytes. Continuing..."); } return rawContents; } }
@Override protected InputStream createSegment() throws IOException { BoundedInputStream stream = new BoundedInputStream(inputStream, segmentationSize); stream.setPropagateClose(false); return stream; }
@Override protected InputStream createSegment() throws IOException { BoundedInputStream stream = new BoundedInputStream(inputStream, segmentationSize); stream.setPropagateClose(false); return stream; }
BoundedInputStream boundedInputStream(InputStream inputStream, int length) { BoundedInputStream bis = new BoundedInputStream(inputStream, length); // No close() required/ not propagated. bis.setPropagateClose(false); return bis; }
@Override public InputStream getInputStream() throws IOException { FSDataInputStream inputStream = localFS.open(inputFile); inputStream.seek(startOffset); return new BoundedInputStream(inputStream, getSize()); }
@Override protected InputStream createSegment() throws IOException { BoundedInputStream res = new BoundedInputStream(Channels.newInputStream(this.randomAccessFile.getChannel().position(currentSegment * segmentationSize)), segmentationSize); res.setPropagateClose(false) ; return res ; }
@Override protected InputStream createSegment() throws IOException { BoundedInputStream res = new BoundedInputStream(Channels.newInputStream(this.randomAccessFile.getChannel().position(currentSegment * segmentationSize)), segmentationSize); res.setPropagateClose(false) ; return res ; }
protected InputStream normalize(final InputStream in, final TransferStatus status) throws ChecksumException { try { final InputStream bounded = status.getLength() > 0 ? new BoundedInputStream(in, status.getOffset() + status.getLength()) : in; return status.getOffset() > 0 ? StreamCopier.skip(bounded, status.getOffset()) : bounded; } catch(BackgroundException e) { throw new ChecksumException(LocaleFactory.localizedString("Checksum failure", "Error"), e.getMessage(), e); } } }