Java Code Examples for java.io.Console#writer()

The following examples show how to use java.io.Console#writer() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ParameterFormat.java    From sis with Apache License 2.0 5 votes vote down vote up
/**
 * Writes the given object to the console using a shared instance of {@code ParameterFormat}.
 */
@SuppressWarnings("UseOfSystemOutOrSystemErr")
static void print(final Object object) {
    final Console console = System.console();
    final Appendable out = (console != null) ? console.writer() : System.out;
    final ParameterFormat f = getSharedInstance(Colors.NAMING);
    try {
        f.format(object, out);
    } catch (IOException e) {
        throw new UncheckedIOException(e);      // Should never happen since we are writing to stdout.
    }
    INSTANCE.set(f);
}
 
Example 2
Source File: FormattableObject.java    From sis with Apache License 2.0 5 votes vote down vote up
/**
 * Prints a string representation of this object to the {@linkplain System#out standard output stream}.
 * If a {@linkplain Console console} is attached to the running JVM (i.e. if the application is run
 * from the command-line and the output is not redirected to a file) and if Apache SIS thinks that
 * the console supports the ANSI escape codes (a.k.a. X3.64), then a syntax coloring will be applied.
 *
 * <p>This is a convenience method for debugging purpose and for console applications.</p>
 */
@Debug
@SuppressWarnings("UseOfSystemOutOrSystemErr")
public void print() {
    final Console console = System.console();
    final PrintWriter out = (console != null) ? console.writer() : null;
    final String wkt = formatWKT(Convention.WKT2_SIMPLIFIED, (out != null) && X364.isAnsiSupported(), false);
    if (out != null) {
        out.println(wkt);
    } else {
        System.out.println(wkt);
    }
}
 
Example 3
Source File: ShapeUtilitiesViewer.java    From sis with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new panel where to paint the input and output values.
 */
@SuppressWarnings("UseOfSystemOutOrSystemErr")
private ShapeUtilitiesViewer() {
    setBackground(Color.BLACK);
    input  = new Path2D.Float();
    output = new Path2D.Float();
    random = new Random();
    final Console console = System.console();
    out = (console != null) ? console.writer() : new PrintWriter(System.out);
}
 
Example 4
Source File: TestCase.java    From sis with Apache License 2.0 4 votes vote down vote up
/**
 * Invoked by {@link TestRunner} in order to flush the {@link #out} stream.
 * The stream content will be flushed to the {@linkplain System#console() console}
 * if available, or to the {@linkplain System#out standard output stream} otherwise.
 * This method clears the stream buffer.
 */
@SuppressWarnings("UseOfSystemOutOrSystemErr")
static void flushOutput() {
    System.out.flush();
    System.err.flush();
    synchronized (buffer) {             // This is the lock used by the 'out' PrintWriter.
        out.flush();
        /*
         * Get the text content and remove the trailing spaces
         * (including line feeds), if any.
         */
        String content = buffer.toString();
        int length = content.length();
        do if (length == 0) return;
        while (Character.isWhitespace(content.charAt(--length)));
        content = content.substring(0, ++length);
        /*
         * Get the output writer, using the specified encoding if any.
         */
        PrintWriter writer = null;
        final String encoding = System.getProperty(TestConfiguration.OUTPUT_ENCODING_KEY);
        if (encoding == null) {
            final Console console = System.console();
            if (console != null) {
                writer = console.writer();
            }
        }
        if (writer == null) {
            if (encoding != null) try {
                writer = new PrintWriter(new OutputStreamWriter(System.out, encoding));
            } catch (UnsupportedEncodingException e) {
                // Ignore. We will use the default encoding.
            }
            if (writer == null) {
                writer = new PrintWriter(System.out);
            }
        }
        writer.println(content);
        writer.flush();
        buffer.getBuffer().setLength(0);
    }
}
 
Example 5
Source File: FormattedOutputCommand.java    From sis with Apache License 2.0 2 votes vote down vote up
/**
 * Returns {@code true} if {@link #out} is sending its output to the console.
 * If not, then we are probably writing to a file or the user specified his own encoding.
 * In such case, we will send the XML output to an {@code OutputStream} instead than to a
 * {@code Writer} and let the marshaller apply the encoding itself.
 */
private boolean isConsole() {
    if (outputBuffer != null) return true;                      // Special case for JUnit tests only.
    final Console console = System.console();
    return (console != null) && console.writer() == out;
}