001: /*
002: * $RCSfile: ContentNegotiator.java,v $
003: *
004: * @(#)ContentNegotiator.java 1.11 98/11/05 20:35:57
005: *
006: * Copyright (c) 1996-1998 Sun Microsystems, Inc. All Rights Reserved.
007: *
008: * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
009: * modify and redistribute this software in source and binary code form,
010: * provided that i) this copyright notice and license appear on all copies of
011: * the software; and ii) Licensee does not utilize the software in a manner
012: * which is disparaging to Sun.
013: *
014: * This software is provided "AS IS," without a warranty of any kind. ALL
015: * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
016: * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
017: * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
018: * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
019: * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
020: * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
021: * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
022: * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
023: * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
024: * POSSIBILITY OF SUCH DAMAGES.
025: *
026: * This software is not designed or intended for use in on-line control of
027: * aircraft, air traffic, aircraft navigation or aircraft communications; or in
028: * the design, construction, operation or maintenance of any nuclear
029: * facility. Licensee represents and warrants that it will not use or
030: * redistribute the Software for such purposes.
031: *
032: * $Revision: 1.2 $
033: * $Date: 2005/02/03 23:06:54 $
034: * $State: Exp $
035: */
036: /*
037: *@Author: Rick Goldberg
038: *@Author: Doug Gehringer
039: */
040: package org.jdesktop.j3d.loaders.vrml97.impl;
041:
042: import java.io.*;
043:
044: import java.net.*;
045:
046: /** Description of the Class */
047: class ContentNegotiator extends Thread implements Runnable {
048: org.jdesktop.j3d.loaders.vrml97.impl.Sound sound;
049: URL url;
050: boolean locked;
051: byte[] buffer;
052: Object content;
053: final static int SOUND_LOADER = 1;
054: final static int URL_BYTE_LOADER = 2;
055: int negotiation = 0;
056:
057: /**
058: *Constructor for the ContentNegotiator object
059: *
060: *@param url Description of the Parameter
061: */
062: ContentNegotiator(URL url) {
063: // to do get negotiation from content-type
064: this .url = url;
065: this .locked = true;
066: negotiation = URL_BYTE_LOADER;
067: start();
068: }
069:
070: // this is no longer in use
071: /**
072: *Constructor for the ContentNegotiator object
073: *
074: *@param sound Description of the Parameter
075: */
076: ContentNegotiator(Sound sound) {
077: this .sound = sound;
078: this .locked = true;
079: negotiation = SOUND_LOADER;
080: start();
081: }
082:
083: /** Main processing method for the ContentNegotiator object */
084: public void run() {
085: startLoading();
086: }
087:
088: /**
089: * Gets the content attribute of the ContentNegotiator object
090: *
091: *@return The content value
092: */
093: synchronized Object getContent() {
094: if (locked) {
095: try {
096: wait();
097: } catch (InterruptedException ie) {
098: ;
099: }
100: }
101:
102: return content;
103: }
104:
105: /** Description of the Method */
106: synchronized void startLoading() {
107: if (negotiation == URL_BYTE_LOADER) {
108: try {
109: DataInputStream d;
110: int contentLength = -1;
111: URLConnection urlc = url.openConnection();
112: urlc.connect();
113: urlc.getContentType();
114: d = new DataInputStream(urlc.getInputStream());
115: contentLength = urlc.getContentLength();
116: buffer = new byte[contentLength];
117: content = buffer;
118: if (d != null) {
119: d.readFully(buffer, 0, contentLength);
120: }
121: yield();
122: } catch (IOException ie) {
123: ie.printStackTrace();
124: }
125: locked = false;
126: notify();
127: } else if (negotiation == SOUND_LOADER) {
128: if (Browser.getBrowser().debug) {
129: System.out.println("Sound negotiation begin");
130: }
131: // This could be a problem, since the j3d.Sound needs to be live
132: //while ( !((javax.media.j3d.Sound)(sound.soundImpl)).isReady() ) {
133: //yield();
134: //try { wait(100); } catch (InterruptedException ie) {;}
135: //}
136: content = sound;
137: locked = false;
138: if (Browser.getBrowser().debug) {
139: System.out.println("Sound negotiation end");
140: }
141: notify();
142: }
143:
144: }
145: }
|