Package student.testingsupport
Class PrintStreamWithHistory
java.lang.Object
java.io.OutputStream
java.io.FilterOutputStream
java.io.PrintStream
student.testingsupport.PrintStreamWithHistory
- All Implemented Interfaces:
Closeable
,Flushable
,Appendable
,AutoCloseable
An enhanced version of
PrintStream
that provides for a history
recall function and some other features making I/O testing a bit
easier to perform. See the documentation for PrintStream
for
more thorough details on what methods are provided.-
Field Summary
Fields inherited from class java.io.FilterOutputStream
out
-
Constructor Summary
ConstructorsConstructorDescriptionPrintStreamWithHistory
(File file) Creates a new print stream, without automatic line flushing, with the specified file.PrintStreamWithHistory
(File file, String csn) Creates a new print stream, without automatic line flushing, with the specified file and charset.Create a new print stream.PrintStreamWithHistory
(OutputStream out, boolean autoFlush) Create a new print stream.PrintStreamWithHistory
(OutputStream out, boolean autoFlush, String encoding) Create a new print stream.PrintStreamWithHistory
(String fileName) Creates a new print stream, without automatic line flushing, with the specified file name.PrintStreamWithHistory
(String fileName, String csn) Creates a new print stream, without automatic line flushing, with the specified file name and charset. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Reset this object's history to be empty, just as when the object was first created.Retrieve the text history of what has been sent to this PrintStream.boolean
Returns whether this object's history has normalized unix-style line endings, or the raw line endings generated by printing.void
setMaxCapacity
(long bytes, boolean failOnOverflow) Set the maximum number of bytes that will be stored as history.void
setNormalizeLineEndings
(boolean value) Set whether this object's history will have normalized unix-style line endings, or the raw line endings generated by printing.void
write
(byte[] buf, int off, int len) Writelen
bytes from the specified byte array starting at offsetoff
to this stream.void
write
(int b) Write the specified byte to this stream.Methods inherited from class java.io.PrintStream
append, append, append, checkError, clearError, close, flush, format, format, print, print, print, print, print, print, print, print, print, printf, printf, println, println, println, println, println, println, println, println, println, println, setError, write, writeBytes
Methods inherited from class java.io.OutputStream
nullOutputStream
-
Constructor Details
-
PrintStreamWithHistory
Create a new print stream. This stream will not flush automatically.- Parameters:
out
- The output stream to which values and objects will be printed- See Also:
-
PrintStreamWithHistory
Create a new print stream.- Parameters:
out
- The output stream to which values and objects will be printedautoFlush
- A boolean; if true, the output buffer will be flushed whenever a byte array is written, one of theprintln
methods is invoked, or a newline character or byte ('\n'
) is written- See Also:
-
PrintStreamWithHistory
public PrintStreamWithHistory(OutputStream out, boolean autoFlush, String encoding) throws UnsupportedEncodingException Create a new print stream.- Parameters:
out
- The output stream to which values and objects will be printedautoFlush
- A boolean; if true, the output buffer will be flushed whenever a byte array is written, one of theprintln
methods is invoked, or a newline character or byte ('\n'
) is writtenencoding
- The name of a supported character encoding- Throws:
UnsupportedEncodingException
- If the named encoding is not supported
-
PrintStreamWithHistory
Creates a new print stream, without automatic line flushing, with the specified file name. This convenience constructor creates the necessary intermediateOutputStreamWriter
, which will encode characters using the default charset for this instance of the Java virtual machine.- Parameters:
fileName
- The name of the file to use as the destination of this print stream. If the file exists, then it will be truncated to zero size; otherwise, a new file will be created. The output will be written to the file and is buffered.- Throws:
FileNotFoundException
- If the given file object does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the fileSecurityException
- If a security manager is present andcheckWrite(fileName)
denies write access to the file- Since:
- 1.5
-
PrintStreamWithHistory
public PrintStreamWithHistory(String fileName, String csn) throws FileNotFoundException, UnsupportedEncodingException Creates a new print stream, without automatic line flushing, with the specified file name and charset. This convenience constructor creates the necessary intermediateOutputStreamWriter
, which will encode characters using the provided charset.- Parameters:
fileName
- The name of the file to use as the destination of this print stream. If the file exists, then it will be truncated to zero size; otherwise, a new file will be created. The output will be written to the file and is buffered.csn
- The name of a supported charset- Throws:
FileNotFoundException
- If the given file object does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the fileSecurityException
- If a security manager is present andcheckWrite(fileName)
denies write access to the fileUnsupportedEncodingException
- If the named charset is not supported- Since:
- 1.5
-
PrintStreamWithHistory
Creates a new print stream, without automatic line flushing, with the specified file. This convenience constructor creates the necessary intermediateOutputStreamWriter
, which will encode characters using the default charset for this instance of the Java virtual machine.- Parameters:
file
- The file to use as the destination of this print stream. If the file exists, then it will be truncated to zero size; otherwise, a new file will be created. The output will be written to the file and is buffered.- Throws:
FileNotFoundException
- If the given file object does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the fileSecurityException
- If a security manager is present andcheckWrite(file.getPath())
denies write access to the file- Since:
- 1.5
-
PrintStreamWithHistory
public PrintStreamWithHistory(File file, String csn) throws FileNotFoundException, UnsupportedEncodingException Creates a new print stream, without automatic line flushing, with the specified file and charset. This convenience constructor creates the necessary intermediateOutputStreamWriter
, which will encode characters using the provided charset.- Parameters:
file
- The file to use as the destination of this print stream. If the file exists, then it will be truncated to zero size; otherwise, a new file will be created. The output will be written to the file and is buffered.csn
- The name of a supported charset- Throws:
FileNotFoundException
- If the given file object does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the fileSecurityException
- If a security manager is presentandcheckWrite(file.getPath())
denies write access to the fileUnsupportedEncodingException
- If the named charset is not supported- Since:
- 1.5
-
-
Method Details
-
getHistory
Retrieve the text history of what has been sent to this PrintStream. This will include all text printed through this object. TheclearHistory()
method resets the history to be empty, just as when the object was first created. Note that newline characters in the history are always represented by '\n', regardless of what value the systemline.separator
property has.- Returns:
- all the text sent to this PrintStream
-
clearHistory
public void clearHistory()Reset this object's history to be empty, just as when the object was first created. You can access the history usinggetHistory()
. -
setNormalizeLineEndings
public void setNormalizeLineEndings(boolean value) Set whether this object's history will have normalized unix-style line endings, or the raw line endings generated by printing. The default for this is true, so history values will automatically be normalized unless this method is used to change that behavior.- Parameters:
value
- If true, the retrieved history will have all line endings automatically standardized to unix-style ("\n") line endings; if fase, the retrieved history will be presented without any modifications, using whatever (platform-specific) line endings were printed to this object.
-
setMaxCapacity
public void setMaxCapacity(long bytes, boolean failOnOverflow) Set the maximum number of bytes that will be stored as history.- Parameters:
bytes
- provides the maximum capacity of the history measured in bytes.failOnOverflow
- If true, this instance will throw an AssertionError when the maximum capacity is exceeded.
-
getNormalizeLineEndings
public boolean getNormalizeLineEndings()Returns whether this object's history has normalized unix-style line endings, or the raw line endings generated by printing. The default behavior is to normalize line endings.- Returns:
- True if line endings will be normalized in the history.
-
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.Note that the byte is written as given; to write a character that will be translated according to the platform's default character encoding, use the
print(char)
orprintln(char)
methods.- Overrides:
write
in classPrintStream
- Parameters:
b
- The byte to be written- See Also:
-
write
public void write(byte[] buf, int off, int len) Writelen
bytes from the specified byte array starting at offsetoff
to this stream. If automatic flushing is enabled then theflush
method will be invoked.Note that the bytes will be written as given; to write characters that will be translated according to the platform's default character encoding, use the
print(char)
orprintln(char)
methods.- Overrides:
write
in classPrintStream
- Parameters:
buf
- A byte arrayoff
- Offset from which to start taking byteslen
- Number of bytes to write
-