@Test public void testTee() throws IOException { final ByteArrayOutputStream baos1 = new ByteArrayOutputStream(); final ByteArrayOutputStream baos2 = new ByteArrayOutputStream(); final TeeOutputStream tos = new TeeOutputStream(baos1, baos2); for (int i = 0; i < 20; i++) { tos.write(i); } assertByteArrayEquals("TeeOutputStream.write(int)", baos1.toByteArray(), baos2.toByteArray()); final byte[] array = new byte[10]; for (int i = 20; i < 30; i++) { array[i - 20] = (byte) i; } tos.write(array); assertByteArrayEquals("TeeOutputStream.write(byte[])", baos1.toByteArray(), baos2.toByteArray()); for (int i = 25; i < 35; i++) { array[i - 25] = (byte) i; } tos.write(array, 5, 5); assertByteArrayEquals("TeeOutputStream.write(byte[], int, int)", baos1.toByteArray(), baos2.toByteArray()); tos.flush(); tos.close(); }
/** * Tests that the branch {@code OutputStream} is closed when closing the main {@code OutputStream} throws an * exception on {@link TeeOutputStream#close()}. */ @Test public void testCloseBranchIOException() { final ByteArrayOutputStream badOs = new ExceptionOnCloseByteArrayOutputStream(); final RecordCloseByteArrayOutputStream goodOs = new RecordCloseByteArrayOutputStream(); final TeeOutputStream tos = new TeeOutputStream(goodOs, badOs); try { tos.close(); Assert.fail("Expected " + IOException.class.getName()); } catch (final IOException e) { Assert.assertTrue(goodOs.closed); } }
/** * Tests that the main {@code OutputStream} is closed when closing the branch {@code OutputStream} throws an * exception on {@link TeeOutputStream#close()}. */ @Test public void testCloseMainIOException() { final ByteArrayOutputStream badOs = new ExceptionOnCloseByteArrayOutputStream(); final RecordCloseByteArrayOutputStream goodOs = new RecordCloseByteArrayOutputStream(); final TeeOutputStream tos = new TeeOutputStream(badOs, goodOs); try { tos.close(); Assert.fail("Expected " + IOException.class.getName()); } catch (final IOException e) { Assert.assertTrue(goodOs.closed); } }
@Override public void close() throws IOException { this.teeOutputStream.close(); } }
public void close() throws IOException { this.wrapper.close(); }
@Override public void close() throws IOException { this.teeOutputStream.close(); }