Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/prolifics/servlet/ChunkedOutputStream.java


1   /*************************************************/
2   /*  Copyright (c) 1999         */
3   /*      by         */
4   /*  JYACC, Inc., New York NY USA     */
5   /*  and contributors.         */
6   /*  Use of this program is governed by the   */
7   /*  JYACC Public License Version 1.0, a copy of   */
8   /*  which can be obtained at       */
9   /*  http://www.possl.org/jyacc-license.html   */
10  /*************************************************/
11  
12  /* @(#)ChunkedOutputStream.java  77.1 99/04/22 15:03:03 */
13  
14  package com.prolifics.servlet;
15  import java.io.*;
16  import javax.servlet.*;
17  import javax.servlet.http.*;
18  
19  /**
20   * The class implements a buffered output stream. By setting up such 
21   * an output stream, an application can write bytes to the underlying 
22   * output stream without necessarily causing a call to the underlying 
23   * system for each byte written. The data is written into a buffer, 
24   * and then written to the underlying stream if the buffer reaches 
25   * its capacity, the buffer output stream is closed, or the buffer 
26   * output stream is explicity flushed. 
27   *
28   * @author    Prolifics
29   * @version    @(#)ChunkedOutputStream.java  77.1 99/04/22 15:03:03
30   * @since   JDK1.0
31   */
32  class ChunkedOutputStream extends FilterOutputStream
33  {
34    public ChunkedOutputStream (OutputStream out)
35    {
36      super(out);
37    }
38  
39    public static void main(String args[])
40      throws FileNotFoundException, IOException
41    {
42      InputStream in = new FileInputStream("upload.htm");
43      OutputStream out = new ChunkedOutputStream(System.out);
44      out = new BufferedOutputStream(out);
45      int chr = 0;
46  
47      while ((chr = in.read()) > 0)
48      {
49        out.write(chr);
50      }
51    }
52  
53    public void write(int val)
54      throws IOException
55    {
56      byte buf[] = new byte[1];
57      
58      buf[0] = (byte) val;
59      write(buf);
60    }
61  
62    public void write(byte buf[])
63      throws IOException
64    {
65      write(buf, 0, buf.length);
66    }
67  
68    public void write(byte buf[], int off, int count)
69      throws IOException
70    {
71      String beginStr = Integer.toHexString(count) + "\r\n";
72      String endStr   = "\r\n";
73      out.write(beginStr.getBytes("8859_1"));
74      if (count > 0)
75      {
76        out.write(buf, off, count);
77      }
78      out.write(endStr.getBytes("8859_1"));
79    }
80  
81    public void close() throws IOException
82    {
83      if (out == null)
84      {
85        return;
86      }
87  
88      try
89      {
90        flush();
91        write(null, 0, 0);
92        flush();
93      }
94      catch (IOException ignored)
95      {
96      }
97      out.close();
98      out = null;
99    }
100 
101 }