Package Torello.Java.Additional
Class AppendableTap
- java.lang.Object
-
- Torello.Java.Additional.AppendableTap
-
- All Implemented Interfaces:
java.lang.Appendable
public class AppendableTap extends java.lang.Object implements java.lang.Appendable
Appendable
Implementation:
This class implements and upgrades the Simple, Javajava.lang.Appendable
interface, providing additional features and improvements to it. TheAppendable
interace is a viable design-decsion possibility for outputing text-data to a log.
AnAppendable
is both blindingly easy, and highly customizable to use for third-party developers who may not be familiar with the internals of another developer's source-code.
This Class' Primary Addition toAppendable
:
This class allows a user to 'wrap' anotherAppendable
, and copy the output that it receives in it'sappend(...)
method invocations to an internal storage buffer. That internal storage may be retrieved whenever the need arises.An implementation of the standard Java'Appendable'
interface that keeps an internal log of all text-data that 'passes through' theAppendable
. This class can be used as a wrapper another (user-provided)Appendable
, so that this class may "listen" or "record" allappend(...)
messages which are being sent to the wrapped (the other, user-provided)Appendable
. The original instance ofAppendable
will still continue to receive all of its'append'
text & character data, as it normally would. However, this wrapper class will be recording the character-data in a buffer.
All messages sent to the wrappedAppendable
will, first, 'pass through'this
AppendableTap
instance. The character data received shall be saved inside an internal instance ofStringBuffer
, so that it may later be retrieved.
RECORDED MESSAGES:
The recorded-message(s) may be obtained, at any time, using the methodAppendableTap.getString()
.
SYNCHRONIZED METHODS:
This class' methods are synchronized. Text and messaging data sent in a multi-threaded environment / application is quite common. This means that the methods any messages which would be sent to this class (AppendableTap
) will force the JVM to lock the instance of thisObject
, and prevent other threads from accessing this class, until this class' operations have completed. (This is what the Java-Keyword'synchronized'
means).
This was an early decision decision made solely because of the original use for this class in the multi-threadedOSResponse
andOSCommands
classes. ClassAppendableTap
was eventually replaced by the classesTriAppendable
and is no longer regularly used in that set of tools.
Hi-Lited Source-Code:- View Here: Torello/Java/Additional/AppendableTap.java
- Open New Browser-Tab: Torello/Java/Additional/AppendableTap.java
File Size: 4,564 Bytes Line Count: 117 '\n' Characters Found
-
-
Constructor Summary
Constructors Constructor Description AppendableTap(Appendable appendable)
Builds an instance of this class.
-
Method Summary
Methods: interface java.lang.Appendable Modifier and Type Method Appendable
append(char c)
Appendable
append(CharSequence s)
Appendable
append(CharSequence s, int start, int end)
Retrieve Tap's Contents, as a java.lang.String Modifier and Type Method String
getString()
Retrieve Tap's Contents, and Reset / Empty the Tap Modifier and Type Method String
poll()
Reset / Empty the Tap Modifier and Type Method void
clear()
-
-
-
Constructor Detail
-
AppendableTap
public AppendableTap(java.lang.Appendable appendable)
Builds an instance of this class. The provided instance ofAppendable
will receive allappend()
invocations that are sent to'this'
instance ofAppendTap
. It is important to note that only theappend()
calls sent to this class first will be 'tapped' (logged and kept in an internalStringBuffer
instance). Any invocations that are made independently on the providedAppendable
will not be saved by this class.- Parameters:
appendable
- This may be any javaAppendable
. Any messages sent to this class will be sent to this inputAppendable
as well.
-
-
Method Detail
-
append
public java.lang.Appendable append(char c) throws java.io.IOException
Logs input-parameter character'c'
to an internalStringBuffer
, and then subsequently sends'c'
to the user-providedAppendable
.- Specified by:
append
in interfacejava.lang.Appendable
- Parameters:
c
- Any character data.- Returns:
'this'
instance ofAppendableTap
shall be returned, for convenience.- Throws:
java.io.IOException
- Code:
- Exact Method Body:
sb.append(c); return outputAppendable.append(c);
-
append
public java.lang.Appendable append(java.lang.CharSequence s) throws java.io.IOException
Logs input-parameterCharSequence 's'
to an internalStringBuffer
, and then subsequently sends's'
to the user-providedAppendable
.- Specified by:
append
in interfacejava.lang.Appendable
- Parameters:
s
- AnyString
data.- Returns:
'this'
instance ofAppendableTap
shall be returned, for convenience.- Throws:
java.io.IOException
- Code:
- Exact Method Body:
sb.append(s); return outputAppendable.append(s);
-
append
public java.lang.Appendable append(java.lang.CharSequence s, int start, int end) throws java.io.IOException
Logs a substring of input-parameterCharSequence 's'
defined by theint
parameters'start'
and'end'
. This operation would be equivalent to calling theString
methodString.substring(start, end)
on the inputCharSequence
prior to logging this character data, and sending it to the user-providedAppendable
.- Specified by:
append
in interfacejava.lang.Appendable
- Parameters:
s
- This may be any JavaCharSequence
start
- The first character (inclusive) in the sub-string / sub-sequence of the inputCharSequence
to log, and pass-on.end
- The last character (exclusive) of the input sub-string / sub-sequence of theCharSequence
to log, and pass-on.- Returns:
'this'
instance ofAppendableTap
shall be returned, for convenience.- Throws:
java.io.IOException
- Code:
- Exact Method Body:
sb.append(s, start, end); return outputAppendable.append(s, start, end);
-
getString
public java.lang.String getString()
Retrieves the text-data that has been sent to thisAppendableTap
NOTE: Invoking this method has no effect on the underlying output (user-provided - at construction time)Appendable
.- Returns:
- Any character or text data that has been received by
'this'
instance ofAppendableTap
through any of itsappend(...)
methods. - Code:
- Exact Method Body:
String s = sb.toString(); sb.setLength(0); sb.append(s); return s;
-
poll
public java.lang.String poll()
Resets or 'clears' the contents of the internalStringBuffer
'tap', and returns the contents that were saved.- Returns:
- Returns the contents of the internal storage, and clears the contents.
- Code:
- Exact Method Body:
String s = sb.toString(); sb.setLength(0); return s;
-
clear
public void clear()
Clears the 'tap' of all contents
-
-