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 theStringBuilderand / orStringWriterstandard 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 aStringfor 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 booleanfield calledsysOut, and when this value is TRUE, everything that is printed toclass StorageWriteris also sent toSystem.out- in addition to being saved to an internalStringBufferfield. When this inner field-value is false, data is only sent to theStringBuffer.
The text data stored in the internalStringBuffercan be obtained by callinggetString()
The text data in thisStringBuffercan 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 StorageWriter()StorageWriter(boolean sendToSysOut)
-
Method Summary
StorageWriter Methods Modifier and Type Method voiderase()StringgetString()Methods: interface java.lang.Appendable Modifier and Type Method Writerappend(char c)Writerappend(CharSequence csq)Writerappend(CharSequence csq, int start, int end)Methods: class java.io.PrintStream Modifier and Type Method Writerformat(String format, Object... args)Writerformat(Locale l, String format, Object... args)voidprint(boolean b)voidprint(char c)voidprint(char[] s)voidprint(double d)voidprint(float f)voidprint(int i)voidprint(long l)voidprint(Object o)voidprint(String s)Writerprintf(String format, Object... args)Writerprintf(Locale l, String format, Object... args)voidprintln()voidprintln(boolean x)voidprintln(char x)voidprintln(char[] x)voidprintln(double x)voidprintln(float x)voidprintln(int x)voidprintln(long x)voidprintln(Object x)voidprintln(String x)Methods: interface java.io.Flushable Modifier and Type Method voidflush()Methods: interface java.io.Closeable Modifier and Type Method voidclose()Methods: class java.io.Writer Modifier and Type Method voidwrite(char[] buf)voidwrite(char[] buf, int off, int len)voidwrite(int c)voidwrite(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.- Code:
- Exact Field Declaration Expression:
public boolean sysOut = true;
-
sb
protected java.lang.StringBuffer sb
All output text is sent to thisStringBufferfor storage, it may cleared by callingerase(). The underlyingStringobtained, at any point, by invokinggetString()- Code:
- Exact Field Declaration Expression:
protected StringBuffer sb = new StringBuffer();
-
-
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:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Specified by:
closein 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:
flushin interfacejava.io.Flushable- Specified by:
flushin 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.- Code:
- Exact Method Body:
sb.setLength(0);
-
print
public void print(boolean b)
- Code:
- Exact Method Body:
if (sysOut) System.out.print(b); sb.append(b);
-
print
public void print(char c)
- Code:
- Exact Method Body:
if (sysOut) System.out.print(c); sb.append(c);
-
print
public void print(char[] s)
- Code:
- Exact Method Body:
if (sysOut) System.out.print(s); sb.append(s);
-
print
public void print(double d)
- Code:
- Exact Method Body:
if (sysOut) System.out.print(d); sb.append(d);
-
print
public void print(float f)
- Code:
- Exact Method Body:
if (sysOut) System.out.print(f); sb.append(f);
-
print
public void print(int i)
- Code:
- Exact Method Body:
if (sysOut) System.out.print(i); sb.append(i);
-
print
public void print(long l)
- Code:
- Exact Method Body:
if (sysOut) System.out.print(l); sb.append(l);
-
print
public void print(java.lang.Object o)
- Code:
- Exact Method Body:
if (sysOut) System.out.print(o); sb.append(o);
-
print
public void print(java.lang.String s)
- Code:
- Exact Method Body:
if (sysOut) System.out.print(s); sb.append(s);
-
println
public void println()
- Code:
- Exact Method Body:
if (sysOut) System.out.println(); sb.append("\n");
-
println
public void println(boolean x)
- Code:
- Exact Method Body:
if (sysOut) System.out.println(x); sb.append(x + "\n");
-
println
public void println(char x)
- Code:
- Exact Method Body:
if (sysOut) System.out.println(x); sb.append(x + "\n");
-
println
public void println(char[] x)
- Code:
- Exact Method Body:
if (sysOut) System.out.println(x); sb.append(x + "\n");
-
println
public void println(double x)
- Code:
- Exact Method Body:
if (sysOut) System.out.println(x); sb.append(x + "\n");
-
println
public void println(float x)
- Code:
- Exact Method Body:
if (sysOut) System.out.println(x); sb.append(x + "\n");
-
println
public void println(int x)
- Code:
- Exact Method Body:
if (sysOut) System.out.println(x); sb.append(x + "\n");
-
println
public void println(long x)
- Code:
- Exact Method Body:
if (sysOut) System.out.println(x); sb.append(x + "\n");
-
println
public void println(java.lang.Object x)
- Code:
- Exact Method Body:
if (sysOut) System.out.println(x); sb.append(x + "\n");
-
println
public void println(java.lang.String x)
- Code:
- Exact Method Body:
if (sysOut) System.out.println(x); sb.append(x + "\n");
-
write
public void write(char[] buf)
- Overrides:
writein classjava.io.Writer- Code:
- Exact Method Body:
if (sysOut) System.out.print(buf); sb.append(buf);
-
write
public void write(int c)
- Overrides:
writein 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:
appendin interfacejava.lang.Appendable- Overrides:
appendin 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:
appendin interfacejava.lang.Appendable- Overrides:
appendin 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:
appendin interfacejava.lang.Appendable- Overrides:
appendin 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)
- Code:
- Exact Method Body:
String temp = String.format(l, format, args); if (sysOut) System.out.print(temp); sb.append(temp); return this;
-
format
public java.io.Writer format(java.lang.String format, java.lang.Object... args)
- Code:
- Exact Method Body:
String temp = String.format(format, args); if (sysOut) System.out.print(temp); sb.append(temp); return this;
-
printf
public java.io.Writer printf(java.util.Locale l, java.lang.String format, java.lang.Object... args)
- Code:
- Exact Method Body:
String temp = String.format(l, format, args); if (sysOut) System.out.print(temp); sb.append(temp); return this;
-
printf
public java.io.Writer printf(java.lang.String format, java.lang.Object... args)
- Code:
- Exact Method Body:
String temp = String.format(format, args); if (sysOut) System.out.print(temp); sb.append(temp); return this;
-
write
public void write(java.lang.String s, int off, int len)
- Overrides:
writein 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:
writein classjava.io.Writer- Code:
- Exact Method Body:
String s = String.valueOf(buf, off, len); if (sysOut) System.out.print(s); sb.append(s);
-
-