📄 dcwrecordfile.java
字号:
// **********************************************************************// // <copyright>// // BBN Technologies// 10 Moulton Street// Cambridge, MA 02138// (617) 873-8000// // Copyright (C) BBNT Solutions LLC. All rights reserved.// // </copyright>// **********************************************************************// $Source: /cvs/distapps/openmap/src/openmap/com/bbn/openmap/layer/vpf/DcwRecordFile.java,v $// $Revision: 1.4.2.2 $ $Date: 2005/01/10 16:39:39 $ $Author: dietrick $// **********************************************************************package com.bbn.openmap.layer.vpf;import java.io.EOFException;import java.io.IOException;import java.util.ArrayList;import java.util.List;import com.bbn.openmap.MoreMath;import com.bbn.openmap.io.BinaryBufferedFile;import com.bbn.openmap.io.BinaryFile;import com.bbn.openmap.io.FormatException;/** * Read and encapsulate VPF table files. */public class DcwRecordFile { /** input is read from this file */ protected BinaryFile inputFile = null; /** the description of the table [read from the file] */ protected String tableDescription = null; /** the name of another table that describes what this one is for */ protected String documentationFileName = null; /** number of bytes consumed by the table header */ private int headerLength = 4; //for the 4 bytes of the // headerlength field /** * big-endian (<code>true</code>) or little-endian ( * <code>false</code>) */ protected boolean MSBFirst = false; /** ordered set of columns (read from table header) */ protected DcwColumnInfo[] columnInfo = null; /** * length of a record (<code>-1</code> indicates * variable-length record) */ protected int recordLength = 0; /** * for tables with variable-length records, the corresponding * variable-length index */ protected DcwVariableLengthIndexFile vli = null; /** the name of the file */ final protected String filename; /** the name of the table */ protected String tablename = null; /** * remember the byte order for later file openings, true for MSB * first */ protected boolean byteorder = true; /** the record number that a call to parseRow() will return */ int cursorRow = -1; /** the name of the row identifier column "id" */ public static final String ID_COLUMN_NAME = "id"; /** * Open a DcwRecordFile and completely initialize it * * @param name the name of the file to use for input * @exception FormatException some problem was encountered dealing * with the file */ public DcwRecordFile(String name) throws FormatException { this(name, false); } /** * Open a DcwRecordFile * * @param name the name of the file to use for input * @param deferInit if <code>true</code>, don't actually open * files and initialize the object. In this state, the only * method that should be called is finishInitialization. * @exception FormatException some problem was encountered dealing * with the file * @see #finishInitialization() */ public DcwRecordFile(String name, boolean deferInit) throws FormatException { this.filename = name; if (!deferInit) { finishInitialization(); } } /** * Strip the tablename out of the filename. Strips both path * information and the trailing '.', if it exists. */ private void internTableName() { int strlen = filename.length(); int firstchar = filename.lastIndexOf('/'); int lastchar = filename.endsWith(".") ? strlen - 1 : strlen; tablename = filename.substring(firstchar + 1, lastchar) .toLowerCase() .intern(); } /** * Returns the File this instance is using * * @return the File being read */ public String getTableFile() { return filename; } /** * return the name of the table */ public String getTableName() { return tablename; } /** * Complete initialization of this object. This function should * only be called once, and only if the object was constructed * with defered initialization. * * @exception FormatException some problem was encountered dealing * with the file */ public synchronized void finishInitialization() throws FormatException { internTableName(); try { inputFile = new BinaryBufferedFile(filename); } catch (IOException e) { throw new FormatException(e.toString()); } try { byte preHeaderLen[] = inputFile.readBytes(4, false); char delim = inputFile.readChar(); switch (delim) { case 'L': case 'l': delim = inputFile.readChar(); //Intentional fall through to set byteorder case ';': //default is LSB first byteorder = false; inputFile.byteOrder(byteorder); break; case 'M': case 'm': //alternatively, it can be MSB first byteorder = true; inputFile.byteOrder(byteorder); delim = inputFile.readChar(); break; default: throw new FormatException("Invalid Byte Encoding Format"); } headerLength += MoreMath.BuildInteger(preHeaderLen, byteorder); if (delim != ';') {//Sanity check the input throw new FormatException("Unexpected character in header"); } tableDescription = inputFile.readToDelimiter(';'); documentationFileName = inputFile.readToDelimiter(';'); if ("-".equals(documentationFileName)) { documentationFileName = null; } ArrayList tmpcols = new ArrayList(); try { while (true) { DcwColumnInfo dci = new DcwColumnInfo(inputFile); int collen = dci.fieldLength(); if ((collen == -1) || (recordLength == -1)) { recordLength = -1; } else { recordLength += collen; } tmpcols.add(dci); } } catch (EOFException e) { } columnInfo = new DcwColumnInfo[tmpcols.size()]; tmpcols.toArray(columnInfo); cursorRow = 1; } catch (EOFException e) { throw new FormatException("Caught EOFException: " + e.getMessage()); } catch (NullPointerException npe) { } } /** * Returns a TilingAdapter for the selected column. * * @param primColumnName the name of the primitive column * @return an appropriate TilingAdapter instance or null */ public TilingAdapter getTilingAdapter(String primColumnName) { return getTilingAdapter(-1, whatColumn(primColumnName)); } /** * Returns a TilingAdapter for the selected column. * * @param primColumnName the name of the primitive column * @param tileColumnName the name of the tile_id column * @return an appropriate TilingAdapter instance or null */ public TilingAdapter getTilingAdapter(String tileColumnName, String primColumnName) { return getTilingAdapter(whatColumn(tileColumnName), whatColumn(primColumnName)); } /** * Returns a TilingAdapter for the selected column. * * @param primColumn the position of the primitive column * @param tileColumn the position of the tile_id column * @return an appropriate TilingAdapter instance or null */ public TilingAdapter getTilingAdapter(int tileColumn, int primColumn) { DcwColumnInfo tile = (tileColumn != -1) ? columnInfo[tileColumn] : null; if (primColumn == -1) { return null; } DcwColumnInfo prim = columnInfo[primColumn]; TilingAdapter retval = null; char primFieldType = prim.getFieldType(); if (tile == null) { if (primFieldType == 'K') { retval = new TilingAdapter.CrossTileAdapter(primColumn); } else if ((primFieldType == 'I') || (primFieldType == 'S')) { retval = new TilingAdapter.UntiledAdapter(primColumn); } } else { if (primFieldType == 'K') { //error??? duplicate tile data retval = new TilingAdapter.CrossTileAdapter(primColumn); } else if ((primFieldType == 'I') || (primFieldType == 'S')) { retval = new TilingAdapter.TiledAdapter(tileColumn, primColumn); } } return retval; } /** * Get the column number for a set of column names. * * @param names the names of the columns * @return an array of column numbers * @exception FormatException the table does not match the * specified schema */ public int[] lookupSchema(String[] names, boolean mustExist) throws FormatException { int retval[] = new int[names.length]; for (int i = 0; i < retval.length; i++) { retval[i] = whatColumn(names[i]); if ((retval[i] == -1) && mustExist) { throw new FormatException("Column " + names[i] + " doesn't exist"); } } return retval; } /** * Get the column number for a set of column names. * * @param names the names of the columns * @param type in same order as names * @param length in same order as names (-1 for a variable length * column) * @param strictlength false means that variable length columns * can be fixed-length instead * @param mustExist if true and a column doesn't exist, method * returns null * @return an array of column numbers * @exception FormatException the table does not match the * specified schema */ public int[] lookupSchema(String[] names, boolean mustExist, char type[], int length[], boolean strictlength) throws FormatException { int retval[] = lookupSchema(names, mustExist); if ((type.length == names.length) && (length.length == names.length)) { for (int i = 0; i < retval.length; i++) { if (retval[i] != -1) { columnInfo[retval[i]].assertSchema(type[i], length[i], strictlength); } } } return retval; } /** * Good for looking at the contents of a data file, this method * dumps a bunch of rows to System.out. It parses all the lines of * the file. * * @exception FormatException some kind of data format error was * encountered while parsing the file */ public void parseAllRowsAndPrintSome() throws FormatException { int row_id_column = whatColumn(ID_COLUMN_NAME); String vectorString = null; int rowcount = 0; for (List l = new ArrayList(getColumnCount()); parseRow(l);) { int cnt = ((Number) (l.get(row_id_column))).intValue(); if (cnt != ++rowcount) { System.out.println("Non-consecutive row number. Expected " + rowcount + " got " + cnt); } vectorString = VPFUtil.listToString(l); if ((rowcount < 20) || (rowcount % 100 == 0)) { System.out.println(vectorString); } } if (rowcount > 20) System.out.println(vectorString); } /** * Good for looking at the contents of a data file, this method * dumps a bunch of rows to System.out. (Using seekToRow to move * between records * * @exception FormatException some kind of data format error was * encountered while parsing the file */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -