Class MemoryCache

  • All Implemented Interfaces:
    Cache, java.io.Closeable, java.lang.AutoCloseable, java.nio.channels.ByteChannel, java.nio.channels.Channel, java.nio.channels.ReadableByteChannel, java.nio.channels.SeekableByteChannel, java.nio.channels.WritableByteChannel

    final class MemoryCache
    extends java.lang.Object
    implements Cache
    • Field Summary

      Fields 
      Modifier and Type Field Description
      (package private) static int BLOCK_SIZE  
      private java.util.List<byte[]> cache  
      private java.nio.channels.ReadableByteChannel channel  
      private long length  
      private int maxBlock  
      private static byte[] NULL_BLOCK  
      private long position  
      private long start  
    • Constructor Summary

      Constructors 
      Constructor Description
      MemoryCache​(java.io.InputStream stream)  
      MemoryCache​(java.nio.channels.ReadableByteChannel channel)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()  
      (package private) byte[] fetchBlock()  
      void flushBefore​(long pos)  
      boolean isOpen()  
      long position()  
      java.nio.channels.SeekableByteChannel position​(long newPosition)  
      int read​(java.nio.ByteBuffer dest)  
      private int readBlock​(byte[] block)  
      long size()  
      java.nio.channels.SeekableByteChannel truncate​(long size)  
      int write​(java.nio.ByteBuffer src)  
      • Methods inherited from class java.lang.Object

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

      • NULL_BLOCK

        private static final byte[] NULL_BLOCK
      • cache

        private final java.util.List<byte[]> cache
      • channel

        private final java.nio.channels.ReadableByteChannel channel
      • maxBlock

        private int maxBlock
      • length

        private long length
      • position

        private long position
      • start

        private long start
    • Constructor Detail

      • MemoryCache

        MemoryCache​(java.io.InputStream stream)
      • MemoryCache

        public MemoryCache​(java.nio.channels.ReadableByteChannel channel)
    • Method Detail

      • fetchBlock

        byte[] fetchBlock()
                   throws java.io.IOException
        Throws:
        java.io.IOException
      • readBlock

        private int readBlock​(byte[] block)
                       throws java.io.IOException
        Throws:
        java.io.IOException
      • isOpen

        public boolean isOpen()
        Specified by:
        isOpen in interface java.nio.channels.Channel
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.nio.channels.Channel
        Specified by:
        close in interface java.io.Closeable
        Throws:
        java.io.IOException
      • read

        public int read​(java.nio.ByteBuffer dest)
                 throws java.io.IOException
        Specified by:
        read in interface java.nio.channels.ReadableByteChannel
        Specified by:
        read in interface java.nio.channels.SeekableByteChannel
        Throws:
        java.io.IOException
      • position

        public long position()
                      throws java.io.IOException
        Specified by:
        position in interface java.nio.channels.SeekableByteChannel
        Throws:
        java.io.IOException
      • position

        public java.nio.channels.SeekableByteChannel position​(long newPosition)
                                                       throws java.io.IOException
        Specified by:
        position in interface java.nio.channels.SeekableByteChannel
        Throws:
        java.io.IOException
      • size

        public long size()
                  throws java.io.IOException
        Specified by:
        size in interface java.nio.channels.SeekableByteChannel
        Throws:
        java.io.IOException
      • write

        public int write​(java.nio.ByteBuffer src)
        Specified by:
        write in interface java.nio.channels.SeekableByteChannel
        Specified by:
        write in interface java.nio.channels.WritableByteChannel
      • truncate

        public java.nio.channels.SeekableByteChannel truncate​(long size)
        Specified by:
        truncate in interface java.nio.channels.SeekableByteChannel
      • flushBefore

        public void flushBefore​(long pos)
        Specified by:
        flushBefore in interface Cache