Class WriterPrintStream

All Implemented Interfaces:
Closeable, Flushable, Appendable, AutoCloseable

public class WriterPrintStream extends PrintStream
A print stream that writes to a given writer. This print stream writer assumes that all bytes written to it are characters encoded in the platform default character encoding.
Author:
Garret Wilson
  • Field Summary

    Fields inherited from class java.io.FilterOutputStream

    out
  • Constructor Summary

    Constructors
    Constructor
    Description
    Writer constructor.
    WriterPrintStream(Writer writer, boolean autoFlush)
    Writer and autoflush constructor.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Flushes the stream and check its error state.
    void
    Closes the stream.
    void
    Flushes the stream.
    format(String format, Object... args)
    Writes a formatted string to this output stream using the specified format string and arguments.
    format(Locale locale, String format, Object... args)
    Writes a formatted string to this output stream using the specified format string and arguments.
    void
    print(boolean b)
    Print a boolean value.
    void
    print(char c)
    Print a character.
    void
    print(char[] charArray)
    Print an array of characters.
    void
    print(double d)
    Print a double-precision floating-point number.
    void
    print(float f)
    Print a floating-point number.
    void
    print(int i)
    Print an integer.
    void
    print(long l)
    Print a long integer.
    void
    print(Object object)
    Print an object.
    void
    print(String string)
    Print a string.
    void
    Terminate the current line by writing the line separator string.
    void
    println(boolean b)
    Print a boolean and then terminates the line.
    void
    println(char c)
    Print a character and then terminates the line.
    void
    println(char[] charArray)
    Print an array of characters and then terminates the line.
    void
    println(double d)
    Print a double and then terminates the line.
    void
    println(float x)
    Print a float and then terminates the line.
    void
    println(int x)
    Print an integer and then terminates the line.
    void
    println(long l)
    Print a long and then terminates the line.
    void
    println(Object object)
    Print an object and then terminates the line.
    void
    println(String string)
    Print a string and then terminates the line.
    protected void
    Set the error state of the stream to true.
    void
    write(byte[] buffer, int offset, int length)
    Write the specified number of bytes bytes from the specified byte array starting at the specified offset.
    void
    write(int b)
    Write the specified byte to this stream.
    protected void
    write(String string)
    Writes a string to the underlying writer.
    protected void
    write(String string, boolean flush)
    Writes a string to the underlying writer, optionally flushing the writer.

    Methods inherited from class java.io.PrintStream

    append, append, append, clearError, printf, printf

    Methods inherited from class java.io.FilterOutputStream

    write

    Methods inherited from class java.lang.Object

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

    • WriterPrintStream

      public WriterPrintStream(Writer writer)
      Writer constructor. The stream will not flush automatically when the end of a line is written.
      Parameters:
      writer - The writer to which values and objects will be printed.
      Throws:
      NullPointerException - if the given writer is null.
    • WriterPrintStream

      public WriterPrintStream(Writer writer, boolean autoFlush)
      Writer and autoflush constructor.
      Parameters:
      writer - The writer to which values and objects will be printed.
      autoFlush - true writer should be flushed whenever a byte array is written, one of the println methods is invoked, or a newline character or byte ('\n') is written.
      Throws:
      NullPointerException - if the given writer is null.
  • Method Details

    • flush

      public void flush()
      Flushes the stream. This version delegates to the decorated writer.
      Specified by:
      flush in interface Flushable
      Overrides:
      flush in class PrintStream
    • close

      public void close()
      Closes the stream. This version delegates to the decorated writer.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class PrintStream
    • checkError

      public boolean checkError()
      Flushes the stream and check its error state.
      Overrides:
      checkError in class PrintStream
      Returns:
      true if and only if this stream has encountered an IOException other than InterruptedIOException, or the setError() method has been invoked.
    • setError

      protected void setError()
      Set the error state of the stream to true.
      Overrides:
      setError in class PrintStream
    • write

      public void write(int b)
      Write the specified byte to this stream. If the byte is a newline and automatic flushing is enabled then the flush() method will be invoked. This implementation assumes the given byte specifies a single character.
      Overrides:
      write in class PrintStream
      Parameters:
      b - The byte to be written
      See Also:
    • write

      public void write(byte[] buffer, int offset, int length)
      Write the specified number of bytes bytes from the specified byte array starting at the specified offset. If automatic flushing is enabled then the flush() method will be invoked. Because a PrintWriter is expected to be used for writing characters rather than bytes, and uses the platform's default character encoding, this version converts the bytes back to characters using the platform's default character encoding.
      Overrides:
      write in class PrintStream
      Parameters:
      buffer - A byte array.
      offset - The offset from which to start taking bytes.
      length - The number of bytes to write.
    • write

      protected void write(String string, boolean flush)
      Writes a string to the underlying writer, optionally flushing the writer.
      Parameters:
      string - The string to write.
      flush - true if the writer should be flushed afterwards.
    • write

      protected void write(String string)
      Writes a string to the underlying writer. If automatic flushing is enabled and the string contains the newline character '\\n' then the flush() method will be invoked.
      Parameters:
      string - The string to write.
    • print

      public void print(boolean b)
      Print a boolean value.
      Overrides:
      print in class PrintStream
      Parameters:
      b - The bolean value to be printed
    • print

      public void print(char c)
      Print a character.
      Overrides:
      print in class PrintStream
      Parameters:
      c - The character to be printed.
    • print

      public void print(int i)
      Print an integer. The string used is the output of String.valueOf(int).
      Overrides:
      print in class PrintStream
      Parameters:
      i - The integer to be printed.
      See Also:
    • print

      public void print(long l)
      Print a long integer. The string used is the output of String.valueOf(long).
      Overrides:
      print in class PrintStream
      Parameters:
      l - The long to be printed.
      See Also:
    • print

      public void print(float f)
      Print a floating-point number. The string used is the output of String.valueOf(float).
      Overrides:
      print in class PrintStream
      Parameters:
      f - The float to be printed.
      See Also:
    • print

      public void print(double d)
      Print a double-precision floating-point number. The string used is the output of String.valueOf(double).
      Overrides:
      print in class PrintStream
      Parameters:
      d - The double to be printed.
      See Also:
    • print

      public void print(char[] charArray)
      Print an array of characters.
      Overrides:
      print in class PrintStream
      Parameters:
      charArray - The array of characters to be printed.
      Throws:
      NullPointerException - if the array is null.
    • print

      public void print(String string)
      Print a string. If the argument is null then the string "null" is printed.
      Overrides:
      print in class PrintStream
      Parameters:
      string - The string to be printed, or null if "null" should be printed.
    • print

      public void print(Object object)
      Print an object. The string used is the output of String.valueOf(Object).
      Overrides:
      print in class PrintStream
      Parameters:
      object - The object to be printed.
      See Also:
    • println

      public void println()
      Terminate the current line by writing the line separator string. The line separator string is defined by the system property "line.separator", and is not necessarily a single newline character ('\n'). This implementation delegates to newLine().
      Overrides:
      println in class PrintStream
    • println

      public void println(boolean b)
      Print a boolean and then terminates the line. This method behaves as though it invokes print(boolean) and then println().
      Overrides:
      println in class PrintStream
      Parameters:
      b - The boolean to be printed
    • println

      public void println(char c)
      Print a character and then terminates the line. This method behaves as though it invokes print(char) and then println().
      Overrides:
      println in class PrintStream
      Parameters:
      c - The character to be printed.
    • println

      public void println(int x)
      Print an integer and then terminates the line. This method behaves as though it invokes print(int) and then println().
      Overrides:
      println in class PrintStream
      Parameters:
      x - The integer to be printed.
    • println

      public void println(long l)
      Print a long and then terminates the line. This method behaves as though it invokes print(long) and then println().
      Overrides:
      println in class PrintStream
      Parameters:
      l - The long to be printed.
    • println

      public void println(float x)
      Print a float and then terminates the line. This method behaves as though it invokes print(float) and then println().
      Overrides:
      println in class PrintStream
      Parameters:
      x - The float to be printed.
    • println

      public void println(double d)
      Print a double and then terminates the line. This method behaves as though it invokes print(double) and then println().
      Overrides:
      println in class PrintStream
      Parameters:
      d - The double to be printed.
    • println

      public void println(char[] charArray)
      Print an array of characters and then terminates the line. This method behaves as though it invokes print(char[]) and then println().
      Overrides:
      println in class PrintStream
      Parameters:
      charArray - An array of characters to print.
    • println

      public void println(String string)
      Print a string and then terminates the line. This method behaves as though it invokes print(String) and then println().
      Overrides:
      println in class PrintStream
      Parameters:
      string - The string to be printed.
    • println

      public void println(Object object)
      Print an object and then terminates the line. This method behaves as though it invokes print(Object) and then println().
      Overrides:
      println in class PrintStream
      Parameters:
      object - The object to be printed.
    • format

      public PrintStream format(String format, Object... args)
      Writes a formatted string to this output stream using the specified format string and arguments.

      The locale always used is the one returned by Locale.getDefault(), regardless of any previous invocations of other formatting methods on this object.

      Overrides:
      format in class PrintStream
      Parameters:
      format - A format string as described in Format string syntax.
      args - Arguments referenced by the format specifiers in the format string.
      Returns:
      This output stream.
      Throws:
      IllegalFormatException - if a format string contains an illegal syntax, a format specifier that is incompatible with the given arguments, insufficient arguments given the format string, or other illegal conditions.
      NullPointerException - If the format is null.
    • format

      public WriterPrintStream format(Locale locale, String format, Object... args)
      Writes a formatted string to this output stream using the specified format string and arguments.
      Overrides:
      format in class PrintStream
      Parameters:
      locale - The locale to apply during formatting, or null if no localization should be applied.
      format - A format string as described in Format string syntax
      args - Arguments referenced by the format specifiers in the format string.
      Returns:
      This output stream.
      Throws:
      IllegalFormatException - if a format string contains an illegal syntax, a format specifier that is incompatible with the given arguments, insufficient arguments given the format string, or other illegal conditions.
      NullPointerException - If the format is null.