Package de.aristaflow.adept2.util.io
Class AttributedInputStream
- java.lang.Object
-
- java.io.InputStream
-
- java.io.FilterInputStream
-
- de.aristaflow.adept2.util.io.AttributedInputStream
-
- All Implemented Interfaces:
Attributable,Closeable,AutoCloseable
- Direct Known Subclasses:
RereadableContent.RrcInputStream
public class AttributedInputStream extends FilterInputStream implements Attributable
AnAttributedInputStreamwraps an input stream and allows for attributing it. Optionally, it supports a custom post-mortem action also used for closing.
-
-
Field Summary
Fields Modifier and Type Field Description protected Cleanup<IOException>cleanupThe clean-up for closing the wrapped input stream as post-mortem action of thisAttributedInputStreamornullto onlyInputStream.close()the wrapped input stream.-
Fields inherited from class java.io.FilterInputStream
in
-
-
Constructor Summary
Constructors Constructor Description AttributedInputStream(InputStream in)Creates a new input stream wrapping the designated input stream and having arbitrary attributes.AttributedInputStream(InputStream in, CleanupTask<IOException> cleanup)Creates a new input stream wrapping the designated input stream and having arbitrary attributes and the designated clean-up task.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()ObjectcomputeIfAbsent(String name, Function<? super String,Object> compFunc)Computes the value of the attribute with the designated name if the attribute is not present yet and returns the value (or the current value if already present and not computed).booleancontains(String name)Gets whether thisAttributablecontains an attribute value for the designated attribute name.booleancontainsType(String name, Class<?> valueType)Gets whether thisAttributablecontains an attribute value for the designated attribute name of the designated type.Objectget(String name)Gets the value of the attribute with the designated name ornullif the attribute is not present or has anullvalue.ObjectputIfAbsent(String name, Object value)Puts an attribute with the designated name and value if the attribute is not present yet and returnsnullor the existing current value.Objectremove(String name)Removes the attribute with the designated name and returns its current (previous) value.Objectreplace(String name, Object newValue)Replaces the value of the attribute with the designated name if it has a value and returns its previous value.booleanreplace(String name, Object oldValue, Object newValue)Replaces the value of the attribute with the designated name if it has the designated value.protected InputStreamunwrap()Gets the wrapped input stream.-
Methods inherited from class java.io.FilterInputStream
available, mark, markSupported, read, read, read, reset, skip
-
Methods inherited from class java.io.InputStream
nullInputStream, readAllBytes, readNBytes, readNBytes, transferTo
-
-
-
-
Field Detail
-
cleanup
protected final Cleanup<IOException> cleanup
The clean-up for closing the wrapped input stream as post-mortem action of thisAttributedInputStreamornullto onlyInputStream.close()the wrapped input stream.
-
-
Constructor Detail
-
AttributedInputStream
public AttributedInputStream(InputStream in)
Creates a new input stream wrapping the designated input stream and having arbitrary attributes.- Parameters:
in- The wrapped input stream.
-
AttributedInputStream
public AttributedInputStream(InputStream in, CleanupTask<IOException> cleanup)
Creates a new input stream wrapping the designated input stream and having arbitrary attributes and the designated clean-up task. If present, this task will be executed before closing the wrapped input stream- Parameters:
in- The wrapped input stream.cleanup- An additional clean-up task for the created instance. This will be executed inclose()and also in clean-up before closing the wrapped input stream. If this isnull, there will be no cleanup and the wrapped input stream has to be closed explicitly.
-
-
Method Detail
-
unwrap
protected InputStream unwrap()
Gets the wrapped input stream.- Returns:
- The wrapped input stream.
-
close
public final void close() throws IOException- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classFilterInputStream- Throws:
IOException
-
contains
public boolean contains(String name)
Description copied from interface:AttributableGets whether thisAttributablecontains an attribute value for the designated attribute name.- Specified by:
containsin interfaceAttributable- Parameters:
name- The name of the attribute for which an attribute value exists or not.- Returns:
- Whether this
Attributablecontains an attribute value for the designated attribute name.
-
containsType
public boolean containsType(String name, Class<?> valueType)
Description copied from interface:AttributableGets whether thisAttributablecontains an attribute value for the designated attribute name of the designated type.- Specified by:
containsTypein interfaceAttributable- Parameters:
name- The name of the attribute for which an attribute value exists or not.valueType- The type of the value of the attribute of the designated name.- Returns:
- Whether this
Attributablecontains an attribute value for the designated attribute name of the designate type.
-
get
public Object get(String name)
Description copied from interface:AttributableGets the value of the attribute with the designated name ornullif the attribute is not present or has anullvalue.- Specified by:
getin interfaceAttributable- Parameters:
name- The name of the attribute for which to retrieve the value.- Returns:
- The value of the attribute with the designated name or
nullif the attribute is not present or has anullvalue.
-
putIfAbsent
public Object putIfAbsent(String name, Object value)
Description copied from interface:AttributablePuts an attribute with the designated name and value if the attribute is not present yet and returnsnullor the existing current value.- Specified by:
putIfAbsentin interfaceAttributable- Parameters:
name- The name of the attribute which make available.value- The value of the attribute which to make available.- Returns:
- The previous value (always
null) or the current value (in case putting is refused since the attribute is already present).
-
computeIfAbsent
public Object computeIfAbsent(String name, Function<? super String,Object> compFunc)
Description copied from interface:AttributableComputes the value of the attribute with the designated name if the attribute is not present yet and returns the value (or the current value if already present and not computed).- Specified by:
computeIfAbsentin interfaceAttributable- Parameters:
name- The name of the attribute which make available.compFunc- The function computing the attribute value which to make available.- Returns:
- The previous value if already present (and therefore not computed) or the computed value.
-
remove
public Object remove(String name)
Description copied from interface:AttributableRemoves the attribute with the designated name and returns its current (previous) value.- Specified by:
removein interfaceAttributable- Parameters:
name- The name of the attribute which to remove.- Returns:
- The value of the removed attribute.
-
replace
public Object replace(String name, Object newValue)
Description copied from interface:AttributableReplaces the value of the attribute with the designated name if it has a value and returns its previous value. If the attribute does not have a value yet, it will not be added andnullwill be returned. Note thatnullwill also be returned when replacing the explicit attribute valuenull.- Specified by:
replacein interfaceAttributable- Parameters:
name- The name of the attribute which to replace.newValue- The value of the attribute with which to replace the existing value.- Returns:
- The replaced value of the attribute or
nullin case of an unknown attribute (and no replacement).
-
replace
public boolean replace(String name, Object oldValue, Object newValue)
Description copied from interface:AttributableReplaces the value of the attribute with the designated name if it has the designated value. If the attribute does not have a value yet, it will not be added andfalsewill be returned.- Specified by:
replacein interfaceAttributable- Parameters:
name- The name of the attribute which to replace.oldValue- The old value of the attribute which to replace.newValue- The value of the attribute with which to replace the old value.- Returns:
- Whether the attribute value has been replaced.
-
-