Package Torello.Java

Class StorageWriter

  • All Implemented Interfaces:
    java.io.Closeable, java.io.Flushable, java.lang.Appendable, java.lang.AutoCloseable

    public class StorageWriter
    extends java.io.Writer
    StorageWriter - Documentation.

    The purpose of this class is to provide a tool that allows the developer to output text BOTH to the terminal AND to a String 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 a public boolean field called sysOut, and when this value is TRUE, everything that is printed to class StorageWriter is also sent to System.out - in addition to being saved to an internal StringBuffer field. When this inner field-value is false, data is only sent to the StringBuffer.

    The text data stored in the internal StringBuffer can be obtained by calling public String getString()
    The text data in this StringBuffer can be cleared/erased by calling public void erase()



    • Field Summary

      Fields 
      Modifier and Type Field
      protected StringBuffer sb
      boolean sysOut
      • Fields inherited from class java.io.Writer

        lock
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method
      Writer append​(char c)
      Writer append​(CharSequence csq)
      Writer append​(CharSequence csq, int start, int end)
      void close()
      void erase()
      void flush()
      Writer format​(String format, Object... args)
      Writer format​(Locale l, String format, Object... args)
      String getString()
      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)
      void write​(char[] buf)
      void write​(char[] buf, int off, int len)
      void write​(int c)
      void write​(String s, int off, int len)
      • Methods inherited from class java.io.Writer

        nullWriter, write
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • sysOut

        public boolean sysOut
        This value is true by default. When this is true, all output text will be sent to System.out, in addition to the underlying StorageWriter
        Code:
        Exact Field Declaration Expression:
        1
        public boolean sysOut = true;
        
      • sb

        protected java.lang.StringBuffer sb
        All output text is sent to this StringBuffer for storage, it may cleared or the underlying String obtained at any point!
        Code:
        Exact Field Declaration Expression:
        1
        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:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Specified by:
        close in class java.io.Writer
        Code:
        Exact Method Body:
        1
         }
        
      • flush

        public void flush()
        Does nothing, dummy method. Is required by definition of java.io.Writer class
        Specified by:
        flush in interface java.io.Flushable
        Specified by:
        flush in class java.io.Writer
        Code:
        Exact Method Body:
        1
         }
        
      • 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 internal 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).

        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:
        1
         return sb.toString();
        
      • erase

        public void erase()
        Clears the "internal memory" in the StringBuffer. Specifically, a new StringBuffer is instantiated, and the old will be garbage collected.
        Code:
        Exact Method Body:
        1
         sb.setLength(0); sb=new StringBuffer();
        
      • print

        public void print​(boolean b)
        Code:
        Exact Method Body:
        1
         if (sysOut) System.out.print(b);      sb.append(b);
        
      • print

        public void print​(char c)
        Code:
        Exact Method Body:
        1
         if (sysOut) System.out.print(c);      sb.append(c);
        
      • print

        public void print​(char[] s)
        Code:
        Exact Method Body:
        1
         if (sysOut) System.out.print(s);      sb.append(s);
        
      • print

        public void print​(double d)
        Code:
        Exact Method Body:
        1
         if (sysOut) System.out.print(d);      sb.append(d);
        
      • print

        public void print​(float f)
        Code:
        Exact Method Body:
        1
         if (sysOut) System.out.print(f);      sb.append(f);
        
      • print

        public void print​(int i)
        Code:
        Exact Method Body:
        1
         if (sysOut) System.out.print(i);      sb.append(i);
        
      • print

        public void print​(long l)
        Code:
        Exact Method Body:
        1
         if (sysOut) System.out.print(l);      sb.append(l);
        
      • print

        public void print​(java.lang.Object o)
        Code:
        Exact Method Body:
        1
         if (sysOut) System.out.print(o);      sb.append(o);
        
      • print

        public void print​(java.lang.String s)
        Code:
        Exact Method Body:
        1
         if (sysOut) System.out.print(s);      sb.append(s);
        
      • println

        public void println()
        Code:
        Exact Method Body:
        1
         if (sysOut) System.out.println();     sb.append("\n");
        
      • println

        public void println​(boolean x)
        Code:
        Exact Method Body:
        1
         if (sysOut) System.out.println(x);    sb.append(x + "\n");
        
      • println

        public void println​(char x)
        Code:
        Exact Method Body:
        1
         if (sysOut) System.out.println(x);    sb.append(x + "\n");
        
      • println

        public void println​(char[] x)
        Code:
        Exact Method Body:
        1
         if (sysOut) System.out.println(x);    sb.append(x + "\n");
        
      • println

        public void println​(double x)
        Code:
        Exact Method Body:
        1
         if (sysOut) System.out.println(x);    sb.append(x + "\n");
        
      • println

        public void println​(float x)
        Code:
        Exact Method Body:
        1
         if (sysOut) System.out.println(x);    sb.append(x + "\n");
        
      • println

        public void println​(int x)
        Code:
        Exact Method Body:
        1
         if (sysOut) System.out.println(x);    sb.append(x + "\n");
        
      • println

        public void println​(long x)
        Code:
        Exact Method Body:
        1
         if (sysOut) System.out.println(x);    sb.append(x + "\n");
        
      • println

        public void println​(java.lang.Object x)
        Code:
        Exact Method Body:
        1
         if (sysOut) System.out.println(x);    sb.append(x + "\n");
        
      • println

        public void println​(java.lang.String x)
        Code:
        Exact Method Body:
        1
         if (sysOut) System.out.println(x);    sb.append(x + "\n");
        
      • write

        public void write​(char[] buf)
        Overrides:
        write in class java.io.Writer
        Code:
        Exact Method Body:
        1
         if (sysOut) System.out.print(buf);            sb.append(buf);
        
      • write

        public void write​(int c)
        Overrides:
        write in class java.io.Writer
        Code:
        Exact Method Body:
        1
         if (sysOut) System.out.print((char) c);	    sb.append((char) c);
        
      • append

        public java.io.Writer append​(char c)
        Specified by:
        append in interface java.lang.Appendable
        Overrides:
        append in class java.io.Writer
        Code:
        Exact Method Body:
        1
         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 interface java.lang.Appendable
        Overrides:
        append in class java.io.Writer
        Code:
        Exact Method Body:
        1
         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 interface java.lang.Appendable
        Overrides:
        append in class java.io.Writer
        Code:
        Exact Method Body:
        1
        2
        3
        4
         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:
        1
        2
        3
         if (sysOut) System.out.format(l, format, args);
             sb.append(String.format(l, format, args));
         return this;
        
      • format

        public java.io.Writer format​(java.lang.String format,
                                     java.lang.Object... args)
        Code:
        Exact Method Body:
        1
        2
        3
         if (sysOut) System.out.format(format, args);
             sb.append(String.format(format, args));
         return this;
        
      • printf

        public java.io.Writer printf​(java.util.Locale l,
                                     java.lang.String format,
                                     java.lang.Object... args)
        Code:
        Exact Method Body:
        1
        2
        3
         if (sysOut) System.out.printf(l, format, args);
             sb.append(String.format(l, format, args));
         return this;
        
      • printf

        public java.io.Writer printf​(java.lang.String format,
                                     java.lang.Object... args)
        Code:
        Exact Method Body:
        1
        2
        3
         if (sysOut) System.out.printf(format, args);
             sb.append(String.format(format, args));
         return this;
        
      • write

        public void write​(java.lang.String s,
                          int off,
                          int len)
        Overrides:
        write in class java.io.Writer
        Code:
        Exact Method Body:
        1
        2
        3
         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 class java.io.Writer
        Code:
        Exact Method Body:
        1
        2
        3
         String s = String.valueOf(buf, off, len);
         if (sysOut) System.out.print(s);
         sb.append(s);