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; }
@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)); }
@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)); }
json.writeCanonical(new OutputStreamWriter(new TeeOutputStream(dos,sos), Charsets.UTF_8)).close();
PrintStream startupOut = new PrintStream( new TeeOutputStream( System.out, startupOutStream));
@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(); }
System.setProperty( "LOG_PATH", path.toString() ); final FileOutputStream fos = new FileOutputStream( path.toFile() ); System.setOut( new PrintStream( new TeeOutputStream( originalSystemOut, fos ) ) ); System.setErr( new PrintStream( new TeeOutputStream( originalSystemErr, fos ) ) ); KettleLogStore.OriginalSystemOut = System.out; KettleLogStore.OriginalSystemErr = System.err;
/** * 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); } }
public static void main(String[] args) throws IOException { TeeOutputStream out = new TeeOutputStream(System.out, System.out); out.write("Hello world!".getBytes()); out.flush(); out.close(); }
try { FileOutputStream fos = new FileOutputStream(f); //we will want to print in standard "System.out" and in "file" TeeOutputStream myOut=new TeeOutputStream(System.out, fos); PrintStream ps = new PrintStream(myOut); System.setOut(ps); } catch (Exception e) { e.printStackTrace(); }
DelegatingServletOutputStream(ServletOutputStream source) { Preconditions.checkArgument(source != null, "source ServletOutputStream must not be null"); this.source = source; this.target = new ByteArrayOutputStream(); this.wrapper = new TeeOutputStream(source, this.target); }
private void writeFiles(final URL url, final Path resourceFile, final Path compressedFile) throws IOException { try (InputStream inputStream = urlOpener.getInputStream(url); OutputStream outputFile = newOutputStream(resourceFile); OutputStream compressedOutputFile = newOutputStream(compressedFile); GZIPOutputStream gzipOutput = new GZIPOutputStream(compressedOutputFile); TeeOutputStream combinedOutput = new TeeOutputStream(outputFile, gzipOutput)) { ByteStreams.copy(inputStream, combinedOutput); } }
@Override public void write(int b) throws IOException { TeeOutputStream write = new TeeOutputStream(BodyCacheHttpServletResponseWrapper.super.getOutputStream(), byteArrayOutputStream); write.write(b); } };
private void runBeeline(String[] args, String logFile) throws Exception { // We do this instead of calling BeeLine.main so we can duplicate the error stream for harvesting Hadoop child job IDs BeeLine beeLine = new BeeLine(); beeLine.setErrorStream(new PrintStream(new TeeOutputStream(System.err, new FileOutputStream(logFile)), false, "UTF-8")); int status = beeLine.begin(args, null); beeLine.close(); if (status != 0) { System.exit(status); } }
private void runBeeline(String[] args, String logFile) throws Exception { // We do this instead of calling BeeLine.main so we can duplicate the error stream for harvesting Hadoop child job IDs BeeLine beeLine = new BeeLine(); beeLine.setErrorStream(new PrintStream(new TeeOutputStream(System.err, new FileOutputStream(logFile)), false, "UTF-8")); int status = beeLine.begin(args, null); beeLine.close(); if (status != 0) { System.exit(status); } }
/** creates a capturing context which sees the output to this stream, without interrupting the original target */ public OutputCapturingContext captureTee() { ByteArrayOutputStream out = new ByteArrayOutputStream(); PrintStream toRestore = setThreadLocalPrintStream(new TeeOutputStream(getDelegate(), out)); return new OutputCapturingContext(this, out, toRestore); }
/** creates a capturing context which sees the output to this stream, without interrupting the original target */ public OutputCapturingContext captureTee() { ByteArrayOutputStream out = new ByteArrayOutputStream(); PrintStream toRestore = setThreadLocalPrintStream(new TeeOutputStream(getDelegate(), out)); return new OutputCapturingContext(this, out, toRestore); }
@Override public void execute(ExecSpec execSpec) { execSpec.setCommandLine(_getCommandLine()); execSpec.setEnvironment( _getEnvironment(execSpec.getEnvironment())); execSpec.setErrorOutput( new TeeOutputStream(byteArrayOutputStream, System.out)); execSpec.setStandardOutput( new TeeOutputStream(byteArrayOutputStream, System.out)); execSpec.setWorkingDir(getWorkingDir()); }
@Override public void execute(ExecSpec execSpec) { execSpec.setCommandLine(_getCommandLine()); execSpec.setEnvironment( _getEnvironment(execSpec.getEnvironment())); execSpec.setErrorOutput( new TeeOutputStream(byteArrayOutputStream, System.out)); execSpec.setStandardOutput( new TeeOutputStream(byteArrayOutputStream, System.out)); execSpec.setWorkingDir(getWorkingDir()); }