< prev index next >
src/jdk.jdi/share/classes/com/sun/tools/example/debug/tty/MessageOutput.java
Print this page
*** 1,7 ****
/*
! * Copyright (c) 2001, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
--- 1,7 ----
/*
! * Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
*** 31,40 ****
--- 31,42 ----
* this sample code.
*/
package com.sun.tools.example.debug.tty;
+ import java.io.ByteArrayOutputStream;
+ import java.io.PrintWriter;
import java.util.*;
import java.text.MessageFormat;
/**
* Internationalization (i18n) convenience methods for jdb.
*
*** 50,59 ****
--- 52,67 ----
* The resource bundle containing localizable message content.
* This is loaded by TTY.main() at start-up
*/
static ResourceBundle textResources;
+ /** If set then used to buffer the output before writing it to System.out */
+ private final static ThreadLocal<BufferedOutput> bufferedOutput = new ThreadLocal<>();
+
+ /** Default writer to System.out */
+ private final static PrintWriter systemOutWriter = new PrintWriter(System.out, true);
+
/** Our message formatter. Allocated once, used many times */
private static MessageFormat messageFormat;
/**
* Fatal shutdown notification. This is sent to System.err
*** 103,178 ****
* Commands.java (multiple locations)
* These are the only sites that should be calling this
* method.
*/
static void printDirectln(String line) {
! System.out.println(line);
}
static void printDirect(String line) {
! System.out.print(line);
}
static void printDirect(char c) {
! System.out.print(c);
}
/**
* Print a newline.
* Use this instead of '\n'
*/
static void println() {
! System.out.println();
}
/**
* Format and print a simple string.
*/
static void print(String key) {
! System.out.print(format(key));
}
/**
* Format and print a simple string.
*/
static void println(String key) {
! System.out.println(format(key));
}
/**
* Fetch, format and print a message with one string argument.
* This is the most common usage.
*/
static void print(String key, String argument) {
! System.out.print(format(key, argument));
}
static void println(String key, String argument) {
! System.out.println(format(key, argument));
}
/**
* Fetch, format and print a message with an arbitrary
* number of message arguments.
*/
static void println(String key, Object [] arguments) {
! System.out.println(format(key, arguments));
}
/**
* Print a newline, followed by the string.
*/
static void lnprint(String key) {
! System.out.println();
! System.out.print(textResources.getString(key));
}
static void lnprint(String key, String argument) {
! System.out.println();
! System.out.print(format(key, argument));
}
static void lnprint(String key, Object [] arguments) {
! System.out.println();
! System.out.print(format(key, arguments));
}
/**
* Print an exception message with a stack trace.
*/
--- 111,186 ----
* Commands.java (multiple locations)
* These are the only sites that should be calling this
* method.
*/
static void printDirectln(String line) {
! getWriter().println(line);
}
static void printDirect(String line) {
! getWriter().print(line);
}
static void printDirect(char c) {
! getWriter().print(c);
}
/**
* Print a newline.
* Use this instead of '\n'
*/
static void println() {
! getWriter().println();
}
/**
* Format and print a simple string.
*/
static void print(String key) {
! getWriter().print(format(key));
}
/**
* Format and print a simple string.
*/
static void println(String key) {
! getWriter().println(format(key));
}
/**
* Fetch, format and print a message with one string argument.
* This is the most common usage.
*/
static void print(String key, String argument) {
! getWriter().print(format(key, argument));
}
static void println(String key, String argument) {
! getWriter().println(format(key, argument));
}
/**
* Fetch, format and print a message with an arbitrary
* number of message arguments.
*/
static void println(String key, Object [] arguments) {
! getWriter().println(format(key, arguments));
}
/**
* Print a newline, followed by the string.
*/
static void lnprint(String key) {
! getWriter().println();
! getWriter().print(textResources.getString(key));
}
static void lnprint(String key, String argument) {
! getWriter().println();
! getWriter().print(format(key, argument));
}
static void lnprint(String key, Object [] arguments) {
! getWriter().println();
! getWriter().print(format(key, arguments));
}
/**
* Print an exception message with a stack trace.
*/
*** 182,209 ****
println(key);
} catch (MissingResourceException mex) {
printDirectln(key);
}
}
! System.out.flush();
! e.printStackTrace();
}
static void printPrompt() {
printPrompt(false);
}
static void printPrompt(boolean simple) {
ThreadInfo threadInfo = ThreadInfo.getCurrentThreadInfo();
if (simple || threadInfo == null) {
! System.out.print
(MessageOutput.format("jdb prompt with no current thread"));
} else {
! System.out.print
(MessageOutput.format("jdb prompt thread name and current stack frame",
new Object [] {
threadInfo.getThread().name(),
Integer.valueOf(threadInfo.getCurrentFrameIndex() + 1)}));
}
! System.out.flush();
}
}
--- 190,267 ----
println(key);
} catch (MissingResourceException mex) {
printDirectln(key);
}
}
! getWriter().flush();
! e.printStackTrace(getWriter());
}
static void printPrompt() {
printPrompt(false);
}
static void printPrompt(boolean simple) {
ThreadInfo threadInfo = ThreadInfo.getCurrentThreadInfo();
if (simple || threadInfo == null) {
! getWriter().print
(MessageOutput.format("jdb prompt with no current thread"));
} else {
! getWriter().print
(MessageOutput.format("jdb prompt thread name and current stack frame",
new Object [] {
threadInfo.getThread().name(),
Integer.valueOf(threadInfo.getCurrentFrameIndex() + 1)}));
}
! getWriter().flush();
! }
!
! /** Starts buffering the output to the internal buffer */
! static void startBuffering() {
! assert (bufferedOutput.get() == null);
! bufferedOutput.set(new BufferedOutput());
! }
!
! /** Stops buffering the output and writes the buffer to System.out */
! static void stopBuffering() {
! BufferedOutput bo = bufferedOutput.get();
! assert (bo != null);
! bufferedOutput.set(null);
! bo.close();
! systemOutWriter.print(bo.toString());
! systemOutWriter.flush();
! }
!
! /**
! * If bufferedOutput is set returns the writer the writes to the internal buffer, otherwise
! * returns writer that writes to System.out
! */
! private static PrintWriter getWriter() {
! return bufferedOutput.get() != null? bufferedOutput.get().getPrintWriter() : systemOutWriter;
! }
!
! /** Provides a writer that buffers the output to the string */
! private static class BufferedOutput {
! private final ByteArrayOutputStream baos;
! private final PrintWriter pw;
!
! BufferedOutput() {
! baos = new ByteArrayOutputStream();
! pw = new PrintWriter(baos);
! }
!
! PrintWriter getPrintWriter() {
! return pw;
! }
!
! void close() {
! pw.flush();
! pw.close();
! }
!
! @Override
! public String toString() {
! return baos.toString();
! }
!
}
}
< prev index next >