Class InputOutputRoot

java.lang.Object
space.arim.dazzleconf.backend.InputOutputRoot
All Implemented Interfaces:
BinaryRoot, DataRoot, ReadableRoot

public abstract class InputOutputRoot extends Object implements ReadableRoot, BinaryRoot
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
     }
 }
     
 

  • Constructor Details

    • InputOutputRoot

      public InputOutputRoot(@NonNull Charset charset)
      Sets the charset to use for read/write operations involving textual streams
      Parameters:
      charset - the charset to use for ReadableRoot operations
  • Method Details