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
AppendableImplementation:
This class implements and upgrades the Simple, Javajava.lang.Appendableinterface, providing additional features and improvements to it. TheAppendableinterace is a viable design-decsion possibility for outputing text-data to a log.
AnAppendableis 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:
AnAppendableSafeinstance merely wraps an instance ofAppendableto catch and prevent the sometimes messy, checked-exceptionIOExceptionfrom 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 anIOExceptionanyway!
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 IOExceptionin 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 problematicIOExceptionthat 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.Appendableinstance, but worries that that interface's copious'throws IOException'will over-complicate their logging-code, then thisinterfaceis for you!.
The following five options are available for dealing with the dreaded CheckedIOExceptionthat Java may throw when writing to anAppendable.- Wrap the checked
IOExceptionin ajava.io.UncheckedIOException, and then pass it to the unchecked exception's constructor's'cause'parameter. After construction, throw the newly constructed, unchecked, exception.
- Wrap the checked
IOExceptionin anAppendableError, and then pass it to the error's constructor's'cause'parameter. After construction, throw the newly constructed, unchecked, error.
- Supress and Ignore the exception competely! After catching an
IOExceptionwhich has been accidentally generated by one of the underlyingAppendable's 'append(...)'methods, simply ignore it and let it go out of scope.
- Wrap the checked
IOExceptionin a User-ProvidedRuntimeException, and then pass it to the User-Provided exception's constructor's'cause'parameter. After that constructor has finished, throw the newly constructtedRuntimeException.
Important: The Class provided to parameterclassRTEXmust actually have a constructor which accepts a'cause'-Throwable. If the User-Provided, "alternate", RuntimeException cannot be constructed using a'cause'Throwable, then the attempt to build this'AppendableSafe'instance will fail.
RuntimeException Constructor Expectations: The constructor for this variant ofAppendableSafewill check for a constructor which accepts aThrowable 'cause'parameter, or aRuntimeExceptionconstructor which accepts aString 'message'andThrowable 'cause'.
- Provide a
Consumer<? extends IOException> 'handler'if-and-when the underlyingAppendable's append(...)methods happen to throw anIOException.
- 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: 22,976 Bytes Line Count: 497 '\n' Characters Found
-
-
Field Summary
Underlying / Wrapped Appendable Modifier and Type Field AppendableappendableIOException-Throw Replacement Decision ConstantsModifier and Type Field static byteSUPPRESS_AND_IGNOREstatic byteUSE_APPENDABLE_ERRORstatic byteUSE_UNCHECKED_IOEXCEPTIONReplacement-Exception Message Modifier and Type Field static StringexceptionMessageIOException-Throw Replacement DecisionsModifier and Type Field Class<? extends RuntimeException>classRTEXbytethrowDecision
-
Constructor Summary
Constructors Constructor AppendableSafe(Appendable appendable, byte throwDecision)AppendableSafe(Appendable appendable, Class<? extends RuntimeException> classRTEX)AppendableSafe(Appendable appendable, Consumer<? super IOException> ioeHandler)
-
Method Summary
Methods: interface java.lang.Appendable Modifier and Type Method AppendableSafeappend(char c)AppendableSafeappend(CharSequence csq)AppendableSafeappend(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 haveAppendableErrorthrown 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
ConfiguresAppendableSafeto catch anyIOException'sthat are thrown by theAppendable'smethodappend(...), 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. ClassAppendableSafeis nothing more than a wrapper class around ajava.lang.Appendableinstance. All thatAppendableSafedoes is to catch any potentialIOExceptioninstances 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.
Thispublicfield provides access to the wrapped / internalAppendable.- Code:
- Exact Field Declaration Expression:
public final Appendable appendable;
-
throwDecision
public final byte throwDecision
If one of the standard ways of avoidingIOException-throws by a JavaAppendablehas 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 aRuntimeExceptionclass to throw when anIOExceptionis caught, this constant-field will be assigned'0'by the constructor.- Code:
- Exact Field Declaration Expression:
public final byte throwDecision;
-
classRTEX
public final java.lang.Class<? extends java.lang.RuntimeException> classRTEX
This may be null, and if it is - the value stored inthrowDecisionor the value ofhandlerwill be used for deciding what to do when the internal / wrappedAppendablethrows anIOExceptionwhile appending character-data.
When this 'class' field-constant is non-null, theRuntimeExceptionrepresented by the class will be thrown whenever the internal / wrappedAppendablethrows a (Checked)IOExceptionwhile writing character-data to it.- Code:
- Exact Field Declaration Expression:
public final Class<? extends RuntimeException> classRTEX;
-
-
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'sthat are thrown by the input'appendable'-parameter'sappend(...)methods.
If invoking anappend(...)method does cause anIOExceptionto throw, then theAppendableSafeinstance that is built right here will be one that, in turn, throws theThrowableindicated 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'smethods 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'sthat are thrown when appending character data to thisAppendableare 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 ofRuntimeExceptionthat should be expected to throw when the underlyingAppendablethrows anIOExceptionduring one of itsappend(...)method invocations.
This parameter may not be null, because otherwise aNullPointerExceptionwill 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.
-
AppendableSafe
public AppendableSafe (java.lang.Appendable appendable, java.util.function.Consumer<? super java.io.IOException> ioeHandler)
Constructs an instance of this class, where any potentialIOException'sthat are thrown when appending character data to thisAppendableare first suppressed, and then passed as parameters to thehandlerparameter received by this constructor.- 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 and provided to thehandlerprovided this constructor.ioeHandler- A Java Consumer which accepts anIOExceptionparameter, and performs whatever User-Decided functions if-and-when anIOExceptionhappens to be thrown by the'append(...)'methods which wrap by the internal'Appendable'instance.
-
-
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:
appendin interfacejava.lang.Appendable- Parameters:
c- The character to append- Returns:
- A reference to this
Appendable. - Throws:
java.lang.RuntimeException- If the underlying / wrappedjava.lang.Appendablethrows anIOExceptionwhile attempting to write charcter data using one of it'sappend(...)methods, and if the user has provided aRuntimeExceptionto fieldclassRTEXthen an instance of that exception will be thrown after catching the wrappedAppendable's IOException.java.io.UncheckedIOException- If the underlying / wrappedjava.lang.Appendablethrows anIOExceptionwhile attempting to write charcter data using one of it'sappend(...)methods, and if the user has configured the fieldthrowDecisionusing the constantUSE_UNCHECKED_IOEXCEPTIONthen an instance ofjava.io.UncheckedIOExceptionwill be thrown after catching the wrappedAppendable's IOException.AppendableError- If the underlying / wrappedjava.lang.Appendablethrows anIOExceptionwhile attempting to write charcter data using one of it'sappend(...)methods, and if the user has configured the fieldthrowDecisionusing the constantUSE_APPENDABLE_ERRORthen an instance ofTorello.Java.Additional.AppendableErrorwill be thrown after catching the wrappedAppendable's IOException.- Code:
- Exact Method Body:
try { appendable.append(c); } catch (IOException ioe) { handler.accept(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:
appendin 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.Appendablethrows anIOExceptionwhile attempting to write charcter data using one of it'sappend(...)methods, and if the user has provided aRuntimeExceptionto fieldclassRTEXthen an instance of that exception will be thrown after catching the wrappedAppendable's IOException.java.io.UncheckedIOException- If the underlying / wrappedjava.lang.Appendablethrows anIOExceptionwhile attempting to write charcter data using one of it'sappend(...)methods, and if the user has configured the fieldthrowDecisionusing the constantUSE_UNCHECKED_IOEXCEPTIONthen an instance ofjava.io.UncheckedIOExceptionwill be thrown after catching the wrappedAppendable's IOException.AppendableError- If the underlying / wrappedjava.lang.Appendablethrows anIOExceptionwhile attempting to write charcter data using one of it'sappend(...)methods, and if the user has configured the fieldthrowDecisionusing the constantUSE_APPENDABLE_ERRORthen an instance ofTorello.Java.Additional.AppendableErrorwill be thrown after catching the wrappedAppendable's IOException.- Code:
- Exact Method Body:
try { appendable.append(csq); } catch (IOException ioe) { handler.accept(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:
appendin 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.Appendablethrows anIOExceptionwhile attempting to write charcter data using one of it'sappend(...)methods, and if the user has provided aRuntimeExceptionto fieldclassRTEXthen an instance of that exception will be thrown after catching the wrappedAppendable's IOException.java.io.UncheckedIOException- If the underlying / wrappedjava.lang.Appendablethrows anIOExceptionwhile attempting to write charcter data using one of it'sappend(...)methods, and if the user has configured the fieldthrowDecisionusing the constantUSE_UNCHECKED_IOEXCEPTIONthen an instance ofjava.io.UncheckedIOExceptionwill be thrown after catching the wrappedAppendable's IOException.AppendableError- If the underlying / wrappedjava.lang.Appendablethrows anIOExceptionwhile attempting to write charcter data using one of it'sappend(...)methods, and if the user has configured the fieldthrowDecisionusing the constantUSE_APPENDABLE_ERRORthen an instance ofTorello.Java.Additional.AppendableErrorwill be thrown after catching the wrappedAppendable's IOException.- Code:
- Exact Method Body:
try { appendable.append(csq); } catch (IOException ioe) { handler.accept(ioe); } return this;
-
-