/** * Fetches entire contents of an <code>InputStream</code> and represent * same data as result InputStream. * <p> * This method is useful where, * <ul> * <li>Source InputStream is slow.</li> * <li>It has network resources associated, so we cannot keep it open for * long time.</li> * <li>It has network timeout associated.</li> * </ul> * It can be used in favor of {@link #toByteArray()}, since it * avoids unnecessary allocation and copy of byte[].<br> * This method buffers the input internally, so there is no need to use a * <code>BufferedInputStream</code>. * * @param input Stream to be fully buffered. * @param size the initial buffer size * @return A fully buffered stream. * @throws IOException if an I/O error occurs * @since 2.5 */ public static InputStream toBufferedInputStream(final InputStream input, final int size) throws IOException { // It does not matter if a ByteArrayOutputStream is not closed as close() is a no-op @SuppressWarnings("resource") final ByteArrayOutputStream output = new ByteArrayOutputStream(size); output.write(input); return output.toInputStream(); }
@Test public void testToInputStream() throws IOException { final ByteArrayOutputStream baout = new ByteArrayOutputStream(); final java.io.ByteArrayOutputStream ref = new java.io.ByteArrayOutputStream(); //Write 8224 bytes writeData(baout, ref, 32); for(int i=0;i<128;i++) { writeData(baout, ref, 64); } //Get data before more writes final InputStream in = baout.toInputStream(); byte refData[] = ref.toByteArray(); //Write some more data writeData(baout, ref, new int[] { 2, 4, 8, 16 }); //Check original data byte baoutData[] = IOUtils.toByteArray(in); assertEquals(8224, baoutData.length); checkByteArrays(refData, baoutData); //Check all data written baoutData = IOUtils.toByteArray(baout.toInputStream()); refData = ref.toByteArray(); assertEquals(8254, baoutData.length); checkByteArrays(refData, baoutData); baout.close(); in.close(); }
@Test public void testToInputStreamWithReset() throws IOException { //Make sure reset() do not destroy InputStream returned from toInputStream() final ByteArrayOutputStream baout = new ByteArrayOutputStream(); final java.io.ByteArrayOutputStream ref = new java.io.ByteArrayOutputStream(); //Write 8224 bytes writeData(baout, ref, 32); for(int i=0;i<128;i++) { writeData(baout, ref, 64); } //Get data before reset final InputStream in = baout.toInputStream(); byte refData[] = ref.toByteArray(); //Reset and write some new data baout.reset(); ref.reset(); writeData(baout, ref, new int[] { 2, 4, 8, 16 }); //Check original data byte baoutData[] = IOUtils.toByteArray(in); assertEquals(8224, baoutData.length); checkByteArrays(refData, baoutData); //Check new data written after reset baoutData = IOUtils.toByteArray(baout.toInputStream()); refData = ref.toByteArray(); assertEquals(30, baoutData.length); checkByteArrays(refData, baoutData); baout.close(); in.close(); }
/** * Fetches entire contents of an <code>InputStream</code> and represent * same data as result InputStream. * <p> * This method is useful where, * <ul> * <li>Source InputStream is slow.</li> * <li>It has network resources associated, so we cannot keep it open for * long time.</li> * <li>It has network timeout associated.</li> * </ul> * It can be used in favor of {@link #toByteArray()}, since it * avoids unnecessary allocation and copy of byte[].<br> * This method buffers the input internally, so there is no need to use a * <code>BufferedInputStream</code>. * * @param input Stream to be fully buffered. * @param size the initial buffer size * @return A fully buffered stream. * @throws IOException if an I/O error occurs * @since 2.5 */ public static InputStream toBufferedInputStream(final InputStream input, final int size) throws IOException { // It does not matter if a ByteArrayOutputStream is not closed as close() is a no-op @SuppressWarnings("resource") final ByteArrayOutputStream output = new ByteArrayOutputStream(size); output.write(input); return output.toInputStream(); }
/** * Fetches entire contents of an <code>InputStream</code> and represent * same data as result InputStream. * <p> * This method is useful where, * <ul> * <li>Source InputStream is slow.</li> * <li>It has network resources associated, so we cannot keep it open for * long time.</li> * <li>It has network timeout associated.</li> * </ul> * It can be used in favor of {@link #toByteArray()}, since it * avoids unnecessary allocation and copy of byte[].<br> * This method buffers the input internally, so there is no need to use a * <code>BufferedInputStream</code>. * * @param input Stream to be fully buffered. * @param size the initial buffer size * @return A fully buffered stream. * @throws IOException if an I/O error occurs * @since 2.5 */ public static InputStream toBufferedInputStream(final InputStream input, int size) throws IOException { // It does not matter if a ByteArrayOutputStream is not closed as close() is a no-op @SuppressWarnings("resource") final ByteArrayOutputStream output = new ByteArrayOutputStream(size); output.write(input); return output.toInputStream(); }
InputStream fakeStream = bufferStream.toInputStream(); return bufferStream.toInputStream();
bufferStream.write(inputStream); IOUtils.closeQuietly(inputStream); inputStream = bufferStream.toInputStream(); SnapConstants.SNAP_VIDEO_DECRYPTOR_METHOD_DECRYPT, param.args[0], new BufferedInputStream(bufferStream.toInputStream()), param.args[2], param.args[3],
streamFactory.newInputStream(os.toInputStream()), pageMeta); pageDocs.add(pageDoc);