Class AbstractWindowsConsoleWriter

  • All Implemented Interfaces:
    java.io.Closeable, java.io.Flushable, java.lang.Appendable, java.lang.AutoCloseable

    public abstract class AbstractWindowsConsoleWriter
    extends java.io.Writer
    Base class for writing to Windows console.

    The AbstractWindowsConsoleWriter class provides a foundation for writing text to the Windows console. It extends the standard Writer class and handles the common aspects of writing to the console, while leaving the actual console interaction to be implemented by concrete subclasses.

    This class is necessary because standard Java output streams don't work well with the Windows console, particularly for non-ASCII characters and color output. Instead of using standard output streams, Windows terminal implementations use this writer to directly interact with the Windows console API.

    Concrete subclasses must implement the writeConsole(char[], int) method to perform the actual writing to the console using platform-specific mechanisms (e.g., JNI, JNA, or FFM).

    See Also:
    Writer
    • Field Summary

      • Fields inherited from class java.io.Writer

        lock
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      void close()  
      void flush()  
      void write​(char[] cbuf, int off, int len)
      Writes a portion of a character array to the Windows console.
      protected abstract void writeConsole​(char[] text, int len)
      Writes text to the Windows console.
      • Methods inherited from class java.io.Writer

        append, append, append, nullWriter, write, write, write, write
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • AbstractWindowsConsoleWriter

        public AbstractWindowsConsoleWriter()
    • Method Detail

      • writeConsole

        protected abstract void writeConsole​(char[] text,
                                             int len)
                                      throws java.io.IOException
        Writes text to the Windows console.

        This method must be implemented by concrete subclasses to perform the actual writing to the Windows console using platform-specific mechanisms. The implementation should handle proper encoding and display of characters, including non-ASCII characters and ANSI escape sequences if supported.

        Parameters:
        text - the character array containing the text to write
        len - the number of characters to write
        Throws:
        java.io.IOException - if an I/O error occurs
      • write

        public void write​(char[] cbuf,
                          int off,
                          int len)
                   throws java.io.IOException
        Writes a portion of a character array to the Windows console.

        This method handles the common logic for writing to the console, including creating a new character array if the offset is not zero and synchronizing access to the console. The actual writing is delegated to the writeConsole(char[], int) method implemented by subclasses.

        Specified by:
        write in class java.io.Writer
        Parameters:
        cbuf - the character array containing the text to write
        off - the offset from which to start reading characters
        len - the number of characters to write
        Throws:
        java.io.IOException - if an I/O error occurs
      • flush

        public void flush()
        Specified by:
        flush in interface java.io.Flushable
        Specified by:
        flush in class java.io.Writer
      • close

        public void close()
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Specified by:
        close in class java.io.Writer