001: /*
002: * $RCSfile: LWOBFileReader.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:07 $
042: * $State: Exp $
043: */
044:
045: package com.sun.j3d.loaders.lw3d;
046:
047: import java.io.File;
048: import java.io.FileNotFoundException;
049: import java.io.FileInputStream;
050: import java.io.BufferedInputStream;
051: import java.io.IOException;
052: import com.sun.j3d.loaders.ParsingErrorException;
053:
054: class LWOBFileReader extends BufferedInputStream {
055:
056: // Debugging constants
057: final static int TRACE = DebugOutput.TRACE;
058: final static int VALUES = DebugOutput.VALUES;
059: final static int MISC = DebugOutput.MISC;
060: final static int LINE_TRACE = DebugOutput.LINE_TRACE;
061: final static int NONE = DebugOutput.NONE;
062: final static int EXCEPTION = DebugOutput.EXCEPTION;
063:
064: protected DebugOutput debugPrinter;
065:
066: protected String theFilename;
067:
068: protected int marker;
069:
070: protected void debugOutputLn(int outputType, String theOutput) {
071: if (theOutput.equals(""))
072: debugPrinter.println(outputType, theOutput);
073: else
074: debugPrinter.println(outputType, getClass().getName()
075: + "::" + theOutput);
076: } // End of debugOutputLn
077:
078: // Return a string consisting of the next 4 bytes in the file
079: public String getToken() throws ParsingErrorException {
080: byte tokenBuffer[] = new byte[4];
081: try {
082: int readResult = read(tokenBuffer, 0, 4);
083: if (readResult == -1) {
084: debugOutputLn(LINE_TRACE, "no token - returning null");
085: return null;
086: }
087: return new String(tokenBuffer);
088: } catch (IOException e) {
089: debugOutputLn(EXCEPTION, "getToken: " + e);
090: throw new ParsingErrorException(e.getMessage());
091: }
092: }
093:
094: /**
095: * Skip ahead amount bytes in the file
096: */
097: public void skipLength(int amount) throws ParsingErrorException {
098: try {
099: skip((long) amount);
100: marker += amount;
101: } catch (IOException e) {
102: debugOutputLn(EXCEPTION, "skipLength: " + e);
103: throw new ParsingErrorException(e.getMessage());
104: }
105: }
106:
107: /**
108: * Read four bytes from the file and return their integer value
109: */
110: public int getInt() throws ParsingErrorException {
111: try {
112: int x = 0;
113: for (int i = 0; i < 4; i++) {
114: int readResult = read();
115: if (readResult == -1)
116: throw new ParsingErrorException("Unexpected EOF");
117: x = (x << 8) | readResult;
118: }
119: return x;
120: } catch (IOException e) {
121: debugOutputLn(EXCEPTION, "getInt: " + e);
122: throw new ParsingErrorException(e.getMessage());
123: }
124: }
125:
126: /**
127: * Read four bytes from the file and return their float value
128: */
129: public float getFloat() throws ParsingErrorException {
130: return Float.intBitsToFloat(getInt());
131: } // End of getFloat
132:
133: /**
134: * Returns the name of the file associated with this stream
135: */
136: public String getFilename() {
137: return theFilename;
138: } // End of getFilename
139:
140: /**
141: * Returns a string read from the file. The string is assumed to
142: * end with '0'.
143: */
144: public String getString() throws ParsingErrorException {
145: byte buf[] = new byte[512];
146: try {
147: byte b;
148: int len = 0;
149: do {
150: b = (byte) read();
151: buf[len++] = b;
152: } while (b != 0);
153: // Have to read an even number of bytes
154: if (len % 2 != 0)
155: read();
156: } catch (IOException e) {
157: debugOutputLn(EXCEPTION, "getString: " + e);
158: throw new ParsingErrorException(e.getMessage());
159: }
160: return new String(buf);
161: } // End of getString
162:
163: /**
164: * Reads an array of xyz values.
165: */
166: public void getVerts(float ar[], int num)
167: throws ParsingErrorException {
168: for (int i = 0; i < num; i++) {
169: ar[i * 3 + 0] = getFloat();
170: ar[i * 3 + 1] = getFloat();
171: ar[i * 3 + 2] = -getFloat();
172: }
173: } // End of getVerts
174:
175: /**
176: * Reads two bytes from the file and returns their integer value.
177: */
178: public int getShortInt() throws ParsingErrorException {
179: int i = 0;
180: try {
181: i = read();
182: i = (i << 8) | read();
183: // Sign extension
184: if ((i & 0x8000) != 0)
185: i |= 0xffff0000;
186: } catch (IOException e) {
187: debugOutputLn(EXCEPTION, "getShortInt: " + e);
188: throw new ParsingErrorException(e.getMessage());
189: }
190: return i;
191: } // End of getShortInt
192:
193: /**
194: * Returns the current position in the file
195: */
196: public int getMarker() {
197: // protected field inherited from BufferedInputStream
198: return marker;
199: } // End of getMarker
200:
201: public int read() throws IOException {
202: marker++;
203: return super .read();
204: } // End of read()
205:
206: public int read(byte[] buffer, int offset, int count)
207: throws IOException {
208: int ret = super .read(buffer, offset, count);
209: if (ret != -1)
210: marker += ret;
211: return ret;
212: } // End of read(byte[], int, int)
213:
214: /**
215: * Constructor.
216: */
217: public LWOBFileReader(String filename) throws FileNotFoundException {
218: super (new FileInputStream(filename));
219:
220: // Add constants on this line to get more debug output
221: debugPrinter = new DebugOutput(127);
222:
223: marker = 0;
224: } // End of constructor
225:
226: public LWOBFileReader(java.net.URL url) throws java.io.IOException {
227: super (url.openStream());
228:
229: // add constants on this line to get more debug output
230: debugPrinter = new DebugOutput(127);
231:
232: marker = 0;
233: }
234:
235: } // End of file LWOBFileReader
|