KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > oreilly > servlet > multipart > LimitedServletInputStream


1 // Copyright (C) 1999-2001 by Jason Hunter <jhunter_AT_acm_DOT_org>.
2
// All rights reserved. Use of this class is limited.
3
// Please see the LICENSE for more information.
4

5 package com.oreilly.servlet.multipart;
6
7 import java.io.IOException JavaDoc;
8
9 import javax.servlet.ServletInputStream JavaDoc;
10
11 /**
12  * A <code>LimitedServletInputStream</code> wraps another
13  * <code>ServletInputStream</code> in order to keep track of how many bytes
14  * have been read and detect when the Content-Length limit has been reached.
15  * This is necessary since some servlet containers are slow to notice the end
16  * of stream and cause the client code to hang if it tries to read past it.
17  *
18  * @author Jason Hunter
19  * @author Geoff Soutter
20  * @version 1.0, 2000/10/27, initial revision
21  */

22 public class LimitedServletInputStream extends ServletInputStream JavaDoc {
23   
24   /** input stream we are filtering */
25   private ServletInputStream JavaDoc in;
26   
27   /** number of bytes to read before giving up */
28   private int totalExpected;
29   
30   /** number of bytes we have currently read */
31   private int totalRead = 0;
32   
33   /**
34    * Creates a <code>LimitedServletInputStream</code> with the specified
35    * length limit that wraps the provided <code>ServletInputStream</code>.
36    */

37   public LimitedServletInputStream(ServletInputStream JavaDoc in, int totalExpected) {
38     this.in = in;
39     this.totalExpected = totalExpected;
40   }
41
42   /**
43    * Implement length limitation on top of the <code>readLine</code> method of
44    * the wrapped <code>ServletInputStream</code>.
45    *
46    * @param b an array of bytes into which data is read.
47    * @param off an integer specifying the character at which
48    * this method begins reading.
49    * @param len an integer specifying the maximum number of
50    * bytes to read.
51    * @return an integer specifying the actual number of bytes
52    * read, or -1 if the end of the stream is reached.
53    * @exception IOException if an I/O error occurs.
54    */

55   public int readLine(byte b[], int off, int len) throws IOException JavaDoc {
56     int result, left = totalExpected - totalRead;
57     if (left <= 0) {
58       return -1;
59     } else {
60       result = ((ServletInputStream JavaDoc)in).readLine(b, off, Math.min(left, len));
61     }
62     if (result > 0) {
63       totalRead += result;
64     }
65     return result;
66   }
67
68   /**
69    * Implement length limitation on top of the <code>read</code> method of
70    * the wrapped <code>ServletInputStream</code>.
71    *
72    * @return the next byte of data, or <code>-1</code> if the end of the
73    * stream is reached.
74    * @exception IOException if an I/O error occurs.
75    */

76   public int read() throws IOException JavaDoc {
77     if (totalRead >= totalExpected) {
78       return -1;
79     }
80
81     int result = in.read();
82     if (result != -1) {
83       totalRead++;
84     }
85     return result;
86   }
87   
88   /**
89    * Implement length limitation on top of the <code>read</code> method of
90    * the wrapped <code>ServletInputStream</code>.
91    *
92    * @param b destination buffer.
93    * @param off offset at which to start storing bytes.
94    * @param len maximum number of bytes to read.
95    * @return the number of bytes read, or <code>-1</code> if the end of
96    * the stream has been reached.
97    * @exception IOException if an I/O error occurs.
98    */

99   public int read( byte b[], int off, int len ) throws IOException JavaDoc {
100     int result, left = totalExpected - totalRead;
101     if (left <= 0) {
102       return -1;
103     } else {
104       result = in.read(b, off, Math.min(left, len));
105     }
106     if (result > 0) {
107       totalRead += result;
108     }
109     return result;
110   }
111 }
112
Popular Tags