Class InMemoryFile


  • public class InMemoryFile
    extends java.lang.Object
    An in-memory "file" that can be used with some ToFileWriter and/or FromFileReaderr implementations instead of a File.

    To dynamically create file contents that only exist in memory (for download): Feed an instance of InMemoryFile rather than File to a ToFileWriter that support doing so (like the TextLineWriter), and keep a reference to the InMemoryFile instance. When done writing you get the (file) contents from that instance.

    To parse some data you have in memory: Create an instance of InMemoryFile using one of the constructors that take byte[] or String as input, and feed that to a FromFileReader that support doing so (like the TextLineReader).

    Note that you can obtain both OutputStream and InputStream instances from an InMemoryFile – you can write to AND (later) read from the same instance.

    This class is essentially a byte[] wrapper making use of ByteArrayInputStream and ByteArrayOutputStream.

    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      byte[] getContentsAsByteArray()  
      java.lang.String getContentsAsString()  
      java.util.Optional<java.lang.String> getName()
      File Name
      java.util.Optional<java.lang.String> getType()
      MIME Type
      InMemoryFile name​(java.lang.String name)  
      java.io.InputStream newInputStream()
      Creates a new InputStream with each invocation (but keeps a reference to it for later internal use).
      java.io.OutputStream newOutputStream()
      Creates a new OutputStream with each invocation (but keeps a reference to it for later internal use).
      void setName​(java.lang.String name)  
      void setType​(java.lang.String type)  
      InMemoryFile type​(java.lang.String type)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • myInitialContents

        private byte[] myInitialContents
      • myInputStream

        private java.io.ByteArrayInputStream myInputStream
      • myName

        private java.lang.String myName
      • myOutputStream

        private java.io.ByteArrayOutputStream myOutputStream
      • myType

        private java.lang.String myType
    • Constructor Detail

      • InMemoryFile

        public InMemoryFile()
      • InMemoryFile

        public InMemoryFile​(byte[] contents)
      • InMemoryFile

        public InMemoryFile​(java.lang.String contents)
    • Method Detail

      • getContentsAsByteArray

        public byte[] getContentsAsByteArray()
      • getContentsAsString

        public java.lang.String getContentsAsString()
      • getName

        public java.util.Optional<java.lang.String> getName()
        File Name
      • getType

        public java.util.Optional<java.lang.String> getType()
        MIME Type
      • name

        public InMemoryFile name​(java.lang.String name)
      • newInputStream

        public java.io.InputStream newInputStream()
        Creates a new InputStream with each invocation (but keeps a reference to it for later internal use). The actual "input" is taken from the contents provided to the constructor or what was later written to an OutputStream obtained via newOutputStream().
      • newOutputStream

        public java.io.OutputStream newOutputStream()
        Creates a new OutputStream with each invocation (but keeps a reference to it for later internal use). Calling this method also clears any/all cached previous contents – you're expected to create new content with this.
      • setName

        public void setName​(java.lang.String name)
      • setType

        public void setType​(java.lang.String type)
      • type

        public InMemoryFile type​(java.lang.String type)