// Create a stream to hold the output ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintStream ps = new PrintStream(baos); // IMPORTANT: Save the old System.out! PrintStream old = System.out; // Tell Java to use your special stream System.setOut(ps); // Print some output: goes to your special stream System.out.println("Foofoofoo!"); // Put things back System.out.flush(); System.setOut(old); // Show what happened System.out.println("Here: " + baos.toString());
/** * Write the specified message to the internal logging destination. * <p> * Note that this method is private; concrete subclasses of this class * should not call it because the diagnosticPrefix string this * method puts in front of all its messages is LogFactory@...., * while subclasses should put SomeSubClass@... * <p> * Subclasses should instead compute their own prefix, then call * logRawDiagnostic. Note that calling isDiagnosticsEnabled is * fine for subclasses. * <p> * Note that it is safe to call this method before initDiagnostics * is called; any output will just be ignored (as isDiagnosticsEnabled * will return false). * * @param msg is the diagnostic message to be output. */ private static final void logDiagnostic(String msg) { if (diagnosticsStream != null) { diagnosticsStream.print(diagnosticPrefix); diagnosticsStream.println(msg); diagnosticsStream.flush(); } }
public ProcessWrapper sendInput(final String input) { checkStarting(); final PrintStream ps = new PrintStream(this.process.getOutputStream()); ps.println(input); ps.flush(); return this; }
/** * Gets the stack trace from a Throwable as a String. * * @param cause the {@link Throwable} to be examined * @return the stack trace as generated by {@link Throwable#printStackTrace(java.io.PrintWriter)} method. */ public static String stackTraceToString(Throwable cause) { ByteArrayOutputStream out = new ByteArrayOutputStream(); PrintStream pout = new PrintStream(out); cause.printStackTrace(pout); pout.flush(); try { return new String(out.toByteArray()); } finally { try { out.close(); } catch (IOException ignore) { // ignore as should never happen } } }
private static void sendRequest(OkHttpClient client, String url) { System.out.printf("%-40s ", url); System.out.flush(); System.out.println(Platform.get()); Request request = new Request.Builder().url(url).build(); try (Response response = client.newCall(request).execute()) { Handshake handshake = response.handshake(); System.out.println(handshake.tlsVersion() + " " + handshake.cipherSuite() + " " + response.protocol() + " " + response.code + " " + response.body.bytes().length + "b"); } catch (IOException ioe) { System.out.println(ioe.toString()); } } }
long currTime = System.currentTimeMillis(); long sentPerSec = linesProcessed / ( (currTime - startTime) / 1000 ); debugStream.println('[' + Redwood.formatTimeDifference(currTime - startTime) + "] Processed " + linesProcessed + " sentences {" + sentPerSec + " sentences / second}... "); debugStream.println("CAUGHT EXCEPTION ON SENTENCE ID: " + id + " (-1 if not known)"); t.printStackTrace(debugStream); exceptions += 1; debugStream.println('[' + Redwood.formatTimeDifference(System.currentTimeMillis() - startTime) + "] DONE"); } catch (Throwable t) { debugStream.println("FATAL EXCEPTION!"); exceptions += 1; } finally { debugStream.flush(); debugStream.close();
private String record(Runnable runnable) { ByteArrayOutputStream stream = new ByteArrayOutputStream(); PrintStream written = new PrintStream(stream); setStream(written); runnable.run(); written.flush(); String result = stream.toString(); quiet(() -> { written.close(); return null; }); return result; }
/** * Encode a string to the output. */ private void encodeString(String n) { PrintStream writer = new PrintStream(out); writer.print(n); writer.flush(); }
@Override public void onScanCompleted(@NotNull PrintStream log) { super.onScanCompleted(log); OutputStream outputStream = null; PrintStream stream = null; try { outputStream = new FileOutputStream("../wiki/ImageDatabaseSummary.md", false); stream = new PrintStream(outputStream, false); writeOutput(stream); stream.flush(); } catch (IOException e) { e.printStackTrace(); } finally { if (stream != null) stream.close(); if (outputStream != null) try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } }
/** * Renders the AST into 'ASCII art' form and returns that string representation. * * @param ast The AST to display. * @param header The header for the display. * * @return The AST in 'ASCII art' form, as a string. */ public String showAsString(AST ast, String header) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintStream ps = new PrintStream( baos ); ps.println( header ); showAst( ast, ps ); ps.flush(); return new String( baos.toByteArray() ); }
/** * Write the specified message to the internal logging destination. * <p> * Note that this method is private; concrete subclasses of this class * should not call it because the diagnosticPrefix string this * method puts in front of all its messages is LogFactory@...., * while subclasses should put SomeSubClass@... * <p> * Subclasses should instead compute their own prefix, then call * logRawDiagnostic. Note that calling isDiagnosticsEnabled is * fine for subclasses. * <p> * Note that it is safe to call this method before initDiagnostics * is called; any output will just be ignored (as isDiagnosticsEnabled * will return false). * * @param msg is the diagnostic message to be output. */ private static final void logDiagnostic(String msg) { if (diagnosticsStream != null) { diagnosticsStream.print(diagnosticPrefix); diagnosticsStream.println(msg); diagnosticsStream.flush(); } }
private void run(VMDetailView view) throws Exception { try { System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream(FileDescriptor.out)), false)); System.out.flush(); Utils.sleep(sleepSeconds * 1000L); System.out.println(""); System.out.flush(); } catch (NoClassDefFoundError e) { e.printStackTrace(System.out); System.out.println(Formats.red("ERROR: Some JDK classes cannot be found.")); System.out.println(" Please check if the JAVA_HOME environment variable has been set to a JDK path."); System.out.println(""); System.out.flush();
/** * Gets the stack trace from a Throwable as a String. * * @param cause the {@link Throwable} to be examined * @return the stack trace as generated by {@link Throwable#printStackTrace(java.io.PrintWriter)} method. */ public static String stackTraceToString(Throwable cause) { ByteArrayOutputStream out = new ByteArrayOutputStream(); PrintStream pout = new PrintStream(out); cause.printStackTrace(pout); pout.flush(); try { return new String(out.toByteArray()); } finally { try { out.close(); } catch (IOException ignore) { // ignore as should never happen } } }
/** * 打印单条线程的stack strace,会造成停顿,但比获取全部线程的stack trace停顿少 */ public void printStack(long tid) throws IOException { System.out.printf("%n Stack trace of thread %d:%n", tid); ThreadInfo info = view.vmInfo.getThreadInfo(tid, 20); if (info == null) { System.err.println(" TID not exist:" + tid); return; } printSingleThread(info); System.out.flush(); }
public static void main(String[] args) throws IOException { byte[] bytes = new byte[1024]; System.out.write(System.getProperty("user.dir").getBytes()); System.out.println(":ModifiedResult"); int numRead = 0; while ((numRead = System.in.read(bytes)) != -1) { System.out.write(bytes, 0, numRead); } System.out.flush(); System.out.close(); }