java.lang.Object
org.rrd4j.core.jrrd.RRDFile
All Implemented Interfaces:
Constants

class RRDFileextends Object implements Constants
This class is used read information from an RRD file. Writing to RRD files is not currently supported. It uses NIO's RandomAccessFile to read the file

Currently this can read RRD files that were generated on Solaris (Sparc) and Linux (x86).

  • Field Details

    • CLEANER_METHOD

      private static final Method CLEANER_METHOD
      Reflected cleaner method of the mapped buffer.
    • CLEAN_METHOD

      private static final Method CLEAN_METHOD
      Reflected clean method of the mapped buffer.
    • INVOKE_CLEANER

      private static final Method INVOKE_CLEANER
      Reflected unsafe method cleaning a buffer.
    • UNSAFE

      private static final Object UNSAFE
      Unsafe instance for unmapping, null if unavailable.
    • alignment

      private int alignment
      Byte alignment of the values in the file.
    • longSize

      private int longSize
      Byte size of a long in the file.
    • underlying

      private final FileInputStream underlying
      Stream backing the mapped file.
    • mappedByteBuffer

      private final MappedByteBuffer mappedByteBuffer
      Mapping of the whole file.
    • order

      private ByteOrder order
      Byte order of the values in the file.
  • Constructor Details

    • RRDFile

      RRDFile(String name) throws IOException
      Open RRD file
      Parameters:
      name - the filesystem path of the RRD to memory-map
      Throws:
      IOException - if the file cannot be opened or mapped
    • RRDFile

      RRDFile(File file) throws IOException
      Open RRD file
      Parameters:
      file - the RRD to memory-map read-only
      Throws:
      IOException - if the file cannot be opened or mapped, or if it is larger than 2**31 bytes, which a ByteBuffer cannot address
  • Method Details

    • initDataLayout

      private void initDataLayout(File file) throws IOException
      Reads the data layout header of the file.
      Parameters:
      file - the RRD file to read
      Throws:
      IOException - if the file cannot be read
    • indexOf

      private int indexOf(byte[] pattern, byte[] array)
      Finds a byte pattern in an array.
      Parameters:
      pattern - the bytes to find
      array - the array to search
      Returns:
      the position, negative if absent
    • isBigEndian

      boolean isBigEndian()
      Reports the byte order of the mapped file.
      Returns:
      true when the order is big endian
    • getAlignment

      int getAlignment()
      Byte alignment this file was mapped with.
      Returns:
      4 for 32 bit RRDs, 8 for 64 bit ones
    • readDouble

      double readDouble()
      Reads a big-endian 8-byte float at the read position.
      Returns:
      the big-endian double read from the mapped file, advancing the position by 8 bytes
    • readInt

      int readInt()
      Reads a 4-byte integer at the read position.
      Returns:
      the next 4-byte integer at the read position
    • readLong

      int readLong()
      Reads a long, narrowed to int, honouring this file's long size.
      Returns:
      the value read from the mapped file, narrowed to an int
    • readString

      String readString(int maxLength)
      Reads a fixed-width NUL-padded UTF-8 field.
      Parameters:
      maxLength - the fixed field width to read, NUL padding included
      Returns:
      the next fixed-width field decoded as UTF-8, with the NUL padding trimmed
    • skipBytes

      void skipBytes(int n)
      Skip bytes
      Parameters:
      n - the number of bytes to advance the read position by
    • align

      int align(int boundary)
      Advances the read position to the given alignment boundary.
      Parameters:
      boundary - the alignment boundary in bytes to advance to
      Returns:
      the bytes skipped, 0 if already aligned
    • align

      int align()
      Advances the read position to the file's own alignment.
      Returns:
      the bytes skipped, 0 if already aligned
    • info

      long info()
      Reports the current read position.
      Returns:
      the byte offset of the next read in the mapped file
    • getFilePointer

      long getFilePointer()
      Reports the current read position.
      Returns:
      the byte offset of the next read
    • unmapFile

      private void unmapFile()
      Unmaps the file so it can be deleted.
    • close

      void close() throws IOException
      Close the file
      Throws:
      IOException - if the underlying stream cannot be closed
    • read

      void read(ByteBuffer bb)
      Read bytes
      Parameters:
      bb - the buffer to fill from the current read position
    • getUnivalArray

      UnivalArray getUnivalArray(int size)
      Allocates a unival array at the current position.
      Parameters:
      size - the number of elements the array holds
      Returns:
      a new array of size elements
    • getBits

      int getBits()
      Width of a long in this file, in bits.
      Returns:
      32 or 64, the long size times 8
    • seek

      public void seek(long position)
      Moves the read position to an absolute offset.
      Parameters:
      position - the absolute byte offset to read from next
    • seekToEndOfFile

      public void seekToEndOfFile()
      Moves the read position to the end of the mapped region.