1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package Torello.Java;

import java.io.*;
import java.util.Locale;

/**
 * A class that is nearly identical to the <CODE>StringBuilder</CODE> and / or
 * <CODE>StringWriter</CODE> standard Java Classes.
 * 
 * <BR /><BR /><EMBED CLASS='external-html' DATA-FILE-ID=STWR>
 */
public class StorageWriter extends Writer
{
    /**
    * This value is true by default.  When this is {@code TRUE}, all output text will be sent to
    * {@code System.out}, in addition to the underlying {@code StringBuffer}.
    */
    public boolean sysOut = true;

    /**
     * All output text is sent to this {@code StringBuffer} for storage, it may cleared by calling
     * {@link #erase()}.  The underlying {@code String} obtained, at any point, by invoking
     * {@link #getString()}
     */
    protected StringBuffer sb = new StringBuffer();

    /** Creates a new StringBuffer, and saves the value of sysOut */
    public StorageWriter(boolean sendToSysOut)
    { this.sysOut = sendToSysOut; }

    /** Default constructor, sysOut = true; */
    public StorageWriter()
    { this.sysOut = true; }

    /** Does nothing, dummy method.  Is required by definition of java.io.Writer class */
    public void close() { }

    /** Does nothing, dummy method.  Is required by definition of java.io.Writer class */
    public void flush()	{ }

    /**
     * Simply calls toString() on the internally saved StringBuffer.
     *
     * <BR /><BR /><SPAN STYLE="color: blue; font-weight: bold">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 internal {@code java.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).</SPAN>
     *
     * <BR /><BR /><B>NOTE:</B> Java does have an entire package ({@code 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.
     *
     * @return This returns the entire contents of the "log" as a String.  This method is the
     * primary reason this class exists.
     */
    public String getString()
    { return sb.toString(); }

    /** Clears the "internal memory" in the {@code StringBuffer}. */
    public void erase() { sb.setLength(0); }

    public void	print(boolean b)    { if (sysOut) System.out.print(b); sb.append(b); }
    public void	print(char c)       { if (sysOut) System.out.print(c); sb.append(c); }
    public void	print(char[] s)     { if (sysOut) System.out.print(s); sb.append(s); }
    public void	print(double d)     { if (sysOut) System.out.print(d); sb.append(d); }
    public void	print(float f)      { if (sysOut) System.out.print(f); sb.append(f); }
    public void	print(int i)        { if (sysOut) System.out.print(i); sb.append(i); }
    public void	print(long l)       { if (sysOut) System.out.print(l); sb.append(l); }
    public void	print(Object o)     { if (sysOut) System.out.print(o); sb.append(o); }
    public void	print(String s)     { if (sysOut) System.out.print(s); sb.append(s); }

    public void	println()           { if (sysOut) System.out.println(); sb.append("\n"); }
    public void	println(boolean x)  { if (sysOut) System.out.println(x); sb.append(x + "\n"); }
    public void	println(char x)     { if (sysOut) System.out.println(x); sb.append(x + "\n"); }
    public void	println(char[] x)   { if (sysOut) System.out.println(x); sb.append(x + "\n"); }
    public void	println(double x)   { if (sysOut) System.out.println(x); sb.append(x + "\n"); }
    public void	println(float x)    { if (sysOut) System.out.println(x); sb.append(x + "\n"); }
    public void	println(int x)      { if (sysOut) System.out.println(x); sb.append(x + "\n"); }
    public void	println(long x)     { if (sysOut) System.out.println(x); sb.append(x + "\n"); }
    public void	println(Object x)   { if (sysOut) System.out.println(x); sb.append(x + "\n"); }
    public void	println(String x)   { if (sysOut) System.out.println(x); sb.append(x + "\n"); }

    public void	write(char[] buf)   { if (sysOut) System.out.print(buf); sb.append(buf); }
    public void	write(int c)        { if (sysOut) System.out.print((char) c); sb.append((char) c); }

    public Writer append(char c)
    {
        if (sysOut) System.out.print(c);
        sb.append(c);
        return this; 
    }

    public Writer append(CharSequence csq)
    {
        if (sysOut) System.out.print(csq);
        sb.append(csq);
        return this;
    }

    public Writer append(CharSequence csq, int start, int end)
    {
        String s = csq.subSequence(start, end).toString();
        if (sysOut) System.out.print(s);
        sb.append(s);
        return this;
    }

    public Writer format(Locale l, String format, Object... args)
    {
        String temp = String.format(l, format, args);
        if (sysOut) System.out.print(temp);
        sb.append(temp);
        return this;
    }

    public Writer format(String format, Object... args)
    {
        String temp = String.format(format, args);
        if (sysOut) System.out.print(temp);
        sb.append(temp);
        return this;
    }

    public Writer printf(Locale l, String format, Object... args)
    {
        String temp = String.format(l, format, args);
        if (sysOut) System.out.print(temp);
        sb.append(temp);
        return this;
    }

    public Writer printf(String format, Object... args)
    {
        String temp = String.format(format, args);
        if (sysOut) System.out.print(temp);
        sb.append(temp);
        return this;
    }

    public void write(String s, int off, int len)
    { 
        s = s.substring(off, off+len); 
        if (sysOut) System.out.print(s);
        sb.append(s);
    }

    public void	write(char[] buf, int off, int len)
    {
        String s = String.valueOf(buf, off, len);
        if (sysOut) System.out.print(s);
        sb.append(s);
    }
}