Class DBFReaderJGeom

  • All Implemented Interfaces:
    java.lang.AutoCloseable

    public class DBFReaderJGeom
    extends java.lang.Object
    implements java.lang.AutoCloseable
    Reads records from a Shapefile (DBF format), decoding the fields data using a derived or specified Charset.

    The charset is derived in the next priority order:

    1. If a Charset instance is explicitly passed through the constructors of DBFReaderJGeom, then that charset will be used (regardless if it's a DBF standard supported charset or a custom one)
    2. Else, if the same directory that contains the DBF file also contains a sibling file which name differs from the DBF only by the
      cpg
      extension (for example, "myFile.dbf" and "myFile.cpg"), then its contents will be read, and the JVM will attempt finding a Charset match: if a matching Charset instance is retrieved by the JVM, then that charset will be used (regardless if it's a DBF standard supported charset or a custom one)
    3. Else, the DBF header will be read and the Code Page Mark (which is the 29th byte from the DBF stream) will be extracted. The extracted Code Page Mark will be then used to lookup for a mapped Charset through the DBFFileCodePage class. If a mapped DBFFileCodePage instance is found, then the JVM will attempt finding a charset match: if a matching Charset instance is retrieved by the JVM, then that charset will be used. Notice that this option is subject to DBF supported code pages.
    4. Else, if all attempts to find a valid Charset failed, then "ISO-8859-1" charset will be used, which is the JVM's default when decoding an array of bytes using the String(byte[]) constructor.

    • Constructor Summary

      Constructors 
      Constructor Description
      DBFReaderJGeom()  
      DBFReaderJGeom​(java.lang.String dbfName)
      Constructs an object out of the specified shapefile's DBF file, and that will decode the fields using the derived charset as explained in the DBFReaderJGeom class documentation.
      DBFReaderJGeom​(java.lang.String dbfFileName, java.nio.charset.Charset charset)
      Constructs an object out of the specified shapefile's DBF file, and that will decode the fields using the specified charset.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()  
      void closeDBF()
      closes this dbfile.
      java.nio.charset.Charset getCharset()
      Retrieves the Charset that is used when reading fields from the file; this charset is derived as explained in DBFReaderJGeom docs.
      java.lang.String getCodePage()
      Retrieves the Code Page found as text contents of the optional
      byte getCodePageMark()
      Retrieves the Code Page Mark found in the header of the
      java.lang.String getFieldData​(int ith, byte[] rec)
      get the value string for the ith field in the given record.
      int getFieldLength​(int nth)
      return the length of the specified field.
      java.lang.String getFieldName​(int ith)
      returns the name for the ith field of records in this file.
      java.lang.String[] getFieldNames()
      Retrieves all the field names in lower case, ordered by index.
      byte getFieldType​(int ith)
      get the type for the ith field in a record.
      byte[] getFieldTypes()
      Retrieves all the field data types, ordered by index.
      byte[] getRecord​(int nth)
      retrieve the nth record in the file.
      int numFields()
      return the number of fields in each record
      int numRecords()
      return the number of records in the DBF file.
      void openDBF​(java.lang.String name)
      Given a DBF file's name, open the file and read the header info.
      void print()
      nicely print out the general information of this shapefile.
      int recordSize()
      return the record size in the DBF file
      • Methods inherited from class java.lang.Object

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

      • DBFReaderJGeom

        public DBFReaderJGeom()
      • DBFReaderJGeom

        public DBFReaderJGeom​(java.lang.String dbfName)
                       throws java.io.IOException
        Constructs an object out of the specified shapefile's DBF file, and that will decode the fields using the derived charset as explained in the DBFReaderJGeom class documentation.
        Parameters:
        dbfName - the path of the DBF file.
        Throws:
        java.io.IOException - if any IO problem occurs
      • DBFReaderJGeom

        public DBFReaderJGeom​(java.lang.String dbfFileName,
                              java.nio.charset.Charset charset)
                       throws java.io.IOException
        Constructs an object out of the specified shapefile's DBF file, and that will decode the fields using the specified charset.
        Parameters:
        dbfFileName - the path of the DBF file.
        charset - the charset to decode the fields; if null is passed, then it will derive a charset as explained in the DBFReaderJGeom class documentation.
        Throws:
        java.io.IOException - if any IO problem occurs
    • Method Detail

      • openDBF

        public void openDBF​(java.lang.String name)
                     throws java.io.IOException
        Given a DBF file's name, open the file and read the header info. Records can be read afterwards.
        Throws:
        java.io.IOException
      • closeDBF

        public void closeDBF()
                      throws java.io.IOException
        closes this dbfile.
        Throws:
        java.io.IOException
      • close

        public void close()
                   throws java.lang.Exception
        Specified by:
        close in interface java.lang.AutoCloseable
        Throws:
        java.lang.Exception
      • numRecords

        public int numRecords()
        return the number of records in the DBF file.
      • numFields

        public int numFields()
        return the number of fields in each record
      • recordSize

        public int recordSize()
        return the record size in the DBF file
      • getRecord

        public byte[] getRecord​(int nth)
        retrieve the nth record in the file. return the pointer to the beginning of the record. the recordDeform method can be called on this pointer to interpret individual fields.
        Parameters:
        nth - determines the number of record to retrieve. Value must be from [0, numRecords())
      • getFieldData

        public java.lang.String getFieldData​(int ith,
                                             byte[] rec)
        get the value string for the ith field in the given record. This is an ascii representation of this field's actual value. For fields with type 'I' or 'N', Use atoi() or strtod() accordingly to convert the returned string into an integer or double. Null is returned if something is wrong (e.g., invalid field index). bug-9852192: explicitly set codePage if .cpg file available
      • getFieldLength

        public int getFieldLength​(int nth)
        return the length of the specified field.
      • getFieldName

        public java.lang.String getFieldName​(int ith)
        returns the name for the ith field of records in this file.
      • getFieldNames

        public java.lang.String[] getFieldNames()
        Retrieves all the field names in lower case, ordered by index.
        Returns:
        an array of numFields() length with all the field names
        Throws:
        java.lang.IllegalStateException - if the DBF is not opened.
      • getFieldTypes

        public byte[] getFieldTypes()
        Retrieves all the field data types, ordered by index.
        Returns:
        an array of numFields() length with all the field types
        Throws:
        java.lang.IllegalStateException - if the DBF is not opened.
      • getFieldType

        public byte getFieldType​(int ith)
        get the type for the ith field in a record. returns a single character the meaning of which is defined as below:
        
                'C': character
        
                'D': Date (8 digits: YYYYMMDD)
        
                'F': Floating point binary numeric: -. 0 1 2 3 4 5 6 7 8 9
        
                'G': General. All OEM characters or OLE.
        
                'L': Logical: ? Y y N  n T t F f (? initially)
        
                'M': Memo
        
                'N': Float: - . 0 1 2 3 4 5 6 7 8 9
        
                'I': Integer: - 0 1 2 3 4 5 6 7 8 9
        
         
      • print

        public void print()
        nicely print out the general information of this shapefile.
      • getCodePage

        public java.lang.String getCodePage()
        Retrieves the Code Page found as text contents of the optional
        .cpg
        file, if provided.
        Returns:
        the Code Page as read from the
        .cpg
        file; null if no
        .cpg
        file was provided, or could not be read.
      • getCodePageMark

        public byte getCodePageMark()
        Retrieves the Code Page Mark found in the header of the
        .dbf
        file at the 29th position of the byte stream. This byte value can be used to derive a Code Page supported by
        .dbf
        files.
        Returns:
        the Code Page Mark value if found; 0 if not found, or if the Code Page Mark was zero.
      • getCharset

        public java.nio.charset.Charset getCharset()
        Retrieves the Charset that is used when reading fields from the file; this charset is derived as explained in DBFReaderJGeom docs.
        Returns:
        the Charset to decode the fields