📄 randomaccessfilecontrol.java
字号:
/* * $RCSfile: RandomAccessFileControl.java,v $ * * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistribution of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistribution in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * Neither the name of Sun Microsystems, Inc. or the names of * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * This software is provided "AS IS," without a warranty of any * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE * POSSIBILITY OF SUCH DAMAGES. * * You acknowledge that this software is not designed, licensed or * intended for use in the design, construction, operation or * maintenance of any nuclear facility. * * $Revision: 1.4 $ * $Date: 2007/02/09 17:20:29 $ * $State: Exp $ */package com.sun.j3d.utils.scenegraph.io.retained;import java.io.RandomAccessFile;import java.io.IOException;import java.io.DataOutput;import java.io.DataInput;import javax.media.j3d.BranchGroup;import javax.media.j3d.CapabilityNotSetException;import javax.media.j3d.Canvas3D;import com.sun.j3d.utils.scenegraph.io.state.javax.media.j3d.SceneGraphObjectState;import com.sun.j3d.utils.scenegraph.io.state.javax.media.j3d.NodeComponentState;import com.sun.j3d.utils.scenegraph.io.state.javax.media.j3d.BranchGroupState;import com.sun.j3d.utils.scenegraph.io.UnsupportedUniverseException;import com.sun.j3d.utils.universe.SimpleUniverse;import com.sun.j3d.utils.universe.ConfiguredUniverse;public class RandomAccessFileControl extends Controller { protected String FILE_IDENT = new String( "j3dff" ); private long user_data; private long universe_config; private long symbol_table; private RandomAccessFile raf; private int branchGraphCount=0; private boolean writeMode = false; private Object userData; /** Creates new RandomAccessFileControl */ public RandomAccessFileControl() { super(); symbolTable = new SymbolTable(this); } /** * Create the file and write the inital header information */ public void createFile( java.io.File file, SimpleUniverse universe, boolean writeUniverseContent, String description, java.io.Serializable userData ) throws IOException, UnsupportedUniverseException, CapabilityNotSetException { raf = new RandomAccessFile( file, "rw" ); writeMode = true; raf.seek(0); raf.writeUTF( FILE_IDENT ); raf.seek(20); raf.writeInt( outputFileVersion ); raf.seek( BRANCH_GRAPH_COUNT ); raf.writeInt( 0 ); // Place holder to branch graph count raf.seek( FILE_DESCRIPTION ); if (description==null) description=""; raf.writeUTF( description ); try { writeSerializedData( raf, userData ); universe_config = raf.getFilePointer(); writeUniverse( raf, universe, writeUniverseContent ); } catch( SGIORuntimeException e ) { throw new IOException( e.getMessage() ); } } /** * Open the file for reading */ public void openFile( java.io.File file ) throws IOException { raf = new RandomAccessFile( file, "r" ); writeMode = false; raf.seek(0); String ident = raf.readUTF(); if ( ident.equals("demo_j3f") ) throw new IOException( "Use Java 3D Fly Through I/O instead of Java 3D Scenegraph I/O" ); if ( !ident.equals("j3dff") ) throw new IOException( "This is a Stream - use SceneGraphStreamReader instead"); raf.seek(20); currentFileVersion = raf.readInt(); if ( currentFileVersion > outputFileVersion ) { throw new IOException("Unsupported file version. This file was written using a new version of the SceneGraph IO API, please update your installtion to the latest version"); } // readFileDescription sets user_data String description = readFileDescription(); raf.seek( BRANCH_GRAPH_COUNT ); branchGraphCount = raf.readInt(); //System.out.println("BranchGraph count : "+branchGraphCount ); raf.seek( UNIVERSE_CONFIG_PTR ); universe_config = raf.readLong(); raf.seek( SYMBOL_TABLE_PTR ); symbol_table = raf.readLong(); ConfiguredUniverse universe; raf.seek( symbol_table ); symbolTable.readTable( raf, false ); raf.seek(user_data); userData = readSerializedData(raf); } public ConfiguredUniverse readUniverse( boolean attachBranchGraphs, Canvas3D canvas) throws IOException { raf.seek( universe_config ); return readUniverse( raf, attachBranchGraphs, canvas ); } public Object getUserData() { return userData; } /** * Read the set of branchgraps. * * Used by readUniverse * * RandomAccessFileControl will read the graphs in the array, * StreamControl will read all graphs in the stream */ protected void readBranchGraphs( int[] graphs ) throws IOException { for(int i=0; i<graphs.length; i++) { readBranchGraph( graphs[i] ); } } /** * Return the number of branchgraphs in the file */ public int getBranchGraphCount() { return symbolTable.getBranchGraphCount(); } public void writeBranchGraph( BranchGroup bg, java.io.Serializable userData ) throws IOException { long filePointer = raf.getFilePointer(); raf.writeInt( 0 ); // Node count try { writeSerializedData( raf, userData ); // Size and byte[] //System.out.println("Actual Write at "+raf.getFilePointer() ); SymbolTableData symbol = symbolTable.getSymbol( bg ); if (symbol==null) { symbol = symbolTable.createSymbol( bg ); symbol.branchGraphID = -1; // This is a new BranchGraph so set the ID to -1 } // which will cause setBranchGraphRoot to assign a new ID. symbolTable.setBranchGraphRoot( symbol, filePointer ); symbolTable.startUnsavedNodeComponentFrame(); SceneGraphObjectState state = createState( bg, symbol ); //System.out.println(state); try { writeObject( raf, state ); writeNodeComponents( raf ); } catch( IOException e ) { e.printStackTrace(); } symbolTable.endUnsavedNodeComponentFrame(); } catch( SGIORuntimeException e ) { throw new IOException( e.getMessage() ); } } public BranchGroup[] readBranchGraph( int graphID ) throws IOException { //System.out.print("Loading graph "+graphID+" : "); // TODO - remove try { int[] dependencies = symbolTable.getBranchGraphDependencies(graphID); //System.out.println("Dependencies "); //for(int i=0; i<dependencies.length; i++) // System.out.print( dependencies[i]+" "); // TODO - remove //System.out.println(); BranchGroupState[] states = new BranchGroupState[ dependencies.length+1 ]; BranchGroup[] ret = new BranchGroup[ states.length ]; states[0] = readSingleBranchGraph( graphID ); for( int i=0; i<dependencies.length; i++) { states[i+1] = readSingleBranchGraph( dependencies[i] ); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -