@Override public InputStream getInputstream() throws IOException { return sizeLimit == null ? fileItem.getInputStream() : new BoundedInputStream(fileItem.getInputStream(), sizeLimit); }
@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()); }
@Override protected InputStream createSegment() throws IOException { BoundedInputStream stream = new BoundedInputStream(inputStream, segmentationSize); stream.setPropagateClose(false); return stream; }
public String nextLog() throws IOException { if (currentLogData != null && currentLogLength > 0) { // seek to the end of the current log, relying on BoundedInputStream // to prevent seeking past the end of the current log do { if (currentLogData.skip(currentLogLength) < 0) { break; } } while (currentLogData.read() != -1); } currentLogType = null; currentLogLength = 0; currentLogData = null; currentLogISR = null; try { String logType = valueStream.readUTF(); String logLengthStr = valueStream.readUTF(); currentLogLength = Long.parseLong(logLengthStr); currentLogData = new BoundedInputStream(valueStream, currentLogLength); currentLogData.setPropagateClose(false); currentLogISR = new InputStreamReader(currentLogData, Charset.forName("UTF-8")); currentLogType = logType; } catch (EOFException e) { } return currentLogType; }
boundedInputStream = new BoundedInputStream(inputStream, (endbyte - startbyte) + 1); boundedInputStream.setPropagateClose(false); while ((readBytes = boundedInputStream.read(buffer)) != -1)
/** * Invokes the delegate's <code>read(byte[])</code> method. * @param b the buffer to read the bytes into * @return the number of bytes read or -1 if the end of stream or * the limit has been reached. * @throws IOException if an I/O error occurs */ @Override public int read(final byte[] b) throws IOException { return this.read(b, 0, b.length); }
public long skip(long n) throws IOException { return currentLogData.skip(n); }
@Override protected InputStream createSegment() throws IOException { BoundedInputStream stream = new BoundedInputStream(inputStream, segmentationSize); stream.setPropagateClose(false); return stream; }
public String nextLog() throws IOException { if (currentLogData != null && currentLogLength > 0) { // seek to the end of the current log, relying on BoundedInputStream // to prevent seeking past the end of the current log do { if (currentLogData.skip(currentLogLength) < 0) { break; } } while (currentLogData.read() != -1); } currentLogType = null; currentLogLength = 0; currentLogData = null; currentLogISR = null; try { String logType = valueStream.readUTF(); String logLengthStr = valueStream.readUTF(); currentLogLength = Long.parseLong(logLengthStr); currentLogData = new BoundedInputStream(valueStream, currentLogLength); currentLogData.setPropagateClose(false); currentLogISR = new InputStreamReader(currentLogData, Charset.forName("UTF-8")); currentLogType = logType; } catch (EOFException e) { } return currentLogType; }
boundedInputStream = new BoundedInputStream(inputStream, (endbyte - startbyte) + 1); boundedInputStream.setPropagateClose(false); while ((readBytes = boundedInputStream.read(buffer)) != -1)
/** * Invokes the delegate's <code>read(byte[])</code> method. * @param b the buffer to read the bytes into * @return the number of bytes read or -1 if the end of stream or * the limit has been reached. * @throws IOException if an I/O error occurs */ @Override public int read(byte[] b) throws IOException { return this.read(b, 0, b.length); }
public long skip(long n) throws IOException { return currentLogData.skip(n); }
@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)); }
BoundedInputStream boundedInputStream(InputStream inputStream, int length) { BoundedInputStream bis = new BoundedInputStream(inputStream, length); // No close() required/ not propagated. bis.setPropagateClose(false); return bis; }
public String nextLog() throws IOException { if (currentLogData != null && currentLogLength > 0) { // seek to the end of the current log, relying on BoundedInputStream // to prevent seeking past the end of the current log do { if (currentLogData.skip(currentLogLength) < 0) { break; } } while (currentLogData.read() != -1); } currentLogType = null; currentLogLength = 0; currentLogData = null; currentLogISR = null; try { String logType = valueStream.readUTF(); String logLengthStr = valueStream.readUTF(); currentLogLength = Long.parseLong(logLengthStr); currentLogData = new BoundedInputStream(valueStream, currentLogLength); currentLogData.setPropagateClose(false); currentLogISR = new InputStreamReader(currentLogData, Charset.forName("UTF-8")); currentLogType = logType; } catch (EOFException e) { } return currentLogType; }
/** * Invokes the delegate's <code>read(byte[])</code> method. * @param b the buffer to read the bytes into * @return the number of bytes read or -1 if the end of stream or * the limit has been reached. * @throws IOException if an I/O error occurs */ @Override public int read(byte[] b) throws IOException { return this.read(b, 0, b.length); }
public long skip(long n) throws IOException { return currentLogData.skip(n); }
@Override public InputStream getInputstream() throws IOException { return sizeLimit == null ? part.getInputStream() : new BoundedInputStream(part.getInputStream(), sizeLimit); }
@Override protected InputStream createSegment() throws IOException { BoundedInputStream res = new BoundedInputStream(Channels.newInputStream(this.randomAccessFile.getChannel().position(currentSegment * segmentationSize)), segmentationSize); res.setPropagateClose(false) ; return res ; }
/** * Invokes the delegate's <code>read(byte[])</code> method. * @param b the buffer to read the bytes into * @return the number of bytes read or -1 if the end of stream or * the limit has been reached. * @throws IOException if an I/O error occurs */ @Override public int read(final byte[] b) throws IOException { return this.read(b, 0, b.length); }