IOUtils { public static void copyBytes(InputStream in, OutputStream out, int buffSize, boolean close) throws IOException { try { copyBytes(in, out, buffSize); if(close) { out.close(); out = null; in.close(); in = null; } } finally { if(close) { closeStream(out); closeStream(in); } } }  static void copyBytes(InputStream in, OutputStream out, int buffSize, boolean close); static void copyBytes(InputStream in, OutputStream out, int buffSize); static void copyBytes(InputStream in, OutputStream out, Configuration conf); static void copyBytes(InputStream in, OutputStream out, Configuration conf, boolean close); static void copyBytes(InputStream in, OutputStream out, long count,
      boolean close); static int wrappedReadForCompressedData(InputStream is, byte[] buf,
      int off, int len); static void readFully(InputStream in, byte buf[],
      int off, int len); static void skipFully(InputStream in, long len); static void cleanup(Log log, java.io.Closeable... closeables); static void closeStream(java.io.Closeable stream); static void closeSocket(Socket sock); static void writeFully(WritableByteChannel bc, ByteBuffer buf); static void writeFully(FileChannel fc, ByteBuffer buf,
      long offset); static List<String> listDirectory(File dir, FilenameFilter filter);  }
@Test public void testCopyBytesShouldCloseStreamsWhenCloseIsTrue() throws Exception { InputStream inputStream = Mockito.mock(InputStream.class); OutputStream outputStream = Mockito.mock(OutputStream.class); Mockito.doReturn(-1).when(inputStream).read(new byte[1]); IOUtils.copyBytes(inputStream, outputStream, 1, true); Mockito.verify(inputStream, Mockito.atLeastOnce()).close(); Mockito.verify(outputStream, Mockito.atLeastOnce()).close(); }
@Test public void testCopyBytesShouldCloseInputSteamWhenOutputStreamCloseThrowsException() throws Exception { InputStream inputStream = Mockito.mock(InputStream.class); OutputStream outputStream = Mockito.mock(OutputStream.class); Mockito.doReturn(-1).when(inputStream).read(new byte[1]); Mockito.doThrow(new IOException()).when(outputStream).close(); try{ IOUtils.copyBytes(inputStream, outputStream, 1, true); } catch (IOException e) { } Mockito.verify(inputStream, Mockito.atLeastOnce()).close(); Mockito.verify(outputStream, Mockito.atLeastOnce()).close(); }
@Test public void testCopyBytesShouldNotCloseStreamsWhenCloseIsFalse() throws Exception { InputStream inputStream = Mockito.mock(InputStream.class); OutputStream outputStream = Mockito.mock(OutputStream.class); Mockito.doReturn(-1).when(inputStream).read(new byte[1]); IOUtils.copyBytes(inputStream, outputStream, 1, false); Mockito.verify(inputStream, Mockito.atMost(0)).close(); Mockito.verify(outputStream, Mockito.atMost(0)).close(); }
@Test public void testCopyBytesWithCountShouldCloseStreamsWhenCloseIsTrue() throws Exception { InputStream inputStream = Mockito.mock(InputStream.class); OutputStream outputStream = Mockito.mock(OutputStream.class); Mockito.doReturn(-1).when(inputStream).read(new byte[4096], 0, 1); IOUtils.copyBytes(inputStream, outputStream, (long) 1, true); Mockito.verify(inputStream, Mockito.atLeastOnce()).close(); Mockito.verify(outputStream, Mockito.atLeastOnce()).close(); }
@Test public void testCopyBytesWithCountShouldNotCloseStreamsWhenCloseIsFalse() throws Exception { InputStream inputStream = Mockito.mock(InputStream.class); OutputStream outputStream = Mockito.mock(OutputStream.class); Mockito.doReturn(-1).when(inputStream).read(new byte[4096], 0, 1); IOUtils.copyBytes(inputStream, outputStream, (long) 1, false); Mockito.verify(inputStream, Mockito.atMost(0)).close(); Mockito.verify(outputStream, Mockito.atMost(0)).close(); }
@Test public void testCopyBytesWithCountShouldThrowOutTheStreamClosureExceptions() throws Exception { InputStream inputStream = Mockito.mock(InputStream.class); OutputStream outputStream = Mockito.mock(OutputStream.class); Mockito.doReturn(-1).when(inputStream).read(new byte[4096], 0, 1); Mockito.doThrow(new IOException("Exception in closing the stream")).when( outputStream).close(); try { IOUtils.copyBytes(inputStream, outputStream, (long) 1, true); fail("Should throw out the exception"); } catch (IOException e) { assertEquals("Not throwing the expected exception.", "Exception in closing the stream", e.getMessage()); } Mockito.verify(inputStream, Mockito.atLeastOnce()).close(); Mockito.verify(outputStream, Mockito.atLeastOnce()).close(); }