KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xmlrpc > ServerInputStream


1 /*
2  * Copyright 1999,2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17
18 package org.apache.xmlrpc;
19
20 import java.io.BufferedInputStream JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23
24 /**
25  *
26  * @author <a HREF="mailto:hannes@apache.org">Hannes Wallnoefer</a>
27  * @version $Id: ServerInputStream.java,v 1.4 2005/04/22 10:25:57 hgomez Exp $
28  */

29 class ServerInputStream extends InputStream JavaDoc
30 {
31     // bytes remaining to be read from the input stream. This is
32
// initialized from CONTENT_LENGTH (or getContentLength()).
33
// This is used in order to correctly return a -1 when all the
34
// data POSTed was read. If this is left to -1, content length is
35
// assumed as unknown and the standard InputStream methods will be used
36
private long available = -1;
37     private long markedAvailable;
38
39     private BufferedInputStream JavaDoc in;
40
41     /**
42      *
43      * @param in
44      * @param available
45      */

46     public ServerInputStream(BufferedInputStream JavaDoc in, int available)
47     {
48         this.in = in;
49         this.available = available;
50     }
51
52     /**
53      *
54      * @return
55      * @throws IOException
56      */

57     public int read() throws IOException JavaDoc
58     {
59         if (available > 0)
60         {
61             available--;
62             return in.read();
63         }
64         else if (available == -1)
65         {
66             return in.read ();
67         }
68         return -1;
69     }
70
71     /**
72      *
73      * @param b
74      * @return
75      * @throws IOException
76      */

77     public int read(byte b[]) throws IOException JavaDoc
78     {
79         return read(b, 0, b.length);
80     }
81
82     /**
83      *
84      * @param b
85      * @param off
86      * @param len
87      * @return
88      * @throws IOException
89      */

90     public int read(byte b[], int off, int len) throws IOException JavaDoc
91     {
92         if (available > 0)
93         {
94             if (len > available)
95             {
96                 // shrink len
97
len = (int) available;
98             }
99             int read = in.read(b, off, len);
100             if (read != -1)
101             {
102                 available -= read;
103             }
104             else
105             {
106                 available = -1;
107             }
108             return read;
109         }
110         else if (available == -1)
111         {
112             return in.read(b, off, len);
113         }
114         return -1;
115     }
116
117     /**
118      *
119      * @param n
120      * @return
121      * @throws IOException
122      */

123     public long skip(long n) throws IOException JavaDoc
124     {
125         long skip = in.skip(n);
126         if (available > 0)
127         {
128             available -= skip;
129         }
130         return skip;
131     }
132
133     /**
134      *
135      * @param readlimit
136      */

137     public void mark(int readlimit)
138     {
139         in.mark(readlimit);
140         markedAvailable = available;
141     }
142
143     /**
144      *
145      * @throws IOException
146      */

147     public void reset() throws IOException JavaDoc
148     {
149         in.reset();
150         available = markedAvailable;
151     }
152
153     /**
154      *
155      * @return
156      */

157     public boolean markSupported()
158     {
159         return true;
160     }
161 }
162
Popular Tags