001: /*
002: * $RCSfile: StreamControl.java,v $
003: *
004: * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * - Redistribution of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * - Redistribution in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * Neither the name of Sun Microsystems, Inc. or the names of
019: * contributors may be used to endorse or promote products derived
020: * from this software without specific prior written permission.
021: *
022: * This software is provided "AS IS," without a warranty of any
023: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
024: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
025: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
026: * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
027: * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
028: * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
029: * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
030: * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
031: * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
032: * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
033: * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
034: * POSSIBILITY OF SUCH DAMAGES.
035: *
036: * You acknowledge that this software is not designed, licensed or
037: * intended for use in the design, construction, operation or
038: * maintenance of any nuclear facility.
039: *
040: * $Revision: 1.4 $
041: * $Date: 2007/02/09 17:20:29 $
042: * $State: Exp $
043: */
044:
045: package com.sun.j3d.utils.scenegraph.io.retained;
046:
047: import java.io.RandomAccessFile;
048: import java.io.IOException;
049: import java.io.DataOutput;
050: import java.io.DataInput;
051: import java.io.DataInputStream;
052: import java.io.DataOutputStream;
053: import java.util.HashMap;
054: import java.util.Iterator;
055:
056: import javax.media.j3d.VirtualUniverse;
057: import javax.media.j3d.BranchGroup;
058: import javax.media.j3d.SceneGraphObject;
059:
060: import com.sun.j3d.utils.scenegraph.io.state.javax.media.j3d.SceneGraphObjectState;
061: import com.sun.j3d.utils.scenegraph.io.state.javax.media.j3d.NodeComponentState;
062: import com.sun.j3d.utils.scenegraph.io.state.javax.media.j3d.BranchGroupState;
063: import com.sun.j3d.utils.scenegraph.io.UnsupportedUniverseException;
064:
065: /**
066: * Provides the infrastructure for ScenGraphStream Reader and Writer
067: */
068: public class StreamControl extends Controller {
069:
070: protected String FILE_IDENT = new String("j3dsf");
071:
072: private DataInputStream inputStream;
073: private DataOutputStream outputStream;
074:
075: public StreamControl(DataOutputStream out) {
076: super ();
077: outputStream = out;
078: symbolTable = new SymbolTable(this );
079: }
080:
081: public StreamControl(DataInputStream in) {
082: super ();
083: inputStream = in;
084: symbolTable = new SymbolTable(this );
085: }
086:
087: /**
088: * Prepare the Stream for writing, by sending header information
089: */
090: public void writeStreamHeader() throws IOException {
091: outputStream.writeUTF(FILE_IDENT);
092: outputStream.writeInt(outputFileVersion);
093: }
094:
095: public void readStreamHeader() throws IOException {
096: String ident = inputStream.readUTF();
097: if (ident.equals("demo_j3s"))
098: throw new IOException(
099: "Use Java 3D Fly Through I/O instead of Java 3D Scenegraph I/O");
100:
101: if (!ident.equals("j3dsf"))
102: throw new IOException(
103: "This is a File - use SceneGraphFileReader instead");
104:
105: currentFileVersion = inputStream.readInt();
106:
107: if (currentFileVersion > outputFileVersion) {
108: throw new IOException(
109: "Unsupported file version. This file was written using a new version of the SceneGraph IO API, please update your installtion to the latest version");
110: }
111: }
112:
113: /**
114: * Add the named objects to the symbol table
115: */
116: public void addNamedObjects(HashMap namedObjects) {
117: symbolTable.addNamedObjects(namedObjects);
118: }
119:
120: /**
121: * The BranchGraph userData is not supported in a stream and will be
122: * ignored.
123: *
124: * However the data in the userData field of the BranchGroup will be
125: * stored in the stream
126: */
127: public void writeBranchGraph(BranchGroup bg,
128: java.io.Serializable userData) throws IOException {
129: try {
130: SymbolTableData symbol = symbolTable.getSymbol(bg);
131:
132: if (symbol == null) {
133: symbol = symbolTable.createSymbol(bg);
134: symbol.branchGraphID = -1; // This is a new BranchGraph so set the ID to -1
135: } // which will cause setBranchGraphRoot to assign a new ID.
136:
137: symbolTable.setBranchGraphRoot(symbol, 0);
138: symbolTable.startUnsavedNodeComponentFrame();
139: SceneGraphObjectState state = createState(bg, symbol);
140: writeObject(outputStream, state);
141: writeNodeComponents(outputStream);
142: symbolTable.endUnsavedNodeComponentFrame();
143:
144: if (symbolTable
145: .branchGraphHasDependencies(symbol.branchGraphID))
146: throw new javax.media.j3d.DanglingReferenceException();
147:
148: symbolTable.clearUnshared();
149: symbolTable.writeTable(outputStream);
150: } catch (SGIORuntimeException e) {
151: throw new IOException(e.getMessage());
152: }
153: }
154:
155: public BranchGroup readBranchGraph(HashMap namedObjects)
156: throws IOException {
157: try {
158: SceneGraphObjectState state = readObject(inputStream);
159: readNodeComponents(inputStream);
160: symbolTable.readTable(inputStream, true);
161:
162: symbolTable.setBranchGraphRoot(state.getSymbol(), 0);
163:
164: state.buildGraph();
165:
166: if (namedObjects != null)
167: symbolTable.getNamedObjectMap(namedObjects);
168:
169: return (BranchGroup) state.getNode();
170: } catch (SGIORuntimeException e) {
171: throw new IOException(e.getMessage());
172: }
173: }
174:
175: /**
176: * Read the set of branchgraps.
177: *
178: * Used by readUniverse
179: *
180: * RandomAccessFileControl will read the graphs in the array,
181: * StreamControl expects the graphs to follow the universe in the
182: * stream so it will read graphs.length branchgraphs.
183: */
184: protected void readBranchGraphs(int[] graphs) throws IOException {
185: for (int i = 0; i < graphs.length; i++)
186: readBranchGraph(null);
187: }
188:
189: /**
190: * Used by SymbolTable to load a node component that is not in current
191: * graph
192: */
193: public void loadNodeComponent(SymbolTableData symbol)
194: throws IOException {
195: throw new java.io.IOException(
196: "Unable to load individual NodeComponents from Stream");
197: }
198:
199: public void close() throws IOException {
200: super .reset();
201: }
202:
203: /**
204: * Implementation of abstract method from Controller.
205: *
206: * Always returns 0
207: */
208: public long getFilePointer() {
209: return 0L;
210: }
211:
212: }
|