com.sun.j3d.utils.scenegraph.io.retained.RandomAccessFileControl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java3d-core Show documentation
Show all versions of java3d-core Show documentation
Java3D Core And Java3D Util Libraries
The newest version!
/*
* 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.
*
*/
package com.sun.j3d.utils.scenegraph.io.retained;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.io.RandomAccessFile;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.CapabilityNotSetException;
import com.sun.j3d.utils.scenegraph.io.UnsupportedUniverseException;
import com.sun.j3d.utils.scenegraph.io.state.javax.media.j3d.BranchGroupState;
import com.sun.j3d.utils.scenegraph.io.state.javax.media.j3d.SceneGraphObjectState;
import com.sun.j3d.utils.universe.ConfiguredUniverse;
import com.sun.j3d.utils.universe.SimpleUniverse;
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
*/
@Override
protected void readBranchGraphs( int[] graphs ) throws IOException {
for(int i=0; i