KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > HTTPClient > ChunkedInputStream


1 /*
2  * @(#)ChunkedInputStream.java 0.3-2 18/06/1999
3  *
4  * This file is part of the HTTPClient package
5  * Copyright (C) 1996-1999 Ronald Tschalär
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free
19  * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20  * MA 02111-1307, USA
21  *
22  * For questions, suggestions, bug-reports, enhancement-requests etc.
23  * I may be contacted at:
24  *
25  * ronald@innovation.ch
26  *
27  */

28
29 package HTTPClient;
30
31 import java.io.IOException JavaDoc;
32 import java.io.EOFException JavaDoc;
33 import java.io.InputStream JavaDoc;
34 import java.io.FilterInputStream JavaDoc;
35
36
37 /**
38  * This class de-chunks an input stream.
39  *
40  * @version 0.3-2 18/06/1999
41  * @author Ronald Tschalär
42  */

43 class ChunkedInputStream extends FilterInputStream JavaDoc
44 {
45     /**
46      * @param is the input stream to dechunk
47      */

48     ChunkedInputStream(InputStream JavaDoc is)
49     {
50     super(is);
51     }
52
53
54     byte[] one = new byte[1];
55     public synchronized int read() throws IOException JavaDoc
56     {
57     int b = read(one, 0, 1);
58     if (b == 1)
59         return (one[0] & 0xff);
60     else
61         return -1;
62     }
63
64
65     private int chunk_len = -1;
66     private boolean eof = false;
67
68     public synchronized int read(byte[] buf, int off, int len)
69         throws IOException JavaDoc
70     {
71     if (eof) return -1;
72
73     if (chunk_len == -1) // it's a new chunk
74
{
75         try
76         { chunk_len = Codecs.getChunkLength(in); }
77         catch (ParseException pe)
78         { throw new IOException JavaDoc(pe.toString()); }
79     }
80
81     if (chunk_len > 0) // it's data
82
{
83         if (len > chunk_len) len = chunk_len;
84         int rcvd = in.read(buf, off, len);
85         if (rcvd == -1)
86         throw new EOFException JavaDoc("Premature EOF encountered");
87
88         chunk_len -= rcvd;
89         if (chunk_len == 0) // got the whole chunk
90
{
91         in.read(); // CR
92
in.read(); // LF
93
chunk_len = -1;
94         }
95
96         return rcvd;
97     }
98     else // the footers (trailers)
99
{
100         // discard
101
Request dummy =
102             new Request(null, null, null, null, null, null, false);
103         new Response(dummy, null).readTrailers(in);
104
105         eof = true;
106         return -1;
107     }
108     }
109
110
111     public synchronized long skip(long num) throws IOException JavaDoc
112     {
113     byte[] tmp = new byte[(int) num];
114     int got = read(tmp, 0, (int) num);
115
116     if (got > 0)
117         return (long) got;
118     else
119         return 0L;
120     }
121
122
123     public synchronized int available() throws IOException JavaDoc
124     {
125     if (eof) return 0;
126
127     if (chunk_len != -1)
128         return chunk_len + in.available();
129     else
130         return in.available();
131     }
132 }
133
134
Popular Tags