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

Java Develop

Development Platform:

Java

  1. /*
  2.  * @(#)ChunkedInputStream.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. import java.io.EOFException;
  35. import java.io.InputStream;
  36. import java.io.FilterInputStream;
  37. /**
  38.  * This class de-chunks an input stream.
  39.  *
  40.  * @version 0.3-3  06/05/2001
  41.  * @author Ronald Tschal鋜
  42.  */
  43. class ChunkedInputStream extends FilterInputStream
  44. {
  45.     /**
  46.      * @param is the input stream to dechunk
  47.      */
  48.     ChunkedInputStream(InputStream is)
  49.     {
  50. super(is);
  51.     }
  52.     byte[] one = new byte[1];
  53.     public synchronized int read() throws IOException
  54.     {
  55. int b = read(one, 0, 1);
  56. if (b == 1)
  57.     return (one[0] & 0xff);
  58. else
  59.     return -1;
  60.     }
  61.     private long chunk_len = -1;
  62.     private boolean eof   = false;
  63.     public synchronized int read(byte[] buf, int off, int len)
  64.     throws IOException
  65.     {
  66. if (eof)  return -1;
  67. if (chunk_len == -1)    // it's a new chunk
  68. {
  69.     try
  70. { chunk_len = Codecs.getChunkLength(in); }
  71.     catch (ParseException pe)
  72. { throw new IOException(pe.toString()); }
  73. }
  74. if (chunk_len > 0)              // it's data
  75. {
  76.     if (len > chunk_len)  len = (int) chunk_len;
  77.     int rcvd = in.read(buf, off, len);
  78.     if (rcvd == -1)
  79. throw new EOFException("Premature EOF encountered");
  80.     chunk_len -= rcvd;
  81.     if (chunk_len == 0) // got the whole chunk
  82.     {
  83. in.read();  // CR
  84. in.read();  // LF
  85. chunk_len = -1;
  86.     }
  87.     return rcvd;
  88. }
  89. else     // the footers (trailers)
  90. {
  91.     // discard
  92.     Request dummy =
  93.     new Request(null, null, null, null, null, null, false);
  94.     new Response(dummy, null).readTrailers(in);
  95.     eof = true;
  96.     return -1;
  97. }
  98.     }
  99.     public synchronized long skip(long num)  throws IOException
  100.     {
  101. byte[] tmp = new byte[(int) num];
  102. int got = read(tmp, 0, (int) num);
  103. if (got > 0)
  104.     return (long) got;
  105. else
  106.     return 0L;
  107.     }
  108.     public synchronized int available()  throws IOException
  109.     {
  110. if (eof)  return 0;
  111. if (chunk_len != -1)
  112.     return (int) chunk_len + in.available();
  113. else
  114.     return in.available();
  115.     }
  116. }