Package com.globalmentor.io
Class WriterPrintStream
java.lang.Object
java.io.OutputStream
java.io.FilterOutputStream
java.io.PrintStream
com.globalmentor.io.WriterPrintStream
- All Implemented Interfaces:
Closeable
,Flushable
,Appendable
,AutoCloseable
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
ConstructorsConstructorDescriptionWriterPrintStream
(Writer writer) Writer constructor.WriterPrintStream
(Writer writer, boolean autoFlush) Writer and autoflush constructor. -
Method Summary
Modifier and TypeMethodDescriptionboolean
Flushes the stream and check its error state.void
close()
Closes the stream.void
flush()
Flushes the stream.Writes a formatted string to this output stream using the specified format string and arguments.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 an object.void
Print a string.void
println()
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
Print an object and then terminates the line.void
Print a string and then terminates the line.protected void
setError()
Set the error state of the stream totrue
.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
Writes a string to the underlying writer.protected void
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
-
Constructor Details
-
WriterPrintStream
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 isnull
.
-
WriterPrintStream
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 theprintln
methods is invoked, or a newline character or byte ('\n'
) is written.- Throws:
NullPointerException
- if the given writer isnull
.
-
-
Method Details
-
flush
public void flush()Flushes the stream. This version delegates to the decorated writer.- Specified by:
flush
in interfaceFlushable
- Overrides:
flush
in classPrintStream
-
close
public void close()Closes the stream. This version delegates to the decorated writer.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classPrintStream
-
checkError
public boolean checkError()Flushes the stream and check its error state.- Overrides:
checkError
in classPrintStream
- Returns:
true
if and only if this stream has encountered anIOException
other thanInterruptedIOException
, or thesetError()
method has been invoked.
-
setError
protected void setError()Set the error state of the stream totrue
.- Overrides:
setError
in classPrintStream
-
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 theflush()
method will be invoked. This implementation assumes the given byte specifies a single character.- Overrides:
write
in classPrintStream
- 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 theflush()
method will be invoked. Because aPrintWriter
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 classPrintStream
- Parameters:
buffer
- A byte array.offset
- The offset from which to start taking bytes.length
- The number of bytes to write.
-
write
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
Writes a string to the underlying writer. If automatic flushing is enabled and the string contains the newline character '\\n' then theflush()
method will be invoked.- Parameters:
string
- The string to write.
-
print
public void print(boolean b) Print a boolean value.- Overrides:
print
in classPrintStream
- Parameters:
b
- The bolean value to be printed
-
print
public void print(char c) Print a character.- Overrides:
print
in classPrintStream
- Parameters:
c
- The character to be printed.
-
print
public void print(int i) Print an integer. The string used is the output ofString.valueOf(int)
.- Overrides:
print
in classPrintStream
- 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 ofString.valueOf(long)
.- Overrides:
print
in classPrintStream
- 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 ofString.valueOf(float)
.- Overrides:
print
in classPrintStream
- 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 ofString.valueOf(double)
.- Overrides:
print
in classPrintStream
- Parameters:
d
- The double to be printed.- See Also:
-
print
public void print(char[] charArray) Print an array of characters.- Overrides:
print
in classPrintStream
- Parameters:
charArray
- The array of characters to be printed.- Throws:
NullPointerException
- if the array isnull
.
-
print
Print a string. If the argument isnull
then the string "null" is printed.- Overrides:
print
in classPrintStream
- Parameters:
string
- The string to be printed, ornull
if "null" should be printed.
-
print
Print an object. The string used is the output ofString.valueOf(Object)
.- Overrides:
print
in classPrintStream
- 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 tonewLine()
.- Overrides:
println
in classPrintStream
-
println
public void println(boolean b) Print a boolean and then terminates the line. This method behaves as though it invokesprint(boolean)
and thenprintln()
.- Overrides:
println
in classPrintStream
- 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 invokesprint(char)
and thenprintln()
.- Overrides:
println
in classPrintStream
- 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 invokesprint(int)
and thenprintln()
.- Overrides:
println
in classPrintStream
- 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 invokesprint(long)
and thenprintln()
.- Overrides:
println
in classPrintStream
- 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 invokesprint(float)
and thenprintln()
.- Overrides:
println
in classPrintStream
- 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 invokesprint(double)
and thenprintln()
.- Overrides:
println
in classPrintStream
- 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 invokesprint(char[])
and then
.println()
- Overrides:
println
in classPrintStream
- Parameters:
charArray
- An array of characters to print.
-
println
Print a string and then terminates the line. This method behaves as though it invokesprint(String)
and thenprintln()
.- Overrides:
println
in classPrintStream
- Parameters:
string
- The string to be printed.
-
println
Print an object and then terminates the line. This method behaves as though it invokesprint(Object)
and thenprintln()
.- Overrides:
println
in classPrintStream
- Parameters:
object
- The object to be printed.
-
format
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 classPrintStream
- 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 isnull
.
-
format
Writes a formatted string to this output stream using the specified format string and arguments.- Overrides:
format
in classPrintStream
- Parameters:
locale
- The locale to apply during formatting, ornull
if no localization should be applied.format
- A format string as described in Format string syntaxargs
- 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 isnull
.
-