Class CAFS

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable, java.lang.Iterable<SHA1>

    public class CAFS
    extends java.lang.Object
    implements java.io.Closeable, java.lang.Iterable<SHA1>
    CAFS implements a SHA-1 based file store. The basic idea is that every file in the universe has a unique SHA-1. Hard to believe but people smarter than me have come to that conclusion. This class maintains a compressed store of SHA-1 identified files. So if you have the SHA-1, you can get the contents. This makes it easy to store a SHA-1 instead of the whole file or maintain a naming scheme. An added advantage is that it is always easy to verify you get the right stuff. The SHA-1 Content Addressable File Store is the core underlying idea in Git.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      (package private) static java.lang.String ALGORITHM  
      (package private) static byte[] CAFE  
      (package private) static byte[] CAFS  
      (package private) java.nio.channels.FileChannel channel  
      (package private) static int HEADERLENGTH  
      (package private) java.io.File home  
      (package private) Index index  
      (package private) static java.lang.String INDEXFILE  
      (package private) static int KEYLENGTH  
      (package private) java.io.RandomAccessFile store  
      (package private) static java.lang.String STOREFILE  
    • Constructor Summary

      Constructors 
      Constructor Description
      CAFS​(java.io.File home, boolean create)
      Constructor for a Content Addressable File Store
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      (package private) short checksum​(int flags, int compressedLength, int totalLength, byte[] sha1)  
      void close()  
      boolean exists​(byte[] sha1)  
      private java.io.InputStream getSha1Stream​(SHA1 sha1, byte[] buffer, int total)  
      boolean isEmpty()  
      java.util.Iterator<SHA1> iterator()  
      java.io.InputStream read​(SHA1 sha1)
      Read the contents of a sha 1 key.
      void reindex()  
      private void update​(byte[] sha1, byte[] compressed, int totalLength)
      Update a record in the store, assuming the store is at the right position.
      private SHA1 verifyEntry​(java.io.RandomAccessFile in)  
      private boolean verifySignature​(java.io.DataInput din, byte[] org)  
      SHA1 write​(java.io.InputStream in)
      Store an input stream in the CAFS while calculating and returning the SHA-1 code.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.lang.Iterable

        forEach, spliterator
    • Constructor Detail

      • CAFS

        public CAFS​(java.io.File home,
                    boolean create)
             throws java.lang.Exception
        Constructor for a Content Addressable File Store
        Parameters:
        home -
        create -
        Throws:
        java.lang.Exception
    • Method Detail

      • write

        public SHA1 write​(java.io.InputStream in)
                   throws java.lang.Exception
        Store an input stream in the CAFS while calculating and returning the SHA-1 code.
        Parameters:
        in - The input stream to store.
        Returns:
        The SHA-1 code.
        Throws:
        java.lang.Exception - if anything goes wrong
      • read

        public java.io.InputStream read​(SHA1 sha1)
                                 throws java.lang.Exception
        Read the contents of a sha 1 key.
        Parameters:
        sha1 - The key
        Returns:
        An Input Stream on the content or null of key not found
        Throws:
        java.lang.Exception
      • exists

        public boolean exists​(byte[] sha1)
                       throws java.lang.Exception
        Throws:
        java.lang.Exception
      • reindex

        public void reindex()
                     throws java.lang.Exception
        Throws:
        java.lang.Exception
      • close

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

        private SHA1 verifyEntry​(java.io.RandomAccessFile in)
                          throws java.io.IOException,
                                 java.security.NoSuchAlgorithmException
        Throws:
        java.io.IOException
        java.security.NoSuchAlgorithmException
      • verifySignature

        private boolean verifySignature​(java.io.DataInput din,
                                        byte[] org)
                                 throws java.io.IOException
        Throws:
        java.io.IOException
      • getSha1Stream

        private java.io.InputStream getSha1Stream​(SHA1 sha1,
                                                  byte[] buffer,
                                                  int total)
                                           throws java.security.NoSuchAlgorithmException
        Throws:
        java.security.NoSuchAlgorithmException
      • update

        private void update​(byte[] sha1,
                            byte[] compressed,
                            int totalLength)
                     throws java.io.IOException
        Update a record in the store, assuming the store is at the right position.
        Parameters:
        sha1 - The checksum
        compressed - The compressed length
        totalLength - The uncompressed length
        Throws:
        java.io.IOException - The exception
      • checksum

        short checksum​(int flags,
                       int compressedLength,
                       int totalLength,
                       byte[] sha1)
      • iterator

        public java.util.Iterator<SHA1> iterator()
        Specified by:
        iterator in interface java.lang.Iterable<SHA1>
      • isEmpty

        public boolean isEmpty()
                        throws java.io.IOException
        Throws:
        java.io.IOException