📄 randomaccessfilecontrol.java
字号:
for( int i=0; i<states.length; i++) { if (!states[i].getSymbol().graphBuilt) { states[i].buildGraph(); states[i].getSymbol().graphBuilt = true; } ret[i] = (BranchGroup)states[i].getNode(); } symbolTable.clearUnshared(); // Remove all unshared symbols return ret; } catch( SGIORuntimeException e ) { throw new IOException( e.getMessage() ); } } /** * Read and return all the graphs in the file */ public BranchGroup[] readAllBranchGraphs() throws IOException { int size = getBranchGraphCount(); BranchGroupState[] states = new BranchGroupState[ size ]; BranchGroup[] ret = new BranchGroup[ size ]; try { for( int i=0; i<size; i++) { states[i] = readSingleBranchGraph( i ); } for( int i=0; i<states.length; i++) { if (!states[i].getSymbol().graphBuilt) { states[i].buildGraph(); states[i].getSymbol().graphBuilt = true; } ret[i] = (BranchGroup)states[i].getNode(); } symbolTable.clearUnshared(); // Remove all unshared symbols } catch( SGIORuntimeException e ) { throw new IOException( e.getMessage() ); } return ret; } /** * Read the specified branchgraph but do NOT call buildGraph */ private BranchGroupState readSingleBranchGraph( int graphID ) throws IOException { SymbolTableData symbol = symbolTable.getBranchGraphRoot( graphID ); if (symbol.nodeState!=null) { return (BranchGroupState)symbol.nodeState; } raf.seek( symbolTable.getBranchGraphFilePosition( graphID ) ); return readNextBranchGraph(); } /** * Read the next userData and BranchGraph structure in the file * at the current position */ private BranchGroupState readNextBranchGraph() throws IOException { int nodeCount = raf.readInt(); skipUserData( raf ); BranchGroupState state=null; try { state = (BranchGroupState)readObject( raf ); readNodeComponents( raf ); } catch( IOException e ) { e.printStackTrace(); } return state; } public Object readBranchGraphUserData( int graphID ) throws IOException { try { raf.seek( symbolTable.getBranchGraphFilePosition( graphID ) ); int nodeCount = raf.readInt(); return readSerializedData( raf ); } catch( SGIORuntimeException e ) { throw new IOException( e.getMessage() ); } } /** * Write all the unsaved NodeComponents and SharedGroups to DataOutput. * Mark all the NodeComponents as saved. */ protected void writeNodeComponents( DataOutput out ) throws IOException { // The RandomAccessFileControl version sets throws the pointer to // the next NodeComponent correctly long ptrLoc=0L; java.util.ListIterator list = symbolTable.getUnsavedNodeComponents(); out.writeInt( symbolTable.getUnsavedNodeComponentsSize() ); while( list.hasNext() ) { SymbolTableData symbol = (SymbolTableData)list.next(); out.writeInt( symbol.nodeID ); ptrLoc = raf.getFilePointer(); out.writeLong( 0L ); // Pointer to next NodeComponent writeObject( out, symbol.getNodeState() ); long ptr = raf.getFilePointer(); raf.seek( ptrLoc ); out.writeLong( ptr ); raf.seek( ptr ); } } /** * Read in all the node components in this block */ protected void readNodeComponents( DataInput in ) throws IOException { int count = in.readInt(); for(int i=0; i<count; i++) { int nodeID = in.readInt(); long nextNC = in.readLong(); if (symbolTable.isLoaded( nodeID )) { // Skip this object raf.seek( nextNC ); } else { // Reading the objects will register them in the symbol table SceneGraphObjectState nodeComponent = readObject( in ); } } } //static java.util.LinkedList objSizeTracker = new java.util.LinkedList(); public void writeObject( DataOutput out, SceneGraphObjectState obj ) throws IOException { symbolTable.setFilePosition( raf.getFilePointer(), obj ); try { // These commented out lines will display the size of each object // as it's written to the file //long start = raf.getFilePointer(); //int childStart = objSizeTracker.size(); super.writeObject( out, obj ); //long size = raf.getFilePointer()-start; //while( childStart!=objSizeTracker.size() ) // size -= ((Long)objSizeTracker.removeLast()).longValue(); //String name = obj.getClass().getName(); //System.out.println( name.substring( name.lastIndexOf('.')+1, name.length() )+" size "+size); //objSizeTracker.addLast( new Long( size )); } catch( SGIORuntimeException e ) { throw new IOException( e.getMessage() ); }} public String readFileDescription() throws IOException { raf.seek( FILE_DESCRIPTION ); String ret = raf.readUTF(); user_data = raf.getFilePointer(); return ret; } /** * Used by SymbolTable to load a node component that is not in current * graph */ public void loadNodeComponent( SymbolTableData symbol ) throws IOException { try { raf.seek( symbol.filePosition ); readObject( raf ); } catch( SGIORuntimeException e ) { throw new IOException( e.getMessage() ); } } /** * Loads the specified SharedGroup */ public void loadSharedGroup( SymbolTableData symbol ) throws IOException { try { raf.seek( symbol.filePosition ); readObject( raf ); } catch( SGIORuntimeException e ) { throw new IOException( e.getMessage() ); } } public void close() throws IOException { try { if (writeMode) writeClose(); //System.out.println("File size at close "+raf.length() ); raf.close(); super.reset(); } catch( SGIORuntimeException e ) { throw new IOException( e.getMessage() ); } } /** * Write all the pointers etc */ private void writeClose() throws IOException { symbol_table = raf.getFilePointer(); super.getSymbolTable().writeTable( raf ); //System.out.println("Symbol table size "+(raf.getFilePointer()-symbol_table)); raf.seek( UNIVERSE_CONFIG_PTR ); raf.writeLong( universe_config ); raf.seek( SYMBOL_TABLE_PTR ); raf.writeLong( symbol_table ); raf.seek( BRANCH_GRAPH_COUNT ); raf.writeInt( symbolTable.getBranchGraphCount() ); } public long getFilePointer() { try { return raf.getFilePointer(); } catch(IOException e ) {} return 0; } /** * Given a branchgraph, return the corresponding index of the graph * in the file. Returns -1 if graph not found. */ public int getBranchGraphPosition( BranchGroup graph ) { SymbolTableData symbol = symbolTable.getSymbol( graph ); if ( symbol!=null ) return symbol.branchGraphID; return -1; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -