KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > util > EncodingDetectingOutputStreamWriter


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9  */

10 package org.mmbase.util;
11
12 import java.io.*;
13
14 /**
15  * Like {@link java.io.OutputStreamWriter} but it tries to autodetect the encoding of the
16  * OutputStream. This works at least if the OutputStream is XML, which is a very common thing to be for Resources.
17  *
18  * For this to work at least the first part (e.g. the first 100 bytes) need to be buffered.
19  *
20  * If determining the encoding did not succeed it is supposed to be 'UTF-8', which is (should be) an
21  * acceptable encoding, and also the default encoding for XML streams.
22  *
23  * @author Michiel Meeuwissen
24  * @since MMBase-1.8
25  * @version $Id: EncodingDetectingOutputStreamWriter.java,v 1.2 2005/01/30 16:46:35 nico Exp $
26  * @todo Is it named correctly?
27  */

28 public class EncodingDetectingOutputStreamWriter extends Writer {
29
30     private OutputStream outputStream;
31
32     // Either wrapped or buffer is null, and the other one is currenlty in use.
33
private Writer wrapped = null;
34     private StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(100);
35
36     EncodingDetectingOutputStreamWriter(OutputStream os) {
37         outputStream = os;
38     }
39
40     /**
41      * Stop buffering, determine encoding, and start behaving as a normal OutputStreamWriter (by
42      * wrapping one). Unless, this happened already.
43      */

44     private void wrap() throws IOException {
45         if (wrapped == null) {
46             String JavaDoc encoding = GenericResponseWrapper.getXMLEncoding(buffer.toString());
47             if (encoding == null) {
48                 encoding = "UTF-8";
49             }
50             try {
51                 wrapped = new OutputStreamWriter(outputStream, encoding);
52             } catch (UnsupportedEncodingException uee) {
53             }
54             wrapped.write(buffer.toString());
55             buffer = null;
56         }
57     }
58     public void close() throws IOException {
59         wrap();
60         wrapped.close();
61     }
62     public void flush() throws IOException {
63         wrap();
64         wrapped.flush();
65     }
66
67     public void write(char[] cbuf) throws IOException {
68         if (wrapped != null) {
69             wrapped.write(cbuf);
70         } else {
71             write(cbuf, 0, cbuf.length);
72         }
73     }
74
75     public void write(int c) throws IOException {
76         if (wrapped != null) { wrapped.write(c); } else { super.write(c); }
77     }
78
79     public void write(String JavaDoc str) throws IOException {
80         if (wrapped != null) { wrapped.write(str); } else { super.write(str); }
81     }
82
83     public void write(String JavaDoc str, int off, int len) throws IOException {
84         if (wrapped != null) { wrapped.write(str, off, len); } else { super.write(str, off, len); }
85
86     }
87     public void write(char[] cbuf, int off, int len) throws IOException {
88         if (wrapped != null) {
89             wrapped.write(cbuf, off, len);
90         } else {
91             for (int i = off; i < len + off; i++) {
92                 buffer.append(cbuf[i]);
93                 if (buffer.length() == 100) {
94                     wrap();
95                     i++;
96                     if (i < len) {
97                         wrapped.write(cbuf, i, len - (i - off));
98                     }
99                     break;
100                 }
101             }
102         }
103     }
104 }
105
Popular Tags