001: /*
002: * @(#)ResponseHandler.java 0.3-2 18/06/1999
003: *
004: * This file is part of the HTTPClient package
005: * Copyright (C) 1996-1999 Ronald Tschalär
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free
019: * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
020: * MA 02111-1307, USA
021: *
022: * For questions, suggestions, bug-reports, enhancement-requests etc.
023: * I may be contacted at:
024: *
025: * ronald@innovation.ch
026: *
027: */
028:
029: package HTTPClient;
030:
031: import java.io.IOException;
032:
033: /**
034: * This holds various information about an active response. Used by the
035: * StreamDemultiplexor and RespInputStream.
036: *
037: * @version 0.3-2 18/06/1999
038: * @author Ronald Tschalär
039: * @since V0.2
040: */
041:
042: final class ResponseHandler implements GlobalConstants {
043: /** the response stream */
044: RespInputStream stream;
045:
046: /** the response class */
047: Response resp;
048:
049: /** the response class */
050: Request request;
051:
052: /** signals that the demux has closed the response stream, and that
053: therefore no more data can be read */
054: boolean eof = false;
055:
056: /** this is non-null if the stream has an exception pending */
057: IOException exception = null;
058:
059: /**
060: * Creates a new handler. This also allocates the response input
061: * stream.
062: *
063: * @param resp the reponse
064: * @param request the request
065: * @param demux our stream demultiplexor.
066: */
067: ResponseHandler(Response resp, Request request,
068: StreamDemultiplexor demux) {
069: this .resp = resp;
070: this .request = request;
071: this .stream = new RespInputStream(demux, this );
072:
073: if (DebugDemux)
074: System.err.println("Demux: Opening stream "
075: + this .stream.hashCode() + " (" + " ("
076: + demux.hashCode() + ") (" + Thread.currentThread()
077: + ")");
078: }
079:
080: /** holds the string that marks the end of this stream; used for
081: multipart delimited responses. */
082: private byte[] endbndry = null;
083:
084: /** holds the compilation of the above string */
085: private int[] end_cmp = null;
086:
087: /**
088: * return the boundary string for this response. Set's up the
089: * InputStream buffer if neccessary.
090: *
091: * @param MasterStream the input stream from which the stream demux
092: * is reading.
093: * @return the boundary string.
094: */
095: byte[] getEndBoundary(ExtBufferedInputStream MasterStream)
096: throws IOException, ParseException {
097: if (endbndry == null)
098: setupBoundary(MasterStream);
099:
100: return endbndry;
101: }
102:
103: /**
104: * return the compilation of the boundary string for this response.
105: * Set's up the InputStream buffer if neccessary.
106: *
107: * @param MasterStream the input stream from which the stream demux
108: * is reading.
109: * @return the compiled boundary string.
110: */
111: int[] getEndCompiled(ExtBufferedInputStream MasterStream)
112: throws IOException, ParseException {
113: if (end_cmp == null)
114: setupBoundary(MasterStream);
115:
116: return end_cmp;
117: }
118:
119: /**
120: * Gets the boundary string, compiles it for searching, and initializes
121: * the buffered input stream.
122: */
123: void setupBoundary(ExtBufferedInputStream MasterStream)
124: throws IOException, ParseException {
125: String endstr = "--"
126: + Util.getParameter("boundary", resp
127: .getHeader("Content-Type")) + "--\r\n";
128: endbndry = new byte[endstr.length()];
129: endstr.getBytes(0, endbndry.length, endbndry, 0);
130: end_cmp = Util.compile_search(endbndry);
131: MasterStream.initMark();
132: }
133: }
|