KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > http > ajp > AJP13InputStream


1 // ========================================================================
2
// $Id: AJP13InputStream.java,v 1.11 2006/10/08 14:13:05 gregwilkins Exp $
3
// Copyright 2002-2004 Mort Bay Consulting Pty. Ltd.
4
// ------------------------------------------------------------------------
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
// ========================================================================
15

16 package org.mortbay.http.ajp;
17
18 import java.io.IOException JavaDoc;
19 import java.io.InputStream JavaDoc;
20 import java.io.OutputStream JavaDoc;
21
22 public class AJP13InputStream extends InputStream JavaDoc
23 {
24     /* ------------------------------------------------------------ */
25     private AJP13RequestPacket _packet;
26     private AJP13RequestPacket _getBodyChunk;
27     private InputStream JavaDoc _in;
28     private OutputStream JavaDoc _out;
29     private boolean _gotFirst=false;
30     private boolean _closed;
31
32     /* ------------------------------------------------------------ */
33     AJP13InputStream(InputStream JavaDoc in, OutputStream JavaDoc out, int bufferSize)
34     {
35         _in=in;
36         _out=out;
37         _packet=new AJP13RequestPacket(bufferSize);
38         _getBodyChunk=new AJP13RequestPacket(8);
39         _getBodyChunk.addByte((byte)'A');
40         _getBodyChunk.addByte((byte)'B');
41         _getBodyChunk.addInt(3);
42         _getBodyChunk.addByte(AJP13RequestPacket.__GET_BODY_CHUNK);
43         _getBodyChunk.addInt(bufferSize);
44     }
45
46     /* ------------------------------------------------------------ */
47     public void resetStream()
48     {
49         _gotFirst=false;
50         _closed=false;
51         _packet.reset();
52     }
53
54     /* ------------------------------------------------------------ */
55     public void destroy()
56     {
57         if (_packet!=null)
58             _packet.destroy();
59         _packet=null;
60         if (_getBodyChunk!=null)
61             _getBodyChunk.destroy();
62         _getBodyChunk=null;
63         _in=null;
64         _out=null;
65     }
66
67     /* ------------------------------------------------------------ */
68     public int available() throws IOException JavaDoc
69     {
70         if (_closed)
71             return 0;
72         if (_packet.unconsumedData()==0)
73             fillPacket();
74         return _packet.unconsumedData();
75     }
76
77     /* ------------------------------------------------------------ */
78     public void close() throws IOException JavaDoc
79     {
80         _closed=true;
81     }
82
83     /* ------------------------------------------------------------ */
84     public void mark(int readLimit)
85     {
86     }
87
88     /* ------------------------------------------------------------ */
89     public boolean markSupported()
90     {
91         return false;
92     }
93
94     /* ------------------------------------------------------------ */
95     public void reset() throws IOException JavaDoc
96     {
97         throw new IOException JavaDoc("reset() not supported");
98     }
99
100     /* ------------------------------------------------------------ */
101     public int read() throws IOException JavaDoc
102     {
103         if (_closed)
104             return -1;
105
106         if (_packet.unconsumedData()<=0)
107         {
108             fillPacket();
109             if (_packet.unconsumedData()<=0)
110             {
111                 _closed=true;
112                 return -1;
113             }
114         }
115         return _packet.getByte();
116     }
117
118     /* ------------------------------------------------------------ */
119     public int read(byte[] b, int off, int len) throws IOException JavaDoc
120     {
121         if (_closed)
122             return -1;
123
124         if (_packet.unconsumedData()==0)
125         {
126             fillPacket();
127             if (_packet.unconsumedData()==0)
128             {
129                 _closed=true;
130                 return -1;
131             }
132         }
133
134         return _packet.getBytes(b,off,len);
135     }
136
137     /* ------------------------------------------------------------ */
138     /**
139      * @return The next packet from the stream. The packet is recycled and is
140      * only valid until the next call to nextPacket or read().
141      * @exception IOException
142      */

143     public AJP13RequestPacket nextPacket() throws IOException JavaDoc
144     {
145         if (_packet.read(_in))
146             return _packet;
147         return null;
148     }
149
150     /* ------------------------------------------------------------ */
151     private void fillPacket() throws IOException JavaDoc
152     {
153         if (_closed)
154             return;
155
156         if (_gotFirst||_in.available()==0)
157             _getBodyChunk.write(_out);
158         _gotFirst=true;
159
160         // read packet
161
if (!_packet.read(_in))
162             throw new IOException JavaDoc("EOF");
163
164         if (_packet.unconsumedData()<=0)
165             _closed=true;
166         else if (_packet.getInt()>_packet.getBufferSize())
167             throw new IOException JavaDoc("AJP Protocol error");
168     }
169
170     /* ------------------------------------------------------------ */
171     public long skip(long n) throws IOException JavaDoc
172     {
173         if (_closed)
174             return -1;
175
176         for (int i=0; i<n; i++)
177             if (read()<0)
178                 return i==0?-1:i;
179         return n;
180     }
181 }
182
Popular Tags