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
    A class that is nearly identical to the StringBuilder and / or StringWriter 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 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 getString()

    The text data in this StringBuffer can be cleared/erased by calling erase()


    • Field Summary

       
      All text is saved to a java.util.StringBuffer
      Modifier and Type Field
      protected StringBuffer sb
       
      Boolean flag for automatically sending text to System.out
      Modifier and Type Field
      boolean sysOut
      • Fields inherited from class java.io.Writer

        lock
    • 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)
      • 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 StringBuffer.
      • sb

        🡅  🡇     🗕  🗗  🗖
        protected java.lang.StringBuffer sb
        All output text is sent to this StringBuffer for storage, it may cleared by calling erase(). The underlying String obtained, at any point, by invoking getString()
    • 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:
         
        
      • 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:
         
        
      • 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:
         return sb.toString();
        
      • write

        🡅  🡇     🗕  🗗  🗖
        public void write​(char[] buf)
        Overrides:
        write in class java.io.Writer
        Code:
        Exact Method Body:
         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:
         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:
         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:
         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:
         String s = csq.subSequence(start, end).toString();
         if (sysOut) System.out.print(s);
         sb.append(s);
         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:
         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:
         String s = String.valueOf(buf, off, len);
         if (sysOut) System.out.print(s);
         sb.append(s);