KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > 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.axis.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     static final byte[] LAST_TOKEN = "0\r\n\r\n".getBytes();
53
54     public void write(byte[] b,
55         int off,
56         int len)
57         throws IOException JavaDoc {
58         if (len == 0) return;
59            
60         out.write((Integer.toHexString(len)).getBytes());
61         out.write(CRLF);
62         out.write(b, off, len);
63         out.write(CRLF);
64     }
65
66     /*
67      public void flush()
68      throws IOException {
69      out.flush();
70      }
71      */

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