Package Torello.Java
Class StorageWriter
- java.lang.Object
-
- java.io.Writer
-
- Torello.Java.StorageWriter
-
- All Implemented Interfaces:
java.io.Closeable
,java.io.Flushable
,java.lang.Appendable
,java.lang.AutoCloseable
public class StorageWriter extends java.io.Writer
A class that is nearly identical to theStringBuilder
and / orStringWriter
standard Java Classes.
The purpose of this class is to provide a tool that allows the developer to output text BOTH to the terminal AND to aString
for later saving. Logging is facilitated by this class. When code is run, if the developer wishes to view any log/terminal output while the code is running, and then use that output for saving to review it later too - then this class can help.
There is apublic boolean
field calledsysOut
, and when this value is TRUE, everything that is printed toclass StorageWriter
is also sent toSystem.out
- in addition to being saved to an internalStringBuffer
field. When this inner field-value is false, data is only sent to theStringBuffer
.
The text data stored in the internalStringBuffer
can be obtained by callinggetString()
The text data in thisStringBuffer
can be cleared/erased by callingerase()
Hi-Lited Source-Code:- View Here: Torello/Java/StorageWriter.java
- Open New Browser-Tab: Torello/Java/StorageWriter.java
File Size: 6,058 Bytes Line Count: 154 '\n' Characters Found
-
-
Constructor Summary
Constructors Constructor Description StorageWriter()
Default constructor, sysOut = true;StorageWriter(boolean sendToSysOut)
Creates a new StringBuffer, and saves the value of sysOut
-
Method Summary
StorageWriter Methods Modifier and Type Method void
erase()
String
getString()
Methods: interface java.lang.Appendable Modifier and Type Method Writer
append(char c)
Writer
append(CharSequence csq)
Writer
append(CharSequence csq, int start, int end)
Methods: class java.io.PrintStream Modifier and Type Method Writer
format(String format, Object... args)
Writer
format(Locale l, String format, Object... args)
void
print(boolean b)
void
print(char c)
void
print(char[] s)
void
print(double d)
void
print(float f)
void
print(int i)
void
print(long l)
void
print(Object o)
void
print(String s)
Writer
printf(String format, Object... args)
Writer
printf(Locale l, String format, Object... args)
void
println()
void
println(boolean x)
void
println(char x)
void
println(char[] x)
void
println(double x)
void
println(float x)
void
println(int x)
void
println(long x)
void
println(Object x)
void
println(String x)
Methods: interface java.io.Flushable Modifier and Type Method void
flush()
Methods: interface java.io.Closeable Modifier and Type Method void
close()
Methods: class java.io.Writer Modifier and Type Method void
write(char[] buf)
void
write(char[] buf, int off, int len)
void
write(int c)
void
write(String s, int off, int len)
-
-
-
Field Detail
-
sysOut
public boolean sysOut
This value is true by default. When this isTRUE
, all output text will be sent toSystem.out
, in addition to the underlyingStringBuffer
.
-
sb
protected java.lang.StringBuffer sb
All output text is sent to thisStringBuffer
for storage, it may cleared by callingerase()
. The underlyingString
obtained, at any point, by invokinggetString()
-
-
Constructor Detail
-
StorageWriter
public StorageWriter(boolean sendToSysOut)
Creates a new StringBuffer, and saves the value of sysOut
-
StorageWriter
public StorageWriter()
Default constructor, sysOut = true;
-
-
Method Detail
-
close
public void close()
Does nothing, dummy method. Is required by definition of java.io.Writer class- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Specified by:
close
in classjava.io.Writer
- Code:
- Exact Method Body:
-
flush
public void flush()
Does nothing, dummy method. Is required by definition of java.io.Writer class- Specified by:
flush
in interfacejava.io.Flushable
- Specified by:
flush
in classjava.io.Writer
- Code:
- Exact Method Body:
-
getString
public java.lang.String getString()
Simply calls toString() on the internally saved StringBuffer.
This method is really the center of this class, it allows a programmer to write log-data or log-information to both the terminal, and to an internaljava.lang.StringBuffer
. This internally saved data can be retrieved later and written to a data-file (in addition to the data which was written to the screen).
NOTE: Java does have an entire package (java.util.logging
) - unfortunately, it is not one of the Java 8 Packages I have "gone through" completely, so I used this simple, less-featured, version of a logging class.- Returns:
- This returns the entire contents of the "log" as a String. This method is the primary reason this class exists.
- Code:
- Exact Method Body:
return sb.toString();
-
erase
public void erase()
Clears the "internal memory" in theStringBuffer
.
-
print
public void print(boolean b)
-
print
public void print(char c)
-
print
public void print(char[] s)
-
print
public void print(double d)
-
print
public void print(float f)
-
print
public void print(int i)
-
print
public void print(long l)
-
print
public void print(java.lang.Object o)
-
print
public void print(java.lang.String s)
-
println
public void println()
-
println
public void println(boolean x)
-
println
public void println(char x)
-
println
public void println(char[] x)
-
println
public void println(double x)
-
println
public void println(float x)
-
println
public void println(int x)
-
println
public void println(long x)
-
println
public void println(java.lang.Object x)
-
println
public void println(java.lang.String x)
-
write
public void write(char[] buf)
- Overrides:
write
in classjava.io.Writer
- Code:
- Exact Method Body:
if (sysOut) System.out.print(buf); sb.append(buf);
-
write
public void write(int c)
- Overrides:
write
in classjava.io.Writer
- Code:
- Exact Method Body:
if (sysOut) System.out.print((char) c); sb.append((char) c);
-
append
public java.io.Writer append(char c)
- Specified by:
append
in interfacejava.lang.Appendable
- Overrides:
append
in classjava.io.Writer
- Code:
- Exact Method Body:
if (sysOut) System.out.print(c); sb.append(c); return this;
-
append
public java.io.Writer append(java.lang.CharSequence csq)
- Specified by:
append
in interfacejava.lang.Appendable
- Overrides:
append
in classjava.io.Writer
- Code:
- Exact Method Body:
if (sysOut) System.out.print(csq); sb.append(csq); return this;
-
append
public java.io.Writer append(java.lang.CharSequence csq, int start, int end)
- Specified by:
append
in interfacejava.lang.Appendable
- Overrides:
append
in classjava.io.Writer
- Code:
- Exact Method Body:
String s = csq.subSequence(start, end).toString(); if (sysOut) System.out.print(s); sb.append(s); return this;
-
format
public java.io.Writer format(java.util.Locale l, java.lang.String format, java.lang.Object... args)
-
format
public java.io.Writer format(java.lang.String format, java.lang.Object... args)
-
printf
public java.io.Writer printf(java.util.Locale l, java.lang.String format, java.lang.Object... args)
-
printf
public java.io.Writer printf(java.lang.String format, java.lang.Object... args)
-
write
public void write(java.lang.String s, int off, int len)
- Overrides:
write
in classjava.io.Writer
- Code:
- Exact Method Body:
s = s.substring(off, off+len); if (sysOut) System.out.print(s); sb.append(s);
-
write
public void write(char[] buf, int off, int len)
- Specified by:
write
in classjava.io.Writer
- Code:
- Exact Method Body:
String s = String.valueOf(buf, off, len); if (sysOut) System.out.print(s); sb.append(s);
-
-