public BinaryFile open(String pszFilename) throws IOException {
  fileName=pszFilename;
  fpDDF=new BinaryBufferedFile(pszFilename);
  byte[] achLeader=new byte[DDF_LEADER_SIZE];
  if (fpDDF.read(achLeader) != DDF_LEADER_SIZE) {
    destroy();
    if (Debug.debugging("iso8211")) {
      Debug.output("DDFModule: Leader is short on DDF file " + pszFilename);
    }
    return null;
  }
  int i;
  boolean bValid=true;
  for (i=0; i < (int)DDF_LEADER_SIZE; i++) {
    if (achLeader[i] < 32 || achLeader[i] > 126) {
      bValid=false;
    }
  }
  if (achLeader[5] != '1' && achLeader[5] != '2' && achLeader[5] != '3') {
    bValid=false;
  }
  if (achLeader[6] != 'L') {
    bValid=false;
  }
  if (achLeader[8] != '1' && achLeader[8] != ' ') {
    bValid=false;
  }
  if (bValid) {
    _recLength=Integer.parseInt(new String(achLeader,0,5));
    _interchangeLevel=achLeader[5];
    _leaderIden=achLeader[6];
    _inlineCodeExtensionIndicator=achLeader[7];
    _versionNumber=achLeader[8];
    _appIndicator=achLeader[9];
    _fieldControlLength=Integer.parseInt(new String(achLeader,10,2));
    _fieldAreaStart=Integer.parseInt(new String(achLeader,12,5));
    _extendedCharSet=new String((char)achLeader[17] + "" + (char)achLeader[18]+ ""+ (char)achLeader[19]);
    _sizeFieldLength=Integer.parseInt(new String(achLeader,20,1));
    _sizeFieldPos=Integer.parseInt(new String(achLeader,21,1));
    _sizeFieldTag=Integer.parseInt(new String(achLeader,23,1));
    if (_recLength < 12 || _fieldControlLength == 0 || _fieldAreaStart < 24 || _sizeFieldLength == 0 || _sizeFieldPos == 0 || _sizeFieldTag == 0) {
      bValid=false;
    }
    if (Debug.debugging("iso8211")) {
      Debug.output("bValid = " + bValid + ", from "+ new String(achLeader));
      Debug.output(toString());
    }
  }
  if (!bValid) {
    destroy();
    if (Debug.debugging("iso8211")) {
      Debug.error("DDFModule: File " + pszFilename + " does not appear to have a valid ISO 8211 header.");
    }
    return null;
  }
  if (Debug.debugging("iso8211")) {
    Debug.output("DDFModule:  header parsed successfully");
  }
  byte[] pachRecord=new byte[_recLength];
  System.arraycopy(achLeader,0,pachRecord,0,achLeader.length);
  int numNewRead=pachRecord.length - achLeader.length;
  if (fpDDF.read(pachRecord,achLeader.length,numNewRead) != numNewRead) {
    if (Debug.debugging("iso8211")) {
      Debug.error("DDFModule: Header record is short on DDF file " + pszFilename);
    }
    return null;
  }
  int nFieldEntryWidth=_sizeFieldLength + _sizeFieldPos + _sizeFieldTag;
  int nFieldDefnCount=0;
  for (i=DDF_LEADER_SIZE; i < _recLength; i+=nFieldEntryWidth) {
    if (pachRecord[i] == DDF_FIELD_TERMINATOR)     break;
    nFieldDefnCount++;
  }
  paoFieldDefns=new Vector();
  for (i=0; i < nFieldDefnCount; i++) {
    if (Debug.debugging("iso8211")) {
      Debug.output("DDFModule.open: Reading field " + i);
    }
    byte[] szTag=new byte[128];
    int nEntryOffset=DDF_LEADER_SIZE + i * nFieldEntryWidth;
    int nFieldLength, nFieldPos;
    System.arraycopy(pachRecord,nEntryOffset,szTag,0,_sizeFieldTag);
    nEntryOffset+=_sizeFieldTag;
    nFieldLength=Integer.parseInt(new String(pachRecord,nEntryOffset,_sizeFieldLength));
    nEntryOffset+=_sizeFieldLength;
    nFieldPos=Integer.parseInt(new String(pachRecord,nEntryOffset,_sizeFieldPos));
    byte[] subPachRecord=new byte[nFieldLength];
    System.arraycopy(pachRecord,_fieldAreaStart + nFieldPos,subPachRecord,0,nFieldLength);
    paoFieldDefns.add(new DDFFieldDefinition(this,new String(szTag,0,_sizeFieldTag),subPachRecord));
  }
  achLeader=null;
  pachRecord=null;
  nFirstRecordOffset=fpDDF.getFilePointer();
  return fpDDF;
}

