示例#1
0
  /**
   * This method loads all of the fields in the mapTable argument via the dis stream, which is setup
   * at the proper location.
   *
   * @param dis Input stream reading from the .dbf file.
   * @param mapTable The dbf table being loaded.
   * @throws IOException Thrown when the stream fails.
   */
  private void loadFields(DataInputStream dis, DBFTable mapTable) throws IOException {
    // NUMBER OF FIELDS (COLUMNS) IN THE TABLE
    int numFields = (mapTable.getPositionOfFirstDataRecorded() - 32 - 2 + 1) / 32;

    for (int i = 0; i < numFields; i++) {
      // ANOTHER COLUMN
      DBFField fieldToAdd = new DBFField();
      mapTable.addField(fieldToAdd);

      // FIELD NAME
      String fieldName = "";
      for (int j = 0; j < 11; j++) {
        byte c = dis.readByte();
        if (c != 0) fieldName += (char) c;
      }
      fieldToAdd.setName(fieldName);

      // FIELD TYPE
      byte fieldTypeAsByte = dis.readByte();
      char fieldType = (char) fieldTypeAsByte;
      if (fieldType == 'C') fieldToAdd.setType(DBFFieldType.C);
      else fieldToAdd.setType(DBFFieldType.N);

      // DISPLACEMENT OF FIELD IN RECORD (12-15)
      int displacementOfFieldInRecord = readLittleEndianInt(dis);
      fieldToAdd.setDisplacement(displacementOfFieldInRecord);

      // LENGTH OF FIELD (16)
      int lengthOfField = dis.readByte();
      if (lengthOfField < 0) lengthOfField += 256;
      fieldToAdd.setLength(lengthOfField);

      // NUMBER OF DECIMAL PLACES (17)
      byte numberOfDecimalPlaces = dis.readByte();
      fieldToAdd.setNumberOfDecimalPlaces(numberOfDecimalPlaces);

      // FIELD FLAGS (18)
      byte fieldFlags = dis.readByte();
      fieldToAdd.setFlags(fieldFlags);

      // AUTOINCREMENT NEXT (19-22)
      int next = readLittleEndianInt(dis);
      fieldToAdd.setNext(next);

      // AUTOINCREMENT STEP (23)
      byte step = dis.readByte();
      fieldToAdd.setStep(step);

      // RESERVED (24-31) - WE WON'T USE THIS
      long reservedFieldData = dis.readLong();
      fieldToAdd.setReservedData(reservedFieldData);
    }
  }
示例#2
0
  /**
   * This helper method saves just the .dbf file header portion of the mapTable argument using the
   * dos stream.
   *
   * @param dos The stream writing to the .dbf file.
   * @param mapTable The dbf table being saved.
   * @throws IOException Thrown when the stream fails.
   */
  private void saveHeader(DataOutputStream dos, DBFTable mapTable) throws IOException {
    // DBF file type (0)
    dos.writeByte(mapTable.getFileType());

    // LAST UPDATE (1-3)
    byte year = (byte) (mapTable.getLastModifiedDate().get(Calendar.YEAR) - 1900);
    byte month = (byte) (mapTable.getLastModifiedDate().get(Calendar.MONTH) + 1);
    byte day = (byte) (mapTable.getLastModifiedDate().get(Calendar.DATE));
    dos.writeByte(year);
    dos.writeByte(month);
    dos.writeByte(day);

    // NUMBER OF RECORDS IN FILE (4-7)
    int numberOfRecordsInFile = Integer.reverseBytes(mapTable.getNumberOfRecords());
    dos.writeInt(numberOfRecordsInFile);

    // POSITION OF FIRST DATA RECORDED (8-9)
    short positionOfFirstDataRecorded =
        Short.reverseBytes(mapTable.getPositionOfFirstDataRecorded());
    dos.writeShort(positionOfFirstDataRecorded);

    // LENGTH OF ONE DATA RECORD, INCLUDING DELETE FLAG (10-11)
    short dataRecordLength = Short.reverseBytes(mapTable.getDataRecordLength());
    dos.writeShort(dataRecordLength);

    // ZEROES (12-13)
    short zeroes = Short.reverseBytes(mapTable.getZeroes());
    dos.writeShort(zeroes);

    // DBASE IV Transaction Flag (14)
    dos.writeByte(mapTable.getDbaseTransactionFlag());

    // DBASE IV Encryption Flag (15)
    dos.writeByte(mapTable.getDbaseEncryptionFlag());

    // Multiuser Processing 12 Bytes (16-27)
    int[] mup = mapTable.getMup();
    dos.writeInt(Integer.reverseBytes(mup[0]));
    dos.writeInt(Integer.reverseBytes(mup[1]));
    dos.writeInt(Integer.reverseBytes(mup[2]));

    // TABLE FLAGS (28)
    dos.writeByte(mapTable.getFlags());

    // CODE PAGE MARK/LANGUAGE DRIVER ID (29)
    dos.writeByte(mapTable.getCodePageMark());

    // RESERVED, CONTAINS 0x00 (30-31)
    dos.writeShort(Short.reverseBytes(mapTable.getReserved()));
  }
示例#3
0
  /**
   * This method saves all of the fields in the mapTable argument via the dos stream, which is setup
   * at the proper location.
   *
   * @param dos Output stream writing to the .dbf file.
   * @param mapTable The dbf table being saved.
   * @throws IOException Thrown when the stream fails.
   */
  private void saveFields(DataOutputStream dos, DBFTable mapTable) throws IOException {
    // NUMBER OF FIELDS (COLUMNS) IN THE TABLE
    int numFields = (mapTable.getPositionOfFirstDataRecorded() - 32 - 2 + 1) / 32;

    for (int i = 0; i < numFields; i++) {
      // HERE'S THE FIELD WE'RE SAVING
      DBFField fieldToSave = mapTable.getField(i);

      // FIELD NAME
      String fieldName = fieldToSave.getName();
      int j = 0;
      for (; j < fieldName.length(); j++) {
        byte c = (byte) fieldName.charAt(j);
        dos.writeByte(c);
      }
      byte z = (byte) 0x00;
      while (j < 11) {
        dos.writeByte(z);
        j++;
      }

      // FIELD TYPE
      dos.writeByte((byte) (fieldToSave.getType().toString().charAt(0)));

      // DISPLACEMENT OF FIELD IN RECORD (12-15)
      dos.writeInt(Integer.reverseBytes(fieldToSave.getDisplacement()));

      // LENGTH OF FIELD (16)
      dos.writeByte((byte) fieldToSave.getLength());

      // NUMBER OF DECIMAL PLACES (17)
      dos.writeByte(fieldToSave.getNumberOfDecimalPlaces());

      // FIELD FLAGS (18)
      dos.writeByte(fieldToSave.getFlags());

      // AUTOINCREMENT NEXT (19-22)
      dos.writeInt(Integer.reverseBytes(fieldToSave.getNext()));

      // AUTOINCREMENT STEP (23)
      dos.writeByte(fieldToSave.getStep());

      // RESERVED (24-31) - WE WON'T USE THIS
      dos.writeLong(fieldToSave.getReservedData());
    }
  }