Package space.arim.dazzleconf.backend
Class InputOutputRoot
java.lang.Object
space.arim.dazzleconf.backend.InputOutputRoot
- All Implemented Interfaces:
BinaryRoot,DataRoot,ReadableRoot
A base class for data root that makes use of
InputStream and OutputStream.
This class is designed to be extended, and implementors are only required to define how InputStreams and
OutputStreams are to be provided, and whether data exists. This class then implements all other methods on
that basis. For example, to use a resource inside a jar file, implementors can provide the stream representing that
resource, while throwing an exception if output is requested.
class FromResource extends InputOutputRoot {
private final String resourceName;
FromResource(String resourceName) {
super(StandardCharsets.UTF_8);
this.resourceName = resourceName;
}
@Override
public <R> R openInputStream(@NonNull Operation<R, @NonNull InputStream> operation) throws IOException {
URL resourceURL = FromResource.class.getResource('/' + resourceName);
Objects.requireNonNull(resourceURL, "no jar resource");
try (InputStream inputStream = operation.handlesBuffering() ?
resourceURL.openStream() : new BufferedInputStream(resourceURL.openStream())) {
return operation.operateUsing(inputStream);
}
}
@Override
public <R> R openOutputStream(@NonNull Operation<R, @NonNull OutputStream> operation) throws IOException {
// Effectively a read-only data root, since jar file resources are not writable
throw new UnsupportedOperationException();
}
@Override
public boolean dataExists() throws IOException {
return true; // Assume the jar resource exists
}
}
-
Nested Class Summary
Nested classes/interfaces inherited from interface space.arim.dazzleconf.backend.DataRoot
DataRoot.Operation<R,U> -
Constructor Summary
ConstructorsConstructorDescriptionInputOutputRoot(@NonNull Charset charset) Sets the charset to use for read/write operations involving textual streams -
Method Summary
Modifier and TypeMethodDescription<R> RopenReadChannel(@NonNull DataRoot.Operation<R, @NonNull ReadableByteChannel> operation) Opens a read channel for the data<R> RopenReader(@NonNull DataRoot.Operation<R, @NonNull Reader> operation) Opens a reader to the data<R> RopenWriteChannel(@NonNull DataRoot.Operation<R, @NonNull WritableByteChannel> operation) Opens a write channel to the data<R> RopenWriter(@NonNull DataRoot.Operation<R, @NonNull Writer> operation) Opens a writer for the data@NonNull StringReads to a stringvoidwriteString(@NonNull String content) Writes to a stringMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface space.arim.dazzleconf.backend.BinaryRoot
openInputStream, openOutputStreamMethods inherited from interface space.arim.dazzleconf.backend.DataRoot
dataExists
-
Constructor Details
-
InputOutputRoot
Sets the charset to use for read/write operations involving textual streams- Parameters:
charset- the charset to use forReadableRootoperations
-
-
Method Details
-
readString
Description copied from interface:ReadableRootReads to a string- Specified by:
readStringin interfaceReadableRoot- Returns:
- the read content
- Throws:
IOException- upon failure to open
-
openReader
public <R> R openReader(@NonNull DataRoot.Operation<R, @NonNull Reader> operation) throws IOExceptionDescription copied from interface:ReadableRootOpens a reader to the data- Specified by:
openReaderin interfaceReadableRoot- Type Parameters:
R- the result type- Parameters:
operation- the operation- Returns:
- the operation result
- Throws:
IOException- upon failure to open
-
writeString
Description copied from interface:ReadableRootWrites to a string- Specified by:
writeStringin interfaceReadableRoot- Parameters:
content- the content to write- Throws:
IOException- upon failure to write
-
openWriter
public <R> R openWriter(@NonNull DataRoot.Operation<R, @NonNull Writer> operation) throws IOExceptionDescription copied from interface:ReadableRootOpens a writer for the data- Specified by:
openWriterin interfaceReadableRoot- Type Parameters:
R- the result type- Parameters:
operation- the operation- Returns:
- the operation result
- Throws:
IOException- upon failure to open
-
openReadChannel
public <R> R openReadChannel(@NonNull DataRoot.Operation<R, @NonNull ReadableByteChannel> operation) throws IOExceptionDescription copied from interface:BinaryRootOpens a read channel for the data- Specified by:
openReadChannelin interfaceBinaryRoot- Type Parameters:
R- the result type- Parameters:
operation- the operation- Returns:
- the operation result
- Throws:
IOException- upon failure to open
-
openWriteChannel
public <R> R openWriteChannel(@NonNull DataRoot.Operation<R, @NonNull WritableByteChannel> operation) throws IOExceptionDescription copied from interface:BinaryRootOpens a write channel to the data- Specified by:
openWriteChannelin interfaceBinaryRoot- Type Parameters:
R- the result type- Parameters:
operation- the operation- Returns:
- the operation result
- Throws:
IOException- upon failure to open
-