public BinaryFile open ( String pszFilename ) throws IOException { fileName = pszFilename ; fpDDF = new BinaryBufferedFile ( pszFilename ) ; byte [ ] achLeader = new byte [ DDF_LEADER_SIZE ] ; if ( fpDDF . read ( achLeader ) != DDF_LEADER_SIZE ) { destroy ( ) ; if ( Debug . debugging ( "iso8211" ) ) { Debug . output ( "DDFModule: Leader is short on DDF file " + pszFilename ) ; } return null ; } int i ; boolean bValid = true ; for ( i = 0 ; i < ( int ) DDF_LEADER_SIZE ; i ++ ) { if ( achLeader [ i ] < 32 || achLeader [ i ] > 126 ) { bValid = false ; } } if ( achLeader [ 5 ] != '1' && achLeader [ 5 ] != '2' && achLeader [ 5 ] != '3' ) { bValid = false ; } if ( achLeader [ 6 ] != 'L' ) { bValid = false ; } if ( achLeader [ 8 ] != '1' && achLeader [ 8 ] != ' ' ) { bValid = false ; } if ( bValid ) { _recLength = Integer . parseInt ( new String ( achLeader , 0 , 5 ) ) ; _interchangeLevel = achLeader [ 5 ] ; _leaderIden = achLeader [ 6 ] ; _inlineCodeExtensionIndicator = achLeader [ 7 ] ; _versionNumber = achLeader [ 8 ] ; _appIndicator = achLeader [ 9 ] ; _fieldControlLength = Integer . parseInt ( new String ( achLeader , 10 , 2 ) ) ; _fieldAreaStart = Integer . parseInt ( new String ( achLeader , 12 , 5 ) ) ; _extendedCharSet = new String ( ( char ) achLeader [ 17 ] + "" + ( char ) achLeader [ 18 ] + "" + ( char ) achLeader [ 19 ] ) ; _sizeFieldLength = Integer . parseInt ( new String ( achLeader , 20 , 1 ) ) ; _sizeFieldPos = Integer . parseInt ( new String ( achLeader , 21 , 1 ) ) ; _sizeFieldTag = Integer . parseInt ( new String ( achLeader , 23 , 1 ) ) ; if ( _recLength < 12 || _fieldControlLength == 0 || _fieldAreaStart < 24 || _sizeFieldLength == 0 || _sizeFieldPos == 0 || _sizeFieldTag == 0 ) { bValid = false ; } if ( Debug . debugging ( "iso8211" ) ) { Debug . output ( "bValid = " + bValid + ", from " + new String ( achLeader ) ) ; Debug . output ( toString ( ) ) ; } } if ( ! bValid ) { destroy ( ) ; if ( Debug . debugging ( "iso8211" ) ) { Debug . error ( "DDFModule: File " + pszFilename + " does not appear to have a valid ISO 8211 header." ) ; } return null ; } if ( Debug . debugging ( "iso8211" ) ) { Debug . output ( "DDFModule:  header parsed successfully" ) ; } byte [ ] pachRecord = new byte [ _recLength ] ; System . arraycopy ( achLeader , 0 , pachRecord , 0 , achLeader . length ) ; int numNewRead = pachRecord . length - achLeader . length ; if ( fpDDF . read ( pachRecord , achLeader . length , numNewRead ) != numNewRead ) { if ( Debug . debugging ( "iso8211" ) ) { Debug . error ( "DDFModule: Header record is short on DDF file " + pszFilename ) ; } return null ; } int nFieldEntryWidth = _sizeFieldLength + _sizeFieldPos + _sizeFieldTag ; int nFieldDefnCount = 0 ; for ( i = DDF_LEADER_SIZE ; i < _recLength ; i += nFieldEntryWidth ) { if ( pachRecord [ i ] == DDF_FIELD_TERMINATOR ) break ; nFieldDefnCount ++ ; } paoFieldDefns = new Vector ( ) ; for ( i = 0 ; i < nFieldDefnCount ; i ++ ) { if ( Debug . debugging ( "iso8211" ) ) { Debug . output ( "DDFModule.open: Reading field " + i ) ; } byte [ ] szTag = new byte [ 128 ] ; int nEntryOffset = DDF_LEADER_SIZE + i * nFieldEntryWidth ; int nFieldLength , nFieldPos ; System . arraycopy ( pachRecord , nEntryOffset , szTag , 0 , _sizeFieldTag ) ; nEntryOffset += _sizeFieldTag ; nFieldLength = Integer . parseInt ( new String ( pachRecord , nEntryOffset , _sizeFieldLength ) ) ; nEntryOffset += _sizeFieldLength ; nFieldPos = Integer . parseInt ( new String ( pachRecord , nEntryOffset , _sizeFieldPos ) ) ; byte [ ] subPachRecord = new byte [ nFieldLength ] ; System . arraycopy ( pachRecord , _fieldAreaStart + nFieldPos , subPachRecord , 0 , nFieldLength ) ; paoFieldDefns . add ( new DDFFieldDefinition ( this , new String ( szTag , 0 , _sizeFieldTag ) , subPachRecord ) ) ; } achLeader = null ; pachRecord = null ; nFirstRecordOffset = fpDDF . getFilePointer ( ) ; return fpDDF ; }
open a iso 8211 ( ddf ) file for reading , and read the ddr record to build the field definitions . if the open succeeds the data descriptive record ( ddr ) will have been read , and all the field and subfield definitions will be available .
