KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > matuschek > util > LimitedBandwidthStream


1 package net.matuschek.util;
2
3 import java.io.FilterInputStream JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.io.InputStream JavaDoc;
6 /*********************************************
7     Copyright (c) 2001 by Daniel Matuschek
8 *********************************************/

9
10
11 /**
12  * A FilterInputStream with a limited bandwith
13  *
14  * This implements an filter for an existing input stream that allows
15  * it to limit the read bandwidth. This can be useful for network
16  * streams that should be limited to a specified bandwidth.
17  *
18  * @author <a HREF="mailto: daniel@matuschek.net">Daniel Matuschek</a>
19  * @version $id$
20  */

21 public class LimitedBandwidthStream
22   extends FilterInputStream JavaDoc {
23   
24
25   /** usable bandwidth in bytes/second **/
26   private int bandwidth = 0;
27   
28   /** bandwidth limit will be calculated form the start time **/
29   private boolean isReading = false;
30
31   /** number of bytes read **/
32   private int count = 0;
33
34   /** check bandwidth every n bytes **/
35   private static int CHECK_INTERVAL = 100;
36
37   /** start time **/
38   long starttime = 0;
39
40   /** used time **/
41   long usedtime = 0;
42   
43
44   /**
45    * initializes the LimitedBandWidth stream
46    */

47   public LimitedBandwidthStream (InputStream JavaDoc in, int bandwidth)
48     throws IOException JavaDoc
49   {
50     super(in);
51
52     if (bandwidth > 0) {
53       this.bandwidth=bandwidth;
54     } else {
55       this.bandwidth=0;
56     }
57
58     count = 0;
59   }
60
61   /**
62    * Reads the next byte.
63    *
64    * Reads the next byte of data from this input stream. The value byte
65    * is returned as an int in the range 0 to 255. If no byte is available
66    * because the end of the stream has been reached, the value -1 is
67    * returned. This method blocks until input data is available, the end
68    * of the stream is detected, or an exception is thrown.
69    * If the bandwidth consumption exceeds the defined limit, read will block
70    * until the bandwidth is in the limit again.
71    *
72    * @return the next byte from the stream or -1 if end-of-stream
73    */

74   public int read()
75     throws IOException JavaDoc
76   {
77     long currentBandwidth;
78
79     if (! isReading) {
80       starttime = System.currentTimeMillis();
81       isReading = true;
82     }
83
84     // do bandwidth check only if bandwidth
85
if ((bandwidth > 0) &&
86     ((count % CHECK_INTERVAL) == 0)) {
87       do {
88     usedtime = System.currentTimeMillis()-starttime;
89     if (usedtime > 0) {
90       currentBandwidth = (count*1000) / usedtime;
91     } else {
92       currentBandwidth = 0;
93     }
94     if (currentBandwidth > bandwidth) {
95       try {
96         Thread.sleep(100);
97       } catch (InterruptedException JavaDoc e) {}
98     }
99       } while (currentBandwidth > bandwidth);
100     }
101
102     count++;
103     return super.read();
104   }
105
106   /**
107    * Shortcut for read(b,0,b.length)
108    *
109    * @see #read(byte[], int, int)
110    */

111   public int read(byte[] b) throws IOException JavaDoc {
112     return read(b, 0, b.length);
113   }
114   
115   /**
116    * Reads a block of bytes from the stream.
117    *
118    * If the bandwith is not limited, it simply used the
119    * read(byte[], int, int) method of the input stream, otherwise it
120    * uses multiple read() request to enforce bandwith limitation (this
121    * is easier to implement using byte reads).
122    *
123    * @return the number of bytes read or -1 at end of stream
124    */

125   public int read(byte[] b, int off, int len) throws IOException JavaDoc {
126     int mycount = 0;
127     int current = 0;
128     // limit bandwidth ?
129
if (bandwidth > 0) {
130       for (int i=off; i < off+len; i++) {
131     current = read();
132     if (current == -1) {
133       return mycount;
134     } else {
135       b[i]=(byte)current;
136       count++;
137       mycount++;
138     }
139       }
140       return mycount;
141     } else {
142       return in.read(b, off, len);
143     }
144   }
145       
146 } // LimitedBandwidthStream
147
Popular Tags