ResponseHandler.java
Upload User: demmber
Upload Date: 2007-12-22
Package Size: 717k
Code Size: 4k
Category:

Java Develop

Development Platform:

Java

  1. /*
  2.  * @(#)ResponseHandler.java 0.3-3 06/05/2001
  3.  *
  4.  *  This file is part of the HTTPClient package
  5.  *  Copyright (C) 1996-2001 Ronald Tschal鋜
  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.  *  The HTTPClient's home page is located at:
  28.  *
  29.  *  http://www.innovation.ch/java/HTTPClient/ 
  30.  *
  31.  */
  32. package HTTPClient;
  33. import java.io.IOException;
  34. /**
  35.  * This holds various information about an active response. Used by the
  36.  * StreamDemultiplexor and RespInputStream.
  37.  *
  38.  * @version 0.3-3  06/05/2001
  39.  * @author Ronald Tschal鋜
  40.  * @since V0.2
  41.  */
  42. final class ResponseHandler
  43. {
  44.     /** the response stream */
  45.     RespInputStream     stream;
  46.     /** the response class */
  47.     Response            resp;
  48.     /** the response class */
  49.     Request             request;
  50.     /** signals that the demux has closed the response stream, and that
  51. therefore no more data can be read */
  52.     boolean             eof = false;
  53.     /** this is non-null if the stream has an exception pending */
  54.     IOException         exception = null;
  55.     /**
  56.      * Creates a new handler. This also allocates the response input
  57.      * stream.
  58.      *
  59.      * @param resp     the reponse
  60.      * @param request  the request
  61.      * @param demux    our stream demultiplexor.
  62.      */
  63.     ResponseHandler(Response resp, Request request, StreamDemultiplexor demux)
  64.     {
  65. this.resp     = resp;
  66. this.request  = request;
  67. this.stream   = new RespInputStream(demux, this);
  68. Log.write(Log.DEMUX, "Demux: Opening stream " + this.stream.hashCode() +
  69.      " for demux (" + demux.hashCode() + ")");
  70.     }
  71.     /** holds the string that marks the end of this stream; used for
  72. multipart delimited responses. */
  73.     private byte[] endbndry = null;
  74.     /** holds the compilation of the above string */
  75.     private int[]  end_cmp  = null;
  76.     /**
  77.      * return the boundary string for this response. Set's up the
  78.      * InputStream buffer if neccessary.
  79.      *
  80.      * @param  MasterStream the input stream from which the stream demux
  81.      *                      is reading.
  82.      * @return the boundary string.
  83.      */
  84.     byte[] getEndBoundary(BufferedInputStream MasterStream)
  85. throws IOException, ParseException
  86.     {
  87. if (endbndry == null)
  88.     setupBoundary(MasterStream);
  89. return endbndry;
  90.     }
  91.     /**
  92.      * return the compilation of the boundary string for this response.
  93.      * Set's up the InputStream buffer if neccessary.
  94.      *
  95.      * @param  MasterStream the input stream from which the stream demux
  96.      *                      is reading.
  97.      * @return the compiled boundary string.
  98.      */
  99.     int[] getEndCompiled(BufferedInputStream MasterStream)
  100. throws IOException, ParseException
  101.     {
  102. if (end_cmp == null)
  103.     setupBoundary(MasterStream);
  104. return end_cmp;
  105.     }
  106.     /**
  107.      * Gets the boundary string, compiles it for searching, and initializes
  108.      * the buffered input stream.
  109.      */
  110.     void setupBoundary(BufferedInputStream MasterStream)
  111. throws IOException, ParseException
  112.     {
  113. String endstr = "--" + Util.getParameter("boundary",
  114.     resp.getHeader("Content-Type")) +
  115. "--rn";
  116. endbndry = endstr.getBytes("8859_1");
  117. end_cmp = Util.compile_search(endbndry);
  118. MasterStream.markForSearch();
  119.     }
  120. }