public FileReporter(String path, Map<String, String> query, Map<String, MetricExtractor> allExtractors) throws FileNotFoundException { boolean append = Boolean.parseBoolean(query.getOrDefault("append", "false")); boolean tee = Boolean.parseBoolean(query.getOrDefault("tee", "false")); boolean includesSysOutOrError = false; OutputStream out = null; if (path == null || "/dev/stdout".equals(path)) { out = new NoCloseOutputStream(System.out); includesSysOutOrError = true; tee = false; } else if ("/dev/stderr".equals(path)) { out = new NoCloseOutputStream(System.err); includesSysOutOrError = true; tee = false; } else { out = new FileOutputStream(path, append); } if (tee) { out = new TeeOutputStream(new NoCloseOutputStream(System.out), out); includesSysOutOrError = true; } this.out = new PrintStream(out); //Copy it in case we want to modify it this.allExtractors = new LinkedHashMap<>(allExtractors); this.includesSysOutOrError = includesSysOutOrError; }
@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); } }
@Override public void write(int b) throws IOException { TeeOutputStream write = new TeeOutputStream(BodyCacheHttpServletResponseWrapper.super.getOutputStream(), byteArrayOutputStream); write.write(b); } };
@Override public void write(int b) throws IOException { tee.write(b); } };
@Override public void flush() throws IOException { this.teeOutputStream.flush(); }
@Override public void close() throws IOException { this.teeOutputStream.close(); } }
/** * 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 write(int b) throws IOException { TeeOutputStream write = new TeeOutputStream(BodyCacheHttpServletResponseWrapper.super.getOutputStream(), byteArrayOutputStream); write.write(b); } };
@Override public void write(int b) throws IOException { tee.write(b); } };
public void flush() throws IOException { this.wrapper.flush(); }
@Override public void close() throws IOException { this.teeOutputStream.close(); }
@BeforeEach void setupLogger() { systemOut = System.out; systemErr = System.err; loggingOutputStream = new ByteArrayOutputStream(); System.setErr(new PrintStream(new TeeOutputStream(loggingOutputStream, systemOut), true)); System.setOut(new PrintStream(new TeeOutputStream(loggingOutputStream, systemErr), true)); }
final TeeOutputStream out = new TeeOutputStream(System.out, outFile); final PrintStream ps = new PrintStream(out); System.setOut(ps); out.close();
@Override public void write(int b) throws IOException { tee.write(b); } };
@Override public void flush() throws IOException { try { this.teeOutputStream.flush(); } catch (IOException ex) { listener.onError(ex); throw ex; } }
public void close() throws IOException { this.wrapper.close(); }
@Before public void setupLogger() { systemOut = System.out; systemErr = System.err; loggingOutputStream = new ByteArrayOutputStream(); System.setErr(new PrintStream(new TeeOutputStream(loggingOutputStream, systemOut), true)); System.setOut(new PrintStream(new TeeOutputStream(loggingOutputStream, systemErr), true)); }
@Override public void write(int b) throws IOException { tee.write(b); } };