home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 25 / CDROM25.iso / Share / prog / VJ11 / VJTRIAL.EXE / IE30Java.exe / classd.exe / sun / net / www / MeteredStream.java < prev    next >
Encoding:
Java Source  |  1997-01-27  |  3.7 KB  |  143 lines

  1. /*
  2.  * @(#)MeteredStream.java    1.13 95/02/07 Chris Warth
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package sun.net.www;
  21.  
  22. import java.net.URL;
  23. import java.util.*;
  24. import java.io.*;
  25. import sun.net.ProgressData;
  26.  
  27. public 
  28. class MeteredStream extends FilterInputStream 
  29. {
  30.     // Class variables.
  31.     private static int    total_need;    
  32.     private static int    total_read;    
  33.     private static int    total_connections = 0;
  34.  
  35.     // Instance variables.
  36.     int expected;
  37.     int    count = 0;
  38.     public URL url = null;
  39.    
  40.     private synchronized static void globalJustRead(int n) {
  41.     total_read += n;
  42.     }
  43.  
  44.     private synchronized static void updateExpected(int est) {
  45.     total_need += est;
  46.     }
  47.  
  48.     private synchronized static void addConnection(MeteredStream s) {
  49.     total_connections += 1;
  50.     }
  51.  
  52.     private synchronized static void removeConnection(MeteredStream s) {
  53.     total_connections -= 1;
  54.     total_read -= s.expected;
  55.     total_need -= s.expected;
  56.     }
  57.  
  58.     /*
  59.      * Provide read-only access the static variables of interest to
  60.      * observers.  It is expected that these methods are accessed
  61.      * with the static monitor locked.  This is usually the case
  62.      * because the notification to observers happens through the
  63.      * synchronized method checkUpdate().
  64.      */
  65.     public static synchronized 
  66.     ProgressReport checkProgress(ProgressReport pr) {
  67.     return pr.set(total_read, total_need, total_connections);
  68.     }
  69.  
  70.     public MeteredStream(InputStream is, int estimate, URL u) {
  71.     this(is, estimate);
  72.     this.url = u;
  73.     }
  74.  
  75.     public MeteredStream(InputStream is, int estimate) {
  76.     super(is);
  77.     updateExpected(estimate);
  78.     expected = estimate;
  79.     addConnection(this);
  80.     }
  81.  
  82.     private final void justRead(int n) {
  83.     if (count + n > expected) {
  84.         n = expected - count;
  85.     }
  86.     count += n;
  87.     globalJustRead(n);
  88.     ProgressData.pdata.update(url, count, expected);
  89.     }
  90.  
  91.  
  92.     public int read() throws java.io.IOException {
  93.     int c = super.read();
  94.     if (c != -1) {
  95.         justRead(1);
  96.     }
  97.     return c;
  98.     }
  99.  
  100.     /*
  101.     public int read(byte b[]) 
  102.     This routine is implemented in terms of read(byte b[], int off, int len)
  103.     so we don't need to override it here.
  104.     */
  105.  
  106.     public int read(byte b[], int off, int len) throws java.io.IOException {
  107.     int n = super.read(b, off, len);
  108.     if (n != -1) {
  109.         justRead(n);
  110.     }
  111.     return n;
  112.     }
  113.     
  114.     public long skip(long n) throws IOException {
  115.     n = super.skip(n);
  116.     if (n != -1) {
  117.         justRead((int)n);
  118.     }
  119.     return n;
  120.     }
  121.  
  122.     boolean closed = false;
  123.  
  124.     public void close() throws IOException {
  125.     super.close();
  126.     if (!closed) {
  127.         closed = true;
  128.         justRead(expected - count);
  129.         removeConnection(this);
  130.     }
  131.     ProgressData.pdata.unregister(url);
  132.     }
  133.  
  134.     protected void finalize() {
  135.     if (!closed) {
  136.         closed = true;
  137.         justRead(expected - count);
  138.         removeConnection(this);
  139.     }
  140.     ProgressData.pdata.unregister(url);
  141.     }
  142. }
  143.