KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > server > http > ChunkedInputStream


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.server.http;
30
31 import com.caucho.vfs.ReadStream;
32 import com.caucho.vfs.StreamImpl;
33
34 import java.io.IOException JavaDoc;
35
36 /**
37  * StreamImpl so servlets can read POST data as a normal stream.
38  */

39 class ChunkedInputStream extends StreamImpl {
40   private ReadStream _next;
41   private int _available;
42
43   void init(ReadStream next)
44   {
45     _next = next;
46     _available = 0;
47   }
48
49   public boolean canRead()
50   {
51     return true;
52   }
53
54   public int getAvailable()
55   {
56     return _available;
57   }
58
59   /**
60    * Reads more data from the input stream.
61    */

62   public int read(byte []buf, int offset, int len) throws IOException JavaDoc
63   {
64     // The chunk still has more data left
65
if (_available > 0) {
66       if (_available < len)
67     len = _available;
68
69       len = _next.read(buf, offset, len);
70
71       if (len > 0)
72         _available -= len;
73     }
74     // The chunk is done, so read the next chunk
75
else if (_available == 0) {
76       _available = readChunkLength();
77
78       // the new chunk has data
79
if (_available > 0) {
80     if (_available < len)
81       len = _available;
82
83     len = _next.read(buf, offset, len);
84
85     if (len > 0)
86       _available -= len;
87       }
88       // the new chunk is the last
89
else {
90     _available = -1;
91     len = -1;
92       }
93     }
94     else
95       len = -1;
96
97     return len;
98   }
99
100   /**
101    * Reads the next chunk length from the input stream.
102    */

103   private int readChunkLength()
104     throws IOException JavaDoc
105   {
106     int length = 0;
107     int ch;
108
109     // skip whitespace
110
for (ch = _next.read();
111      ch == '\r' || ch == ' ' || ch == '\n';
112      ch = _next.read()) {
113     }
114
115     // XXX: This doesn't properly handle the case when when the browser
116
// sends headers at the end of the data. See the HTTP/1.1 spec.
117
for (; ch > 0 && ch != '\r' && ch != '\n'; ch = _next.read()) {
118       if (ch >= '0' && ch <= '9')
119     length = 16 * length + ch - '0';
120       else if (ch >= 'a' && ch <= 'f')
121     length = 16 * length + ch - 'a' + 10;
122       else if (ch >= 'A' && ch <= 'F')
123     length = 16 * length + ch - 'A' + 10;
124       else if (ch == ' ' || ch == '\t') {
125     //if (dbg.canWrite())
126
// dbg.println("unexpected chunk whitespace.");
127
}
128       else
129     throw new IOException JavaDoc("HTTP/1.1 protocol error: bad chunk");
130     }
131
132     if (ch == '\r')
133       ch = _next.read();
134
135     return length;
136   }
137 }
138
Popular Tags