KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > util > WriterOutputStream


1 package hudson.util;
2
3 import java.io.IOException JavaDoc;
4 import java.io.OutputStream JavaDoc;
5 import java.io.Writer JavaDoc;
6 import java.nio.ByteBuffer JavaDoc;
7 import java.nio.CharBuffer JavaDoc;
8 import java.nio.charset.Charset JavaDoc;
9 import java.nio.charset.CharsetDecoder JavaDoc;
10 import java.nio.charset.CoderResult JavaDoc;
11 import java.nio.charset.UnsupportedCharsetException JavaDoc;
12
13 /**
14  * {@link OutputStream} that writes to {@link Writer}
15  * by assuming the platform default encoding.
16  *
17  * @author Kohsuke Kawaguchi
18  */

19 public class WriterOutputStream extends OutputStream JavaDoc {
20     private final Writer JavaDoc writer;
21     private final CharsetDecoder JavaDoc decoder;
22
23     private ByteBuffer buf = ByteBuffer.allocate(1024);
24     private CharBuffer JavaDoc out = CharBuffer.allocate(1024);
25
26     public WriterOutputStream(Writer JavaDoc out) {
27         this.writer = out;
28         decoder = DEFAULT_CHARSET.newDecoder();
29     }
30
31     public void write(int b) throws IOException JavaDoc {
32         if(buf.remaining()==0)
33             decode(false);
34         buf.put((byte)b);
35     }
36
37     public void write(byte b[], int off, int len) throws IOException JavaDoc {
38         while(len>0) {
39             if(buf.remaining()==0)
40                 decode(false);
41             int sz = Math.min(buf.remaining(),len);
42             buf.put(b,off,sz);
43             off += sz;
44             len -= sz;
45         }
46     }
47
48     public void flush() throws IOException JavaDoc {
49         decode(false);
50         flushOutput();
51         writer.flush();
52     }
53
54     private void flushOutput() throws IOException JavaDoc {
55         writer.write(out.array(),0,out.position());
56         out.clear();
57     }
58
59     public void close() throws IOException JavaDoc {
60         decode(true);
61         flushOutput();
62         writer.close();
63
64         buf.rewind();
65     }
66
67     /**
68      * Decodes the contents of {@link #buf} as much as possible to {@link #out}.
69      * If necessary {@link #out} is further sent to {@link #writer}.
70      *
71      * <p>
72      * When this method returns, the {@link #buf} is back to the 'accumulation'
73      * mode.
74      *
75      * @param last
76      * if true, tell the decoder that all the input bytes are ready.
77      */

78     private void decode(boolean last) throws IOException JavaDoc {
79         buf.flip();
80         while(true) {
81             CoderResult JavaDoc r = decoder.decode(buf, out, last);
82             if(r==CoderResult.OVERFLOW) {
83                 flushOutput();
84                 continue;
85             }
86             if(r==CoderResult.UNDERFLOW) {
87                 buf.compact();
88                 return;
89             }
90             // otherwise treat it as an error
91
r.throwException();
92         }
93     }
94
95     private static final Charset JavaDoc DEFAULT_CHARSET = getDefaultCharset();
96
97     private static Charset JavaDoc getDefaultCharset() {
98         try {
99             String JavaDoc encoding = System.getProperty("file.encoding");
100             return Charset.forName(encoding);
101         } catch (UnsupportedCharsetException JavaDoc e) {
102             return Charset.forName("UTF-8");
103         }
104     }
105 }
106
Popular Tags