home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-08-14 | 4.2 KB | 160 lines |
- /*---------------------------------------------------------------------------
-
- Written by the Personal Journal developers of Dow Jones & Company, Inc.
-
- Dow Jones makes no representations or warranties about
- the suitability of this software, either express or
- implied, including but not limited to the implied warranties
- of merchantability, fitness for a particular purpose,
- or non-infringement. Dow Jones will not be liable for
- any damages suffered by a user as a result of using,
- modifying or distributing this software or its derivatives.
-
-
- @(#)RemoteString.java 0.00 rphall
-
- A RemoteString is a String stored at a URL.
-
- Authors:
-
- rphall Rick Hall
-
- Version Ident:
-
- $Header$
-
- History:
-
- 16-Jan-1996 rphall Initial Creation
-
- ---------------------------------------------------------------------------*/
-
- package pj.net;
-
- import java.io.BufferedInputStream;
- import java.io.IOException;
- import java.net.URL;
- import java.net.URLConnection;
-
- /**
- * A RemoteString is a String stored at a URL. The current
- * implementation assumes the String is stored as a file
- * of ANSI bytes.
- * <P>
- * This class is totally unrelated to RemoteObject and subclasses
- * defined in the sun.tools.debug package.
- * <P>
- * This class is related to RemoteImage and RemoteURL defined
- * in this package (pj.net).
- *
- * @see pj.net.RemoteImage
- * @see pj.net.RemoteURL
- * @version 0.00 16-Jan-1996
- * @author Rick Hall
- */
- public class RemoteString
- {
- // --- Instance variables
-
- /**
- * The URL from which a String is loaded.
- */
- private URL urlSource;
-
- /**
- * A String resolved from the url.
- * May be null if the url or its contents are invalid.
- */
- private String str;
-
- // --- Public constructors
-
- /**
- * Constructs an RemoteString based on the specified URL.
- */
- public RemoteString(URL u)
- {
- urlSource = u;
- str = null;
- }
-
- // --- Public operations
-
- /**
- * @return The source URL of this RemoteString.
- */
- public URL getSourceURL()
- {
- return urlSource;
- }
-
- /**
- * Get a String resolved from a url.
- * <P>
- * This operation does not automatically force a new resolution.
- * If the current String is not null, the current String is returned.
- * If the current String is null, this operation resolves a new String from
- * the source URL. The returned String is null if the source URL
- * or its contents are invalid.
- * @return The current String, or null if the source url or its contents
- * are invalid.
- */
- public String getString()
- {
- if (str == null)
- str = resolveString();
-
- return str;
- }
-
- // --- Private operations
-
- /**
- * Resolve a String from the source url.
- * The String data is loaded immediately.
- * This operation blocks until the string data is finished loading.
- * @return May be null if url or its contents are invalid;
- */
- private String resolveString()
- {
- String s = null;
- try {
- s = getStringData(urlSource);
- }
- catch(Exception e)
- {
- System.out.println("RemoteString getString, exception = "+e.toString());
- }
-
- return s;
- }
-
- private String getStringData(URL u)
- throws IOException
- {
- // Connect to resource
- URLConnection ucConnection = u.openConnection();
-
- // Open buffered stream to resource
- int iBufSize = 1024;
- BufferedInputStream bis =
- new BufferedInputStream( u.openStream(), iBufSize );
-
- // Download content and create String
- String s = "";
- int nbytes = 0;
- byte b[] = new byte[iBufSize];
- while(nbytes >= 0)
- {
- nbytes = bis.read(b,0,iBufSize);
- if (nbytes < 0)
- break;
- s += new String(b,0,0,nbytes);
- }
- return s;
- } // getStringData
-
-
-
- } // RemoteString
-