KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > transport > http > ChunkedOutputStream


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.axis2.transport.http;
17
18
19 import java.io.FilterOutputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.OutputStream JavaDoc;
22
23
24 /**
25  *
26  * @author Rick Rineholt
27  */

28 public class ChunkedOutputStream extends FilterOutputStream JavaDoc {
29
30     boolean eos = false;
31
32     private ChunkedOutputStream() {
33         super(null);
34     }
35
36     public ChunkedOutputStream(OutputStream JavaDoc os) {
37         super(os);
38     }
39
40     public void write(int b)
41         throws IOException JavaDoc {
42         write(new byte[] {(byte) b}, 0, 1);
43     }
44
45     public void write(byte[] b)
46             
47         throws IOException JavaDoc {
48         write(b, 0, b.length);
49     }
50
51     static final byte[] crlf = "\r\n".getBytes();
52
53     public void write(byte[] b,
54         int off,
55         int len)
56         throws IOException JavaDoc {
57         if (len == 0) return;
58            
59         out.write((Integer.toHexString(len)).getBytes());
60         out.write(crlf);
61         out.write(b, off, len);
62         out.write(crlf);
63     }
64
65     /*
66      public void flush()
67      throws IOException {
68      out.flush();
69      }
70      */

71
72     public void eos()throws IOException JavaDoc {
73         synchronized (this) {
74             if (eos) return;
75             eos = true;
76         }
77         out.write("0\r\n\r\n".getBytes());
78         out.flush();
79     }
80     
81     public void close()
82         throws IOException JavaDoc {
83         eos();
84         out.close();
85     }
86
87 }
88
Popular Tags