Package Torello.Java.Additional
Class AppendableSafe
- java.lang.Object
-
- Torello.Java.Additional.AppendableSafe
-
- All Implemented Interfaces:
java.lang.Appendable
public class AppendableSafe 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
:
AnAppendableSafe
instance merely wraps an instance ofAppendable
to catch and prevent the sometimes messy, checked-exceptionIOException
from cluttering what is often very simple log-code. This class allows a user to make it easy to choose what to do if a user-providedAppendable
-instance does wind up throwing anIOException
anyway!
When writing code that uses an instance ofAppendableSafe
, there is no need to catch any checked-exceptions thrown by theAppendable's 'append(...)'
methods, nor to include athrows IOException
in the signature/header of the methods that employAppendableSafe
-instances.
Used in Java-HTML JAR Library:
This class was originally developed for use with the toolImageScraper
, and it's use can be seen by clicking through the source-code for that tool and its helper classes.Identical to classjava.lang.Appendable
, but shunts the often problematicIOException
that may be thrown by itsappend(...)
-methods, and replaces it with either a user-customizableThrowable
-class, or with (the default)java.io.UncheckedIOException
.
If a tool or application would like to simplify output logging by allows a user to simply pass a reference to ajava.lang.Appendable
instance, but worries that that interface's copious'throws IOException'
will over-complicate their logging-code, then thisinterface
is for you!.- See Also:
ImageScraper
,AppendableLog
Hi-Lited Source-Code:- View Here: Torello/Java/Additional/AppendableSafe.java
- Open New Browser-Tab: Torello/Java/Additional/AppendableSafe.java
File Size: 16,135 Bytes Line Count: 350 '\n' Characters Found
-
-
Field Summary
Underlying / Wrapped Appendable Modifier and Type Field Appendable
appendable
IOException
-Throw Replacement Decision ConstantsModifier and Type Field static byte
SUPPRESS_AND_IGNORE
static byte
USE_APPENDABLE_ERROR
static byte
USE_UNCHECKED_IOEXCEPTION
Replacement-Exception Message Modifier and Type Field static String
exceptionMessage
IOException
-Throw Replacement DecisionsModifier and Type Field Class<? extends RuntimeException>
classRTEX
byte
throwDecision
-
Constructor Summary
Constructors Constructor Description AppendableSafe(Appendable appendable, byte throwDecision)
Constructs an instance of this class that wraps the providedAppendable
.AppendableSafe(Appendable appendable, Class<? extends RuntimeException> classRTEX)
Constructs an instance of this class, where any potentialIOException's
that are thrown when appending character data to thisAppendable
are wrapped into the specified-Throwable
.
-
Method Summary
Methods: interface java.lang.Appendable Modifier and Type Method AppendableSafe
append(char c)
AppendableSafe
append(CharSequence csq)
AppendableSafe
append(CharSequence csq, int start, int end)
-
-
-
Field Detail
-
USE_UNCHECKED_IOEXCEPTION
public static final byte USE_UNCHECKED_IOEXCEPTION
Indicates that the user would like to have'UncheckedIOException'
thrown in place of the standard (checked) exception'IOException'
.- See Also:
- Constant Field Values
- Code:
- Exact Field Declaration Expression:
public static final byte USE_UNCHECKED_IOEXCEPTION = 1;
-
USE_APPENDABLE_ERROR
public static final byte USE_APPENDABLE_ERROR
Indicates that the user would like to haveAppendableError
thrown in place of the standard (checked) exception'IOException'
.- See Also:
- Constant Field Values
- Code:
- Exact Field Declaration Expression:
public static final byte USE_APPENDABLE_ERROR = 2;
-
SUPPRESS_AND_IGNORE
public static final byte SUPPRESS_AND_IGNORE
ConfiguresAppendableSafe
to catch anyIOException's
that are thrown by theAppendable's
methodappend(...)
, and suppress / ignore them.- See Also:
- Constant Field Values
- Code:
- Exact Field Declaration Expression:
public static final byte SUPPRESS_AND_IGNORE = 3;
-
exceptionMessage
public static final java.lang.String exceptionMessage
The message used by the 'wrapper'Throwable
- See Also:
- Constant Field Values
- Code:
- Exact Field Declaration Expression:
public static final String exceptionMessage = "The underlying Appendable instance has thrown an IOException upon invocation of one of " + "its append(...) methods. Please see this Throwable's getCause() to review the " + "specifics of the cause IOException.";
-
appendable
public final java.lang.Appendable appendable
This is the internally usedjava.lang.Appendable
. ClassAppendableSafe
is nothing more than a wrapper class around ajava.lang.Appendable
instance. All thatAppendableSafe
does is to catch any potentialIOException
instances that may or may not be thrown by theAppendable's append(...)
methods, and either suppress them, or re-wrap them in an "Un-Checked" type ofThrowable
.
Thispublic
field provides access to the wrapped / internalAppendable
.
-
throwDecision
public final byte throwDecision
If one of the standard ways of avoidingIOException
-throws by a JavaAppendable
has been chosen, this byte-constant will contain the value of one of the threestatic
-constants defined at the top of this class.
If the user has opted to provide aRuntimeException
class to throw when anIOException
is caught, this constant-field will be assigned'0'
by the constructor.
-
classRTEX
public final java.lang.Class<? extends java.lang.RuntimeException> classRTEX
This may be null, and if it is - the value storedthrowDecision
is used for deciding what to do when the internal / wrappedAppendable
throws anIOException
while appending character-data.
When this 'class' field-constant is non-null, theRuntimeException
represented by the class will be thrown whenever the internal / wrappedAppendable
throws a (Checked)IOException
while writing character-data to it.
-
-
Constructor Detail
-
AppendableSafe
public AppendableSafe(java.lang.Appendable appendable, byte throwDecision)
Constructs an instance of this class that wraps the providedjava.lang.Appendable
. The instance that is created will catch any and allIOException's
that are thrown by the input'appendable'
-parameter'sappend(...)
methods.
If invoking anappend(...)
method does cause anIOException
to throw, then theAppendableSafe
instance that is built right here will be one that, in turn, throws theThrowable
indicated by the'throwDecision'
parameter.
If'throwDecision'
is passed thebyte
-value forSUPPRESS_AND_IGNORE
, then when / if anappend(...)
throwsIOException
, that exception, instead, will simply be caught and ignored.- Parameters:
appendable
- Any instance ofjava.lang.Appendable
.throwDecision
- Allows a user to decide what happens when / if the providedAppendable's
methods for accepting character-data throw anIOException
.
The allowed values are allbyte
-constants, and they are listed at the top of this class:- Throws:
java.lang.NullPointerException
- If null is passed to the'apendable'
parameter.java.lang.IllegalArgumentException
- If parameter'throwDecision'
is not passed one of the threebyte
-value constants provided at the top of this class.
-
AppendableSafe
public AppendableSafe (java.lang.Appendable appendable, java.lang.Class<? extends java.lang.RuntimeException> classRTEX) throws java.lang.NoSuchMethodException
Constructs an instance of this class, where any potentialIOException's
that are thrown when appending character data to thisAppendable
are wrapped into the specified-Throwable
.- Parameters:
appendable
- This may be any implementation ofjava.lang.Appendable
. This class will supercede calls to thisAppendable
, and prevent or catch any exceptions which may or may not throw.
If this instance / this parameter'appendable'
does happen to throw an exception when utilizing one it'sappend(...)
methods, then that exception will be caught, wrapped, and re-thrown as an instance of the specifiedThrowable
-=class.classRTEX
- This is the class ofRuntimeException
that should be expected to throw when the underlyingAppendable
throws anIOException
during one of itsappend(...)
method invocations.
This parameter may not be null, because otherwise aNullPointerException
will ensue. The class that is passed here must be an instance or decendant ofjava.lang.RuntimeException
, and one having a constructor which accepts aString
('message'
) parameter, and aThrowable
('cause'
) parameter.- Throws:
java.lang.NoSuchMethodException
- - This exception will throw if the parameter'classRTEX'
is a class which does not have a two-argument constructor - one of which is aString
, and the other of which is aThrowable
.java.lang.SecurityException
- - If a security manager,'s'
, is present and the caller's class loader is not the same as or an ancestor of the class loader for the current class and invocation ofs.checkPackageAccess()
denies access to the package of this class.java.lang.NullPointerException
- if either parameter'appendable'
or parameter'classRTEX'
are null.
-
-
Method Detail
-
append
public AppendableSafe append(char c)
Appends the specified character to thisAppendable
.
Description copied from class:java.lang.Appendable
, JDK 1.8- Specified by:
append
in interfacejava.lang.Appendable
- Parameters:
c
- The character to append- Returns:
- A reference to this
Appendable
. - Throws:
java.lang.RuntimeException
- If the underlying / wrappedjava.lang.Appendable
throws anIOException
while attempting to write charcter data using one of it'sappend(...)
methods, and if the user has provided aRuntimeException
to fieldclassRTEX
then an instance of that exception will be thrown after catching the wrappedAppendable's IOException
.java.io.UncheckedIOException
- If the underlying / wrappedjava.lang.Appendable
throws anIOException
while attempting to write charcter data using one of it'sappend(...)
methods, and if the user has configured the fieldthrowDecision
using the constantUSE_UNCHECKED_IOEXCEPTION
then an instance ofjava.io.UncheckedIOException
will be thrown after catching the wrappedAppendable's IOException
.AppendableError
- If the underlying / wrappedjava.lang.Appendable
throws anIOException
while attempting to write charcter data using one of it'sappend(...)
methods, and if the user has configured the fieldthrowDecision
using the constantUSE_APPENDABLE_ERROR
then an instance ofTorello.Java.Additional.AppendableError
will be thrown after catching the wrappedAppendable's IOException
.- Code:
- Exact Method Body:
try { appendable.append(c); } catch (IOException ioe) { handleIOE(ioe); } return this;
-
append
public AppendableSafe append(java.lang.CharSequence csq)
Appends the specified character sequence to thisAppendable
.
Depending on which class implements the character sequence'csq'
, the entire sequence may not be appended. For instance, if'csq'
is a'CharBuffer'
the subsequence to append is defined by the buffer's position and limit.
Description copied from class:java.lang.Appendable
, JDK 1.8- Specified by:
append
in interfacejava.lang.Appendable
- Parameters:
csq
- The character sequence to append. If csq is null, then the four characters "null" are appended to thisAppendable
.- Returns:
- A reference to this
Appendable
. - Throws:
java.lang.RuntimeException
- If the underlying / wrappedjava.lang.Appendable
throws anIOException
while attempting to write charcter data using one of it'sappend(...)
methods, and if the user has provided aRuntimeException
to fieldclassRTEX
then an instance of that exception will be thrown after catching the wrappedAppendable's IOException
.java.io.UncheckedIOException
- If the underlying / wrappedjava.lang.Appendable
throws anIOException
while attempting to write charcter data using one of it'sappend(...)
methods, and if the user has configured the fieldthrowDecision
using the constantUSE_UNCHECKED_IOEXCEPTION
then an instance ofjava.io.UncheckedIOException
will be thrown after catching the wrappedAppendable's IOException
.AppendableError
- If the underlying / wrappedjava.lang.Appendable
throws anIOException
while attempting to write charcter data using one of it'sappend(...)
methods, and if the user has configured the fieldthrowDecision
using the constantUSE_APPENDABLE_ERROR
then an instance ofTorello.Java.Additional.AppendableError
will be thrown after catching the wrappedAppendable's IOException
.- Code:
- Exact Method Body:
try { appendable.append(csq); } catch (IOException ioe) { handleIOE(ioe); } return this;
-
append
public AppendableSafe append(java.lang.CharSequence csq, int start, int end)
Appends a subsequence of the specified character sequence to thisAppendable
.
An invocation of this method of the formout.append(csq, start, end)
when'csq'
is not null, behaves in exactly the same way as the invocation:
Java Line of Code:
out.append(csq.subSequence(start, end))
Description copied from class:java.lang.Appendable
, JDK 1.8- Specified by:
append
in interfacejava.lang.Appendable
- Parameters:
csq
- The character sequence from which a subsequence will be appended. If csq is null, then the four characters "null" are appended to thisAppendable
.start
- The index of the first character in the subsequenceend
- The index of the character following the last character in the subsequence- Returns:
- A reference to this
Appendable
. - Throws:
java.lang.RuntimeException
- If the underlying / wrappedjava.lang.Appendable
throws anIOException
while attempting to write charcter data using one of it'sappend(...)
methods, and if the user has provided aRuntimeException
to fieldclassRTEX
then an instance of that exception will be thrown after catching the wrappedAppendable's IOException
.java.io.UncheckedIOException
- If the underlying / wrappedjava.lang.Appendable
throws anIOException
while attempting to write charcter data using one of it'sappend(...)
methods, and if the user has configured the fieldthrowDecision
using the constantUSE_UNCHECKED_IOEXCEPTION
then an instance ofjava.io.UncheckedIOException
will be thrown after catching the wrappedAppendable's IOException
.AppendableError
- If the underlying / wrappedjava.lang.Appendable
throws anIOException
while attempting to write charcter data using one of it'sappend(...)
methods, and if the user has configured the fieldthrowDecision
using the constantUSE_APPENDABLE_ERROR
then an instance ofTorello.Java.Additional.AppendableError
will be thrown after catching the wrappedAppendable's IOException
.- Code:
- Exact Method Body:
try { appendable.append(csq); } catch (IOException ioe) { handleIOE(ioe); } return this;
-
-