writeHeader method
Write the header data to the DBF file.
@param out A channel to write to. If you have an OutputStream you can obtain the correct channel by using java.nio.Channels.newChannel(OutputStream out). @ If errors occur.
Implementation
Future<void> writeHeader(FileWriter out) async {
// take care of the annoying case where no records have been added...
if (headerLength == -1) {
headerLength = MINIMUM_HEADER;
}
Endian endian = Endian.little;
List<int> buffer = []; //List(headerLength);
// write the output file type.
buffer.add(MAGIC);
// write the date stuff
DateTime dt = DateTime.now();
// // Calendar c = Calendar.getInstance();
// c.setTime(Date());
buffer.add(dt.year % 100); // (byte) (c.get(Calendar.YEAR) % 100));
buffer.add(dt.month); // (byte) (c.get(Calendar.MONTH) + 1));
buffer.add(dt.day); // (byte) (c.get(Calendar.DAY_OF_MONTH)));
// write the number of records in the datafile.
// buffer.putInt(recordCnt);
buffer.addAll(
ByteConversionUtilities.bytesFromInt32(recordCnt, endian: endian));
// write the length of the header structure.
// buffer.putShort((short) headerLength);
buffer.addAll(
ByteConversionUtilities.bytesFromInt16(headerLength, endian: endian));
// write the length of a record
// buffer.putShort((short) recordLength);
buffer.addAll(
ByteConversionUtilities.bytesFromInt16(recordLength, endian: endian));
// // write the reserved bytes in the header
// for (int i=0; i<20; i++) out.writeByteLE(0);
// buffer.position(buffer.position() + 20);
for (var i = 0; i < 20; i++) {
buffer.add(0);
}
// write all of the header records
int tempOffset = 0;
for (int i = 0; i < fields.length; i++) {
// write the field name
var fn = fields[i].fieldName;
// if (fn.length < 11) {
// buffer.addAll(fn.codeUnits); // TODO CHARSET?
// } else {
// buffer.addAll(fn.substring(0, 11).codeUnits);
// }
for (int j = 0; j < 11; j++) {
if (fn.length > j) {
buffer.add(fn.codeUnitAt(j));
} else {
buffer.add(0);
}
}
// write the field type
buffer.add(fields[i].fieldType);
// // write the field data address, offset from the start of the
// record.
buffer.addAll(ByteConversionUtilities.bytesFromInt32(tempOffset));
tempOffset += fields[i].fieldLength;
// write the length of the field.
buffer.add(fields[i].fieldLength);
// write the decimal count.
buffer.add(fields[i].decimalCount);
// write the reserved bytes.
// for (in j=0; jj<14; j++) out.writeByteLE(0);
// buffer.position(buffer.position() + 14);
for (var i = 0; i < 14; i++) {
buffer.add(0);
}
}
// write the end of the field definitions marker
buffer.add(0x0D);
await out.put(buffer);
}