📄 dcwrecordfile.java
字号:
public void parseSomeRowsAndPrint() throws FormatException { int row_id_column = whatColumn(ID_COLUMN_NAME); int rowcount = getRecordCount(); for (int i = 1; i <= rowcount; i++) { if ((i > 10) && ((i % 100) != 0) && (i != rowcount)) { continue; } seekToRow(i); List l = parseRow(); int cnt = ((Integer) (l.get(row_id_column))).intValue(); if (cnt != i) { System.out.println("Possible incorrect seek for row number " + i + " got " + cnt); } System.out.println(VPFUtil.listToString(l)); } } /** * Return a row from the table. repeatedly calling parseRow gets * consecutive rows. * * @return a List of fields read from the table * @exception FormatException an error was encountered reading the * row */ public List parseRow() throws FormatException { List retval = new ArrayList(getColumnCount()); return parseRow(retval) ? retval : null; } /** * Return a row from the table. repeatedly calling parseRow gets * consecutive rows. * * @param retval append the fields from a row in the table. * clear() is called before any real work is done. * @return true is we read a row, false if no more rows are * available * @exception FormatException an error was encountered reading the * row * @see java.util.List#clear() */ public synchronized boolean parseRow(List retval) throws FormatException { retval.clear(); try { for (int i = 0; i < columnInfo.length; i++) { Object newobj = columnInfo[i].parseField(inputFile); retval.add(newobj); } cursorRow++; return true; } catch (FormatException f) { throw new FormatException("DcwRecordFile: parserow on table " + filename + ": " + f.getMessage()); } catch (EOFException e) { if (retval.size() > 0) { throw new FormatException("DcwRecordFile: hit EOF when list = " + VPFUtil.listToString(retval)); } try { if (inputFile.available() > 0) { throw new FormatException("DcwRecordFile: hit EOF with available = " + inputFile.available() + " when list = " + VPFUtil.listToString(retval)); } } catch (IOException i) { throw new FormatException("IOException calling available()"); } return false; } } /** * Returns the documentation file associated with this table. * * @return the doc file - may be null */ public String getDocumentationFilename() { return documentationFileName; } /** * Returns the table description for this table. * * @return the table description - may be null */ public String getDescription() { return tableDescription; } /** * get the length of a single record * * @return -1 indicates a variably sized record */ public int getRecordLength() { return recordLength; } /** * Gets the number of records in the table. * * @return the number of records * @exception FormatException some problem was encountered dealing * with the file */ public int getRecordCount() throws FormatException { try { if (recordLength == -1) { return vli().getRecordCount(); } else { return (int) (inputFile.length() - headerLength) / recordLength; } } catch (IOException i) { System.out.println("RecordCount: io exception " + i.getMessage()); } catch (NullPointerException npe) { } return -1; } final private DcwVariableLengthIndexFile vli() throws FormatException, IOException { if (vli == null) { openVLI(); } return vli; } /** * Opens the associated variable length index for the file * * @exception FormatException an error. */ private void openVLI() throws FormatException, IOException { String realfname = filename.toString(); boolean endwithdot = realfname.endsWith("."); String fopen; if (endwithdot) { StringBuffer newf = new StringBuffer(realfname.substring(0, realfname.length() - 2)); fopen = newf.append("x.").toString(); } else { StringBuffer newf = new StringBuffer(realfname.substring(0, realfname.length() - 1)); fopen = newf.append("x").toString(); } vli = new DcwVariableLengthIndexFile(new BinaryBufferedFile(fopen), byteorder); } /** * Parses the row specified by rownumber * * @param rownumber the number of the row to return * [1..recordCount] * @return the values contained in the row * @exception FormatException data format errors */ public List getRow(int rownumber) throws FormatException { List l = new ArrayList(getColumnCount()); return getRow(l, rownumber) ? l : null; } /** * Parses the row specified by rownumber * * @param rownumber the number of the row to return * [1..recordCount] * @param retval values contained in the row * @exception FormatException data format errors * @see #parseRow() */ public synchronized boolean getRow(List retval, int rownumber) throws FormatException { if (inputFile == null) { reopen(rownumber); } else { seekToRow(rownumber); } return parseRow(retval); } /** * moves the input cursor to the specified row [affects subsequent * calls parseRow.] * * @param recordNumber the number of the row to seek to * @exception FormatException data format errors * @exception IllegalArgumentException recordNumber less than 1 */ public synchronized void seekToRow(int recordNumber) throws FormatException { if (recordNumber <= 0) { throw new IllegalArgumentException("DcwRecordFile: seekToRow(" + recordNumber + "," + getRecordCount() + "," + filename + ")"); } if (recordNumber == cursorRow) { return; } cursorRow = recordNumber; int offset = 0; try { if ((recordLength == -1) && (recordNumber != 1)) { offset = vli().recordOffset(recordNumber); } else { offset = (recordLength * (recordNumber - 1)) + headerLength; } inputFile.seek(offset); } catch (IOException io) { throw new FormatException("SeekToRow IOException " + io.getMessage() + " offset: " + offset + " " + tablename + " " + filename); } } /** * Returns the index into columnInfo of the column with the * specified name * * @param columnname the column name to match * @return an index into columnInfo (-1 indicates no such column) */ public int whatColumn(String columnname) { for (int i = 0; i < columnInfo.length; i++) { if (columnInfo[i].getColumnName().equals(columnname)) { return i; } } return -1; } /** * Returns the name of a column * * @param index the column to get the name for * @return the columnName */ public String getColumnName(int index) { return columnInfo[index].getColumnName(); } /** * Prints the table information to System.out. * * @exception FormatException some problem was encountered dealing * with the file */ public void printSchema() throws FormatException { System.out.println("File Name: " + filename + "\nTable name: " + tablename + "\nTable Description: " + tableDescription + "\nDocumentation File Name: " + documentationFileName + "\nRecord Length: " + recordLength + " Record Count: " + getRecordCount()); for (int i = 0; i < columnInfo.length; i++) { System.out.print("Column " + i + " " + columnInfo[i].toString() + "\n"); } System.out.flush(); } /** Closes the associated input file. (may later get reopened) */ public synchronized void close() { cursorRow = -1; try { if (inputFile != null) { inputFile.close(); } inputFile = null; } catch (IOException i) { System.out.println("Caught ioexception " + i.getMessage()); } } /** * Reopen the associated input file. * * @param seekRow the row to seek to upon reopening the file. If * seekRow is invalid (less than 1), then the input stream * is in an undefined location, and seekToRow (or * getRow(int)) must be called before parseRow * @exception FormatException some error was encountered in * reopening file or seeking to the desired row. * @see #parseRow() * @see #getRow(int) * @see #close() */ public synchronized void reopen(int seekRow) throws FormatException { try { if (inputFile == null) { inputFile = new BinaryBufferedFile(filename); inputFile.byteOrder(byteorder); } if (seekRow > 0) { seekToRow(seekRow); } } catch (IOException i) { throw new FormatException(i.getClass() + ": " + i.getMessage()); } } /** * Returns the number of columns this table has */ final public int getColumnCount() { return columnInfo.length; } /** * Return the column info for this table. * <p> * NOTE: modifying this array is likely to cause problems... */ final public DcwColumnInfo[] getColumnInfo() { return columnInfo; } /** releases associated resources */ public void finalize() { close(); } /** * An test main for parsing VPF table files. * * @param args file names to be read */ public static void main(String args[]) { for (int i = 0; i < args.length; i++) { System.out.println(args[i]); try { DcwRecordFile foo = new DcwRecordFile(args[i]); foo.printSchema(); foo.close(); foo.reopen(1); for (List l = new ArrayList(); foo.parseRow(l);) { System.out.println(VPFUtil.listToString(l)); } foo.close(); } catch (Exception e) { e.printStackTrace(); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -