public void writeHeader ( WritableByteChannel out ) throws IOException { // take care of the annoying case where no records have been added... if ( headerLength == - 1 ) { headerLength = MINIMUM_HEADER ; } ByteBuffer buffer = ByteBuffer . allocateDirect ( headerLength ) ; buffer . order ( ByteOrder . LITTLE_ENDIAN ) ; // write the output file type. buffer . put ( MAGIC ) ; // write the date stuff Calendar c = Calendar . getInstance ( ) ; c . setTime ( new Date ( ) ) ; buffer . put ( ( byte ) ( c . get ( Calendar . YEAR ) % 100 ) ) ; buffer . put ( ( byte ) ( c . get ( Calendar . MONTH ) + 1 ) ) ; buffer . put ( ( byte ) ( c . get ( Calendar . DAY_OF_MONTH ) ) ) ; // write the number of records in the datafile. buffer . putInt ( recordCnt ) ; // write the length of the header structure. buffer . putShort ( ( short ) headerLength ) ; // write the length of a record buffer . putShort ( ( short ) recordLength ) ; // // write the reserved bytes in the header buffer . position ( buffer . position ( ) + 17 ) ; // skip / skip the reserved bytes in the header. // write Language driver buffer . put ( getEncodingByte ( ) ) ; // skip reserved buffer . position ( buffer . position ( ) + 2 ) ; // write all of the header records int tempOffset = 0 ; for ( DbaseField field : fields ) { // write the field name byte [ ] fieldName = field . fieldName . getBytes ( fileEncoding ) ; for ( int j = 0 ; j < 11 ; j ++ ) { if ( fieldName . length > j ) { buffer . put ( fieldName [ j ] ) ; } else { buffer . put ( ( byte ) 0 ) ; } } // write the field type buffer . put ( ( byte ) field . fieldType ) ; // // write the field data address, offset from the start of the // record. buffer . putInt ( tempOffset ) ; tempOffset += field . fieldLength ; // write the length of the field. buffer . put ( ( byte ) field . fieldLength ) ; // write the decimal count. buffer . put ( ( byte ) field . decimalCount ) ; // write the reserved bytes. // for (in j=0; jj<14; j++) out.writeByteLE(0); buffer . position ( buffer . position ( ) + 14 ) ; } // write the end of the field definitions marker buffer . put ( ( byte ) 0x0D ) ; buffer . position ( 0 ) ; int r = buffer . remaining ( ) ; do { r -= out . write ( buffer ) ; } while ( r > 0 ) ; }
Write the header data to the DBF file .