package model;

import com.esri.dbf.DBFField;
import com.esri.dbf.DBFHeader;
import com.esri.dbf.DBFReader;
import com.esri.dbf.DBFType;

import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.FloatWritable;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.Writable;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.GregorianCalendar;
import java.util.List;

/**
 */
public class DBFRead
{

    public static void main(String[] argv){
        if (1 == argv.length){
            File file = new File(argv[0]);
            DataInputStream inputStream = null;
            try {
                inputStream = new DataInputStream(new FileInputStream(file));

                final DBFReader dbfReader = new DBFReader(new DataInputStream(inputStream));

                final List<DBFField> fields = dbfReader.getFields();

                while (DBFType.END != dbfReader.nextDataType()){
                    int field = 0;
                    for (DBFField meta: fields){
                        Writable actual = dbfReader.readFieldWritable(field++);
                        switch(DBFType.Data.For(meta)){
                        case Short:
                            {
                                IntWritable ovalue = (IntWritable)actual;
                                int value = ovalue.get();
                                System.out.printf("name '%s', type 'SHORT', value '%d'%n",meta.fieldName,value);
                            }
                            break;
                        case Long:
                            {
                                LongWritable ovalue = (LongWritable)actual;
                                long value = ovalue.get();
                                System.out.printf("name '%s', type 'LONG', value '%d'%n",meta.fieldName,value);
                            }
                            break;
                        case Float:
                            {
                                FloatWritable ovalue = (FloatWritable)actual;
                                float value = ovalue.get();
                                System.out.printf("name '%s', type 'FLOAT', value '%f'%n",meta.fieldName,value);
                            }
                            break;
                        case Double:
                            {
                                DoubleWritable ovalue = (DoubleWritable)actual;
                                double value = ovalue.get();
                                System.out.printf("name '%s', type 'DOUBLE', value '%f'%n",meta.fieldName,value);
                            }
                            break;
                        case String:
                            {
                                Text ovalue = (Text)actual;
                                String value = ovalue.toString().trim();
                                System.out.printf("name '%s', type 'STRING', value '%s'%n",meta.fieldName,value);
                            }
                            break;
                        case Date:
                            {
                                LongWritable ovalue = (LongWritable)actual;
                                long value = ovalue.get();
                                System.out.printf("name '%s', type 'DATE', value '%d'%n",meta.fieldName,value);
                            }
                            break;
                        default:
                            System.out.printf("Unrecognized DBF record name '%s', type '%c', class '%s'%n",meta.fieldName,meta.dataType,actual.getClass().getName());
                            break;
                        }
                    }

                    break;//
                }
            }
            catch (IOException exc){
                System.err.println("usage: model.DBFRead <file.dbf>");
                System.exit(1);
            }
            finally {
                if (null != inputStream){
                    try {
                        inputStream.close();
                    }
                    catch (IOException ignore){
                    }
                }
            }
        }
        else {
            System.err.println("usage: model.DBFRead <file.dbf>");
            System.exit(1);
        }
    }
}

@@ -519,44 +519,8 @@
 am;%0A
-import java.util.GregorianCalendar;%0A
 impo
@@ -1536,45 +1536,16 @@
 tf(%22
-name '%25s', type 'SHORT', value 
+%25s=
 '%25d'
-%25n
+ 
 %22,me
@@ -1885,44 +1885,16 @@
 tf(%22
-name '%25s', type 'LONG', value 
+%25s=
 '%25d'
-%25n
+ 
 %22,me
@@ -2238,45 +2238,16 @@
 tf(%22
-name '%25s', type 'FLOAT', value 
+%25s=
 '%25f'
-%25n
+ 
 %22,me
@@ -2595,46 +2595,16 @@
 tf(%22
-name '%25s', type 'DOUBLE', value 
+%25s=
 '%25f'
-%25n
+ 
 %22,me
@@ -2944,46 +2944,16 @@
 tf(%22
-name '%25s', type 'STRING', value 
+%25s=
 '%25s'
-%25n
+ 
 %22,me
@@ -3293,44 +3293,16 @@
 tf(%22
-name '%25s', type 'DATE', value 
+%25s=
 '%25d'
-%25n
+ 
 %22,me
@@ -3694,16 +3694,29 @@
     
-break;//
+System.out.println();
 %0A   
@@ -3755,34 +3755,43 @@
          catch (
-IO
+java.io.EOF
 Exception exc)%7B%0A
@@ -3810,60 +3810,101 @@
     
-System.err.println(%22usage: model.DBFRead %3Cfile.dbf%3E%22
+return;%0A            %7D%0A            catch (Exception exc)%7B%0A                exc.printStackTrace(
 );%0A 

increment dbf reader