KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > HTTPClient > ResponseHandler


1 /*
2  * @(#)ResponseHandler.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
33 /**
34  * This holds various information about an active response. Used by the
35  * StreamDemultiplexor and RespInputStream.
36  *
37  * @version 0.3-2 18/06/1999
38  * @author Ronald Tschalär
39  * @since V0.2
40  */

41
42 final class ResponseHandler implements GlobalConstants
43 {
44     /** the response stream */
45     RespInputStream stream;
46
47     /** the response class */
48     Response resp;
49
50     /** the response class */
51     Request request;
52
53     /** signals that the demux has closed the response stream, and that
54     therefore no more data can be read */

55     boolean eof = false;
56
57     /** this is non-null if the stream has an exception pending */
58     IOException JavaDoc exception = null;
59
60
61     /**
62      * Creates a new handler. This also allocates the response input
63      * stream.
64      *
65      * @param resp the reponse
66      * @param request the request
67      * @param demux our stream demultiplexor.
68      */

69     ResponseHandler(Response resp, Request request, StreamDemultiplexor demux)
70     {
71     this.resp = resp;
72     this.request = request;
73     this.stream = new RespInputStream(demux, this);
74
75     if (DebugDemux)
76         System.err.println("Demux: Opening stream " +
77                 this.stream.hashCode() + " (" +
78                    " (" + demux.hashCode() + ") (" +
79                 Thread.currentThread() + ")");
80     }
81
82
83     /** holds the string that marks the end of this stream; used for
84     multipart delimited responses. */

85     private byte[] endbndry = null;
86
87     /** holds the compilation of the above string */
88     private int[] end_cmp = null;
89
90     /**
91      * return the boundary string for this response. Set's up the
92      * InputStream buffer if neccessary.
93      *
94      * @param MasterStream the input stream from which the stream demux
95      * is reading.
96      * @return the boundary string.
97      */

98     byte[] getEndBoundary(ExtBufferedInputStream MasterStream)
99         throws IOException JavaDoc, ParseException
100     {
101     if (endbndry == null)
102         setupBoundary(MasterStream);
103
104     return endbndry;
105     }
106
107     /**
108      * return the compilation of the boundary string for this response.
109      * Set's up the InputStream buffer if neccessary.
110      *
111      * @param MasterStream the input stream from which the stream demux
112      * is reading.
113      * @return the compiled boundary string.
114      */

115     int[] getEndCompiled(ExtBufferedInputStream MasterStream)
116         throws IOException JavaDoc, ParseException
117     {
118     if (end_cmp == null)
119         setupBoundary(MasterStream);
120
121     return end_cmp;
122     }
123
124     /**
125      * Gets the boundary string, compiles it for searching, and initializes
126      * the buffered input stream.
127      */

128     void setupBoundary(ExtBufferedInputStream MasterStream)
129         throws IOException JavaDoc, ParseException
130     {
131     String JavaDoc endstr = "--" + Util.getParameter("boundary",
132                 resp.getHeader("Content-Type")) +
133             "--\r\n";
134     endbndry = new byte[endstr.length()];
135     endstr.getBytes(0, endbndry.length, endbndry, 0);
136     end_cmp = Util.compile_search(endbndry);
137     MasterStream.initMark();
138     }
139 }
140
141
Popular Tags