com.unister.semweb.drums.file
Class AbstractHeaderFile

java.lang.Object
  extended by com.unister.semweb.drums.file.AbstractHeaderFile
Direct Known Subclasses:
HeaderIndexFile

public abstract class AbstractHeaderFile
extends java.lang.Object

Header structure:

+-----------+--------------+---------------+---------------+
| FILE SIZE | FILLED UP TO | ............. | ............. |
| 8 bytes . | 8 bytes .... | ............. | ............. |
+-----------+--------------+---------------+---------------+
= 1024 bytes (to have enough space for more values)

To use this class correctly, have a look at the following methods:

  • read(long offset, ByteBuffer destBuffer)
  • write(long offset, ByteBuffer sourceBuffer)
  • append(ByteBuffer sourceBuffer)
  • dbfile.getFilledUpToExclHeader()

    Author:
    Martin Nettling

    Nested Class Summary
    static class AbstractHeaderFile.AccessMode
              The possible AccessModes.
     
    Field Summary
     int DEFAULT_SIZE
              the Default size of a file. (16MB)
    static int HEADER_SIZE
              the size of the header
     java.io.File osFile
              the file to access
     int RETRY_CONNECT_WAITTIME
              the time to wait for a retry if a file is locked
     
    Constructor Summary
    AbstractHeaderFile()
               
     
    Method Summary
    abstract  long append(byte[] sourceBuffer)
              appends the given sourceBuffer to the file and returns the file position of the appended entry
    abstract  long append(java.nio.ByteBuffer sourceBuffer)
              appends the given sourceBuffer to the file and returns the file position of the appended entry
     void clear()
              sets the start of the content to zero
     void close()
              closes all open channels and the file
     void delete()
              tries to delete the file
    abstract  void enlargeFile(long size)
              enlarges the file by the given size
     long getFilledUpFromContentStart()
               
     long getFilledUpTo()
               
     long getFreeSpace()
               
     java.lang.String getName()
               
     long getRemainingBytes(long offset)
              returns the remaining bytes between filledUpTo and the given offset
     boolean isOpen()
               
     void lock()
              tries to lock the file
     void openChannel()
              opens the RandomAccessFile and the corresponding FileChannel.
    abstract  int read(long offset, byte[] destBuffer)
              Reads x bytes from the file to the given byte array, where x is the minimum of the size of the given byte array and the remaining written bytes in the file.
    abstract  int read(long offset, java.nio.ByteBuffer destBuffer)
              Reads x bytes from the file to the given ByteBuffer, where x is the minimum of the capacity of the buffer and the remaining written bytes in the file.
     void unlock()
              tries to unlock the file
    abstract  void write(long offset, byte[] sourceBuffer)
              writes the bytes from the given Byte-array to the file beginning at offset.
    abstract  void write(long offset, java.nio.ByteBuffer sourceBuffer)
              writes the bytes from the given ByteBuffer to the file beginning at offset.
     
    Methods inherited from class java.lang.Object
    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
     

    Field Detail

    DEFAULT_SIZE

    public final int DEFAULT_SIZE
    the Default size of a file. (16MB)

    See Also:
    Constant Field Values

    RETRY_CONNECT_WAITTIME

    public final int RETRY_CONNECT_WAITTIME
    the time to wait for a retry if a file is locked

    See Also:
    Constant Field Values

    HEADER_SIZE

    public static final int HEADER_SIZE
    the size of the header

    See Also:
    Constant Field Values

    osFile

    public java.io.File osFile
    the file to access

    Constructor Detail

    AbstractHeaderFile

    public AbstractHeaderFile()
    Method Detail

    enlargeFile

    public abstract void enlargeFile(long size)
                              throws java.io.IOException
    enlarges the file by the given size

    Parameters:
    size -
    Throws:
    java.io.IOException

    write

    public abstract void write(long offset,
                               java.nio.ByteBuffer sourceBuffer)
                        throws java.io.IOException
    writes the bytes from the given ByteBuffer to the file beginning at offset.

    Parameters:
    offset -
    sourceBuffer -
    Throws:
    java.io.IOException

    write

    public abstract void write(long offset,
                               byte[] sourceBuffer)
                        throws java.io.IOException
    writes the bytes from the given Byte-array to the file beginning at offset.

    Parameters:
    offset -
    sourceBuffer -
    Throws:
    java.io.IOException

    append

    public abstract long append(byte[] sourceBuffer)
                         throws java.io.IOException
    appends the given sourceBuffer to the file and returns the file position of the appended entry

    Parameters:
    sourceBuffer -
    Returns:
    the position in the file after inserting the given sourceBuffer
    Throws:
    java.io.IOException

    append

    public abstract long append(java.nio.ByteBuffer sourceBuffer)
                         throws java.io.IOException
    appends the given sourceBuffer to the file and returns the file position of the appended entry

    Parameters:
    sourceBuffer -
    Returns:
    the position in the file after inserting the given sourceBuffer
    Throws:
    java.io.IOException

    read

    public abstract int read(long offset,
                             java.nio.ByteBuffer destBuffer)
                      throws java.io.IOException
    Reads x bytes from the file to the given ByteBuffer, where x is the minimum of the capacity of the buffer and the remaining written bytes in the file.

    Parameters:
    offset -
    destBuffer -
    Returns:
    the number of bytes read
    Throws:
    java.io.IOException

    read

    public abstract int read(long offset,
                             byte[] destBuffer)
                      throws java.io.IOException
    Reads x bytes from the file to the given byte array, where x is the minimum of the size of the given byte array and the remaining written bytes in the file.

    Parameters:
    offset -
    destBuffer -
    Returns:
    the number of bytes read
    Throws:
    java.io.IOException

    getRemainingBytes

    public long getRemainingBytes(long offset)
    returns the remaining bytes between filledUpTo and the given offset

    Parameters:
    offset -
    Returns:
    long

    clear

    public void clear()
    sets the start of the content to zero


    getFilledUpFromContentStart

    public long getFilledUpFromContentStart()
    Returns:
    the offset in bytes, to where the file is filled

    getFilledUpTo

    public long getFilledUpTo()
    Returns:
    the offset in bytes, to where the file is filled

    getFreeSpace

    public long getFreeSpace()
    Returns:
    the number of available bytes

    openChannel

    public void openChannel()
                     throws FileLockException,
                            java.io.IOException
    opens the RandomAccessFile and the corresponding FileChannel. Reads the header.

    Throws:
    FileLockException
    java.io.IOException

    unlock

    public void unlock()
                throws java.io.IOException
    tries to unlock the file

    Throws:
    java.io.IOException

    lock

    public void lock()
              throws FileLockException,
                     java.io.IOException
    tries to lock the file

    Throws:
    FileLockException
    java.io.IOException

    isOpen

    public boolean isOpen()
    Returns:
    true, if there is access to the file

    close

    public void close()
    closes all open channels and the file


    delete

    public void delete()
                throws java.io.IOException
    tries to delete the file

    Throws:
    java.io.IOException

    getName

    public java.lang.String getName()
    Returns:
    the name of the underlying OS-File