Class MemoryLinkedOutputStream

  • All Implemented Interfaces:
    java.io.Closeable, java.io.Flushable, java.lang.AutoCloseable

    public class MemoryLinkedOutputStream
    extends java.io.OutputStream
    This class implements an OutputStream that writes everything in memory, and never flushes the data to disk. It uses a Linked list of byte[] arrays, probably of different sizes, to keep track of the new allocations. This class needs more memory (the pointers) than the MemoryOutputStream class, but it avoids the reallocation of arrays in memory.
    Author:
    Roi Blanco
    • Constructor Summary

      Constructors 
      Constructor Description
      MemoryLinkedOutputStream()
      Instanciates a MemoryLinkedOutputStream with the buffer size set to DEFAULT_BUFFER_SIZE
      MemoryLinkedOutputStream​(int bufferSize)
      Instanciates a MemoryLinkedOutputStream specifying the buffer size.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void beginRead()
      Sets the structure for reading
      void flush()
      Empty method
      byte[] getBuffer()  
      MemoryLinkedOutputStream getNext()  
      int getPos()  
      boolean isNext()  
      void nextBuffer()
      Moves the pointer to the next buffer (reading)
      void write​(int b)
      Writes a byte into the current buffer.
      • Methods inherited from class java.io.OutputStream

        close, nullOutputStream, write, write
      • Methods inherited from class java.lang.Object

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

      • DEFAULT_BUFFER_SIZE

        public static final int DEFAULT_BUFFER_SIZE
        The default size of the internal buffer in bytes
        See Also:
        Constant Field Values
    • Constructor Detail

      • MemoryLinkedOutputStream

        public MemoryLinkedOutputStream()
        Instanciates a MemoryLinkedOutputStream with the buffer size set to DEFAULT_BUFFER_SIZE
      • MemoryLinkedOutputStream

        public MemoryLinkedOutputStream​(int bufferSize)
        Instanciates a MemoryLinkedOutputStream specifying the buffer size.
        Parameters:
        bufferSize - int size of the first buffer
    • Method Detail

      • getPos

        public int getPos()
        Returns:
        the position in the current buffer.
      • beginRead

        public void beginRead()
        Sets the structure for reading
      • write

        public void write​(int b)
                   throws java.io.IOException
        Writes a byte into the current buffer. If it fills the buffer, it moves to the next one (doubling the capacity).
        Specified by:
        write in class java.io.OutputStream
        Parameters:
        b - the byte to write
        Throws:
        java.io.IOException - if an I/O error occurs.
      • getBuffer

        public byte[] getBuffer()
        Returns:
        the current byte[] buffer.
      • isNext

        public boolean isNext()
        Returns:
        true if there is a buffer next to the current one.
      • nextBuffer

        public void nextBuffer()
        Moves the pointer to the next buffer (reading)
      • flush

        public void flush()
                   throws java.io.IOException
        Empty method
        Specified by:
        flush in interface java.io.Flushable
        Overrides:
        flush in class java.io.OutputStream
        Throws:
        java.io.IOException