home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / un2maiq4 / pjjava / src / pj / net / remotestring.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  4.2 KB  |  160 lines

  1. /*---------------------------------------------------------------------------
  2.  
  3.     Written by the Personal Journal developers of Dow Jones & Company, Inc.
  4.  
  5.     Dow Jones makes no representations or warranties about 
  6.     the suitability of this software, either express or 
  7.     implied, including but not limited to the implied warranties 
  8.     of merchantability, fitness for a particular purpose, 
  9.     or non-infringement.  Dow Jones will not be liable for 
  10.     any damages suffered by a user as a result of using, 
  11.     modifying or distributing this software or its derivatives.
  12.  
  13.  
  14.     @(#)RemoteString.java  0.00 rphall
  15.  
  16.         A RemoteString is a String stored at a URL.
  17.  
  18.     Authors:
  19.  
  20.         rphall          Rick Hall
  21.  
  22.     Version Ident:
  23.  
  24.         $Header$
  25.  
  26.     History:
  27.  
  28.         16-Jan-1996 rphall     Initial Creation
  29.  
  30. ---------------------------------------------------------------------------*/
  31.  
  32. package pj.net;
  33.  
  34. import java.io.BufferedInputStream;
  35. import java.io.IOException;
  36. import java.net.URL;
  37. import java.net.URLConnection;
  38.  
  39. /**
  40.  * A RemoteString is a String stored at a URL.  The current
  41.  * implementation assumes the String is stored as a file
  42.  * of ANSI bytes.
  43.  * <P>
  44.  * This class is totally unrelated to RemoteObject and subclasses
  45.  * defined in the sun.tools.debug package.
  46.  * <P>
  47.  * This class is related to RemoteImage and RemoteURL defined
  48.  * in this package (pj.net).
  49.  *
  50.  * @see pj.net.RemoteImage
  51.  * @see pj.net.RemoteURL
  52.  * @version 0.00 16-Jan-1996
  53.  * @author Rick Hall
  54. */
  55. public class RemoteString
  56.     {
  57.     // --- Instance variables
  58.  
  59.     /**
  60.      * The URL from which a String is loaded.
  61.     */
  62.     private URL urlSource;
  63.  
  64.     /**
  65.      * A String resolved from the url.
  66.      * May be null if the url or its contents are invalid.
  67.     */
  68.     private String str;
  69.  
  70.     // --- Public constructors
  71.  
  72.     /**
  73.      * Constructs an RemoteString based on the specified URL.
  74.     */
  75.     public RemoteString(URL u)
  76.         {
  77.         urlSource = u;
  78.         str = null;
  79.         }
  80.  
  81.     // --- Public operations
  82.  
  83.     /**
  84.      * @return The source URL of this RemoteString.
  85.     */
  86.     public URL getSourceURL()
  87.         { 
  88.         return urlSource; 
  89.         }
  90.  
  91.     /**
  92.      * Get a String resolved from a url.
  93.      * <P>
  94.      * This operation does not automatically force a new resolution.
  95.      * If the current String is not null, the current String is returned.
  96.      * If the current String is null, this operation resolves a new String from
  97.      * the source URL.  The returned String is null if the source URL
  98.      * or its contents are invalid.
  99.      * @return The current String, or null if the source url or its contents
  100.      * are invalid.
  101.     */
  102.     public String getString()
  103.         {
  104.         if (str == null)
  105.             str = resolveString();
  106.  
  107.         return str;
  108.         }
  109.  
  110.     // --- Private operations
  111.  
  112.     /**
  113.      * Resolve a String from the source url.
  114.      * The String data is loaded immediately.
  115.      * This operation blocks until the string data is finished loading.
  116.      * @return May be null if url or its contents are invalid;
  117.     */
  118.     private String resolveString()
  119.         {
  120.         String s = null;
  121.         try {
  122.             s = getStringData(urlSource);
  123.             }
  124.         catch(Exception e)
  125.             {
  126.             System.out.println("RemoteString getString, exception = "+e.toString());
  127.             }
  128.  
  129.         return s;
  130.         }
  131.  
  132.     private String getStringData(URL u)
  133.         throws IOException
  134.         {
  135.         // Connect to resource
  136.         URLConnection ucConnection = u.openConnection();
  137.  
  138.         // Open buffered stream to resource
  139.         int iBufSize = 1024;
  140.         BufferedInputStream bis =
  141.                 new BufferedInputStream( u.openStream(), iBufSize );
  142.  
  143.         // Download content and create String
  144.         String s = "";
  145.         int nbytes = 0;
  146.         byte b[] = new byte[iBufSize];
  147.         while(nbytes >= 0)
  148.             {
  149.             nbytes = bis.read(b,0,iBufSize);
  150.             if (nbytes < 0)
  151.                 break;
  152.             s += new String(b,0,0,nbytes);
  153.             }
  154.         return s;
  155.         } // getStringData
  156.  
  157.     
  158.  
  159.     } // RemoteString
  160.