/** * Copies chars from a large (over 2GB) <code>Reader</code> to a <code>Writer</code>. * <p> * This method buffers the input internally, so there is no need to use a * <code>BufferedReader</code>. * <p> * The buffer size is given by {@link #DEFAULT_BUFFER_SIZE}. * * @param input the <code>Reader</code> to read from * @param output the <code>Writer</code> to write to * @return the number of characters copied * @throws NullPointerException if the input or output is null * @throws IOException if an I/O error occurs * @since 1.3 */ public static long copyLarge(final Reader input, final Writer output) throws IOException { return copyLarge(input, output, new char[DEFAULT_BUFFER_SIZE]); }
/** * Copies bytes from an <code>InputStream</code> to an <code>OutputStream</code> using an internal buffer of the * given size. * <p> * This method buffers the input internally, so there is no need to use a <code>BufferedInputStream</code>. * <p> * * @param input the <code>InputStream</code> to read from * @param output the <code>OutputStream</code> to write to * @param bufferSize the bufferSize used to copy from the input to the output * @return the number of bytes copied * @throws NullPointerException if the input or output is null * @throws IOException if an I/O error occurs * @since 2.5 */ public static long copy(final InputStream input, final OutputStream output, final int bufferSize) throws IOException { return copyLarge(input, output, new byte[bufferSize]); }
/** * @deprecated Use instead {@link org.apache.commons.io.IOUtils#copyLarge(java.io.InputStream, java.io.OutputStream)} */ @Deprecated public static long copyLarge(InputStream input, OutputStream output) throws IOException { return org.apache.commons.io.IOUtils.copyLarge(input, output); }
/** * @deprecated Use instead {@link org.apache.commons.io.IOUtils#copyLarge(java.io.Reader, java.io.Writer)} */ @Deprecated public static long copyLarge(Reader input, Writer output) throws IOException { return org.apache.commons.io.IOUtils.copyLarge(input, output); }
/** * Copies some or all chars from a large (over 2GB) <code>InputStream</code> to an * <code>OutputStream</code>, optionally skipping input chars. * <p> * This method buffers the input internally, so there is no need to use a * <code>BufferedReader</code>. * <p> * The buffer size is given by {@link #DEFAULT_BUFFER_SIZE}. * * @param input the <code>Reader</code> to read from * @param output the <code>Writer</code> to write to * @param inputOffset : number of chars to skip from input before copying * -ve values are ignored * @param length : number of chars to copy. -ve means all * @return the number of chars copied * @throws NullPointerException if the input or output is null * @throws IOException if an I/O error occurs * @since 2.2 */ public static long copyLarge(final Reader input, final Writer output, final long inputOffset, final long length) throws IOException { return copyLarge(input, output, inputOffset, length, new char[DEFAULT_BUFFER_SIZE]); }
/** * Copy bytes from a <code>File</code> to an <code>OutputStream</code>. * <p> * This method buffers the input internally, so there is no need to use a <code>BufferedInputStream</code>. * </p> * * @param input the <code>File</code> to read from * @param output the <code>OutputStream</code> to write to * @return the number of bytes copied * @throws NullPointerException if the input or output is null * @throws IOException if an I/O error occurs * @since 2.1 */ public static long copyFile(final File input, final OutputStream output) throws IOException { try (FileInputStream fis = new FileInputStream(input)) { return IOUtils.copyLarge(fis, output); } }
public void streamToFile(InputStream stream, File dest) throws IOException { dest.getParentFile().mkdirs(); FileOutputStream out = new FileOutputStream(dest, true); try { IOUtils.copyLarge(stream, out); } finally { IOUtils.closeQuietly(out); } }
@Test public void testCopyLarge_CharSkipInvalid() throws IOException { CharArrayReader is = null; CharArrayWriter os = null; try { // Create streams is = new CharArrayReader(carr); os = new CharArrayWriter(); // Test our copy method IOUtils.copyLarge(is, os, 1000, 100); fail("Should have thrown EOFException"); } catch (final EOFException ignore) { } finally { IOUtils.closeQuietly(is); IOUtils.closeQuietly(os); } }
@Test public void testCopyLarge_SkipInvalid() throws IOException { ByteArrayInputStream is = null; ByteArrayOutputStream os = null; try { // Create streams is = new ByteArrayInputStream(iarr); os = new ByteArrayOutputStream(); // Test our copy method IOUtils.copyLarge(is, os, 1000, 100); fail("Should have thrown EOFException"); } catch (final EOFException ignore) { } finally { IOUtils.closeQuietly(is); IOUtils.closeQuietly(os); } }
@Test public void testCopy_readerToWriter_IO84() throws Exception { final long size = (long)Integer.MAX_VALUE + (long)1; final Reader reader = new NullReader(size); final Writer writer = new NullWriter(); // Test copy() method assertEquals(-1, IOUtils.copy(reader, writer)); // reset the input reader.close(); // Test copyLarge() method assertEquals("copyLarge()", size, IOUtils.copyLarge(reader, writer)); }
@Test public void testCopy_inputStreamToOutputStream_IO84() throws Exception { final long size = (long)Integer.MAX_VALUE + (long)1; final InputStream in = new NullInputStream(size); final OutputStream out = new NullOutputStream(); // Test copy() method assertEquals(-1, IOUtils.copy(in, out)); // reset the input in.close(); // Test copyLarge() method assertEquals("copyLarge()", size, IOUtils.copyLarge(in, out)); }
@Test public void testCopyLarge_CharFullLength() throws IOException { CharArrayReader is = null; CharArrayWriter os = null; try { // Create streams is = new CharArrayReader(carr); os = new CharArrayWriter(); // Test our copy method assertEquals(200, IOUtils.copyLarge(is, os, 0, -1)); final char[] oarr = os.toCharArray(); // check that output length is correct assertEquals(200, oarr.length); // check that output data corresponds to input data assertEquals(1, oarr[1]); assertEquals(79, oarr[79]); assertEquals((char) -1, oarr[80]); } finally { IOUtils.closeQuietly(is); IOUtils.closeQuietly(os); } }
@Test public void testCopyLarge_CharSkip() throws IOException { CharArrayReader is = null; CharArrayWriter os = null; try { // Create streams is = new CharArrayReader(carr); os = new CharArrayWriter(); // Test our copy method assertEquals(100, IOUtils.copyLarge(is, os, 10, 100)); final char[] oarr = os.toCharArray(); // check that output length is correct assertEquals(100, oarr.length); // check that output data corresponds to input data assertEquals(11, oarr[1]); assertEquals(79, oarr[69]); assertEquals((char) -1, oarr[70]); } finally { IOUtils.closeQuietly(is); IOUtils.closeQuietly(os); } }
@Test public void testCopyLarge_FullLength() throws IOException { ByteArrayInputStream is = null; ByteArrayOutputStream os = null; try { // Create streams is = new ByteArrayInputStream(iarr); os = new ByteArrayOutputStream(); // Test our copy method assertEquals(200, IOUtils.copyLarge(is, os, 0, -1)); final byte[] oarr = os.toByteArray(); // check that output length is correct assertEquals(200, oarr.length); // check that output data corresponds to input data assertEquals(1, oarr[1]); assertEquals(79, oarr[79]); assertEquals(-1, oarr[80]); } finally { IOUtils.closeQuietly(is); IOUtils.closeQuietly(os); } }
@Test public void testCopyLarge_Skip() throws IOException { ByteArrayInputStream is = null; ByteArrayOutputStream os = null; try { // Create streams is = new ByteArrayInputStream(iarr); os = new ByteArrayOutputStream(); // Test our copy method assertEquals(100, IOUtils.copyLarge(is, os, 10, 100)); final byte[] oarr = os.toByteArray(); // check that output length is correct assertEquals(100, oarr.length); // check that output data corresponds to input data assertEquals(11, oarr[1]); assertEquals(79, oarr[69]); assertEquals(-1, oarr[70]); } finally { IOUtils.closeQuietly(is); IOUtils.closeQuietly(os); } }
@Test public void testCopyLarge_ExtraLength() throws IOException { ByteArrayInputStream is = null; ByteArrayOutputStream os = null; try { // Create streams is = new ByteArrayInputStream(iarr); os = new ByteArrayOutputStream(); // Test our copy method // for extra length, it reads till EOF assertEquals(200, IOUtils.copyLarge(is, os, 0, 2000)); final byte[] oarr = os.toByteArray(); // check that output length is correct assertEquals(200, oarr.length); // check that output data corresponds to input data assertEquals(1, oarr[1]); assertEquals(79, oarr[79]); assertEquals(-1, oarr[80]); } finally { IOUtils.closeQuietly(is); IOUtils.closeQuietly(os); } }
@Test public void testCopyLarge_SkipWithInvalidOffset() throws IOException { ByteArrayInputStream is = null; ByteArrayOutputStream os = null; try { // Create streams is = new ByteArrayInputStream(iarr); os = new ByteArrayOutputStream(); // Test our copy method assertEquals(100, IOUtils.copyLarge(is, os, -10, 100)); final byte[] oarr = os.toByteArray(); // check that output length is correct assertEquals(100, oarr.length); // check that output data corresponds to input data assertEquals(1, oarr[1]); assertEquals(79, oarr[79]); assertEquals(-1, oarr[80]); } finally { IOUtils.closeQuietly(is); IOUtils.closeQuietly(os); } }
@Test public void testCopyLarge_CharNoSkip() throws IOException { CharArrayReader is = null; CharArrayWriter os = null; try { // Create streams is = new CharArrayReader(carr); os = new CharArrayWriter(); // Test our copy method assertEquals(100, IOUtils.copyLarge(is, os, 0, 100)); final char[] oarr = os.toCharArray(); // check that output length is correct assertEquals(100, oarr.length); // check that output data corresponds to input data assertEquals(1, oarr[1]); assertEquals(79, oarr[79]); assertEquals((char) -1, oarr[80]); } finally { IOUtils.closeQuietly(is); IOUtils.closeQuietly(os); } }
@Test public void testCopyLarge_NoSkip() throws IOException { ByteArrayInputStream is = null; ByteArrayOutputStream os = null; try { // Create streams is = new ByteArrayInputStream(iarr); os = new ByteArrayOutputStream(); // Test our copy method assertEquals(100, IOUtils.copyLarge(is, os, 0, 100)); final byte[] oarr = os.toByteArray(); // check that output length is correct assertEquals(100, oarr.length); // check that output data corresponds to input data assertEquals(1, oarr[1]); assertEquals(79, oarr[79]); assertEquals(-1, oarr[80]); } finally { IOUtils.closeQuietly(is); IOUtils.closeQuietly(os); } }
@Test public void testCopyLarge_CharExtraLength() throws IOException { CharArrayReader is = null; CharArrayWriter os = null; try { // Create streams is = new CharArrayReader(carr); os = new CharArrayWriter(); // Test our copy method // for extra length, it reads till EOF assertEquals(200, IOUtils.copyLarge(is, os, 0, 2000)); final char[] oarr = os.toCharArray(); // check that output length is correct assertEquals(200, oarr.length); // check that output data corresponds to input data assertEquals(1, oarr[1]); assertEquals(79, oarr[79]); assertEquals((char) -1, oarr[80]); } finally { IOUtils.closeQuietly(is); IOUtils.closeQuietly(os); } }