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
AnAttributedInputStream
wraps 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>
cleanup
The clean-up for closing the wrapped input stream as post-mortem action of thisAttributedInputStream
ornull
to 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 void
close()
Object
computeIfAbsent(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).boolean
contains(String name)
Gets whether thisAttributable
contains an attribute value for the designated attribute name.boolean
containsType(String name, Class<?> valueType)
Gets whether thisAttributable
contains an attribute value for the designated attribute name of the designated type.Object
get(String name)
Gets the value of the attribute with the designated name ornull
if the attribute is not present or has anull
value.Object
putIfAbsent(String name, Object value)
Puts an attribute with the designated name and value if the attribute is not present yet and returnsnull
or the existing current value.Object
remove(String name)
Removes the attribute with the designated name and returns its current (previous) value.Object
replace(String name, Object newValue)
Replaces the value of the attribute with the designated name if it has a value and returns its previous value.boolean
replace(String name, Object oldValue, Object newValue)
Replaces the value of the attribute with the designated name if it has the designated value.protected InputStream
unwrap()
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 thisAttributedInputStream
ornull
to 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:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classFilterInputStream
- Throws:
IOException
-
contains
public boolean contains(String name)
Description copied from interface:Attributable
Gets whether thisAttributable
contains an attribute value for the designated attribute name.- Specified by:
contains
in interfaceAttributable
- Parameters:
name
- The name of the attribute for which an attribute value exists or not.- Returns:
- Whether this
Attributable
contains an attribute value for the designated attribute name.
-
containsType
public boolean containsType(String name, Class<?> valueType)
Description copied from interface:Attributable
Gets whether thisAttributable
contains an attribute value for the designated attribute name of the designated type.- Specified by:
containsType
in 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
Attributable
contains an attribute value for the designated attribute name of the designate type.
-
get
public Object get(String name)
Description copied from interface:Attributable
Gets the value of the attribute with the designated name ornull
if the attribute is not present or has anull
value.- Specified by:
get
in 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
null
if the attribute is not present or has anull
value.
-
putIfAbsent
public Object putIfAbsent(String name, Object value)
Description copied from interface:Attributable
Puts an attribute with the designated name and value if the attribute is not present yet and returnsnull
or the existing current value.- Specified by:
putIfAbsent
in 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:Attributable
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).- Specified by:
computeIfAbsent
in 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:Attributable
Removes the attribute with the designated name and returns its current (previous) value.- Specified by:
remove
in 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:Attributable
Replaces 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 andnull
will be returned. Note thatnull
will also be returned when replacing the explicit attribute valuenull
.- Specified by:
replace
in 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
null
in case of an unknown attribute (and no replacement).
-
replace
public boolean replace(String name, Object oldValue, Object newValue)
Description copied from interface:Attributable
Replaces 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 andfalse
will be returned.- Specified by:
replace
in 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.
-
-