📄 ddfmodule.java
字号:
/****************************************************************************** * Copyright (c) 1999, Frank Warmerdam * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. ******************************************************************************/package com.bbn.openmap.dataAccess.iso8211;import com.bbn.openmap.io.BinaryBufferedFile;import com.bbn.openmap.io.BinaryFile;import com.bbn.openmap.util.Debug;import java.io.IOException;import java.util.Iterator;import java.util.Vector;/** * The class that represents a ISO 8211 file. */public class DDFModule implements DDFConstants { protected BinaryFile fpDDF; protected String fileName; protected long nFirstRecordOffset; protected byte _interchangeLevel; protected byte _inlineCodeExtensionIndicator; protected byte _versionNumber; protected byte _appIndicator; protected int _fieldControlLength; protected String _extendedCharSet; // 4 characters protected int _recLength; protected byte _leaderIden; protected int _fieldAreaStart; protected int _sizeFieldLength; protected int _sizeFieldPos; protected int _sizeFieldTag; protected Vector paoFieldDefns; //DDFFieldDefinitions protected DDFRecord poRecord; /** * The constructor. Need to call open() if this constuctor is * used. */ public DDFModule() { paoFieldDefns = null; poRecord = null; fpDDF = null; } public DDFModule(String ddfName) throws IOException { open(ddfName); } /** * Close an ISO 8211 file. Just close the file pointer to the * file. */ public void close() { if (fpDDF != null) { try { fpDDF.close(); } catch (IOException ioe) { Debug.error("DDFModule IOException when closing DDFModule file"); } fpDDF = null; } } /** * Clean up, get rid of data and close file pointer. */ public void destroy() { close(); // Cleanup the working record. poRecord = null; // Cleanup the field definitions. paoFieldDefns = null; } /** * 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. * * @param pszFilename The name of the file to open. */ public BinaryFile open(String pszFilename) throws IOException { fileName = pszFilename; fpDDF = new BinaryBufferedFile(pszFilename); // Read the 24 byte leader. 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; } // Verify that this appears to be a valid DDF file. 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; } // Extract information from leader. 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 the header is invalid, then clean up, report the error // and return. 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"); } /* -------------------------------------------------------------------- */ /* Read the whole record into memory. */ /* -------------------------------------------------------------------- */ 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; } /* First make a pass counting the directory entries. */ 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++; } /* Allocate, and read field definitions. */ 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,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -