File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\util\WriterOutputStream.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.util;
25
26 import java.io.OutputStream;
27 import java.io.Writer;
28 import java.io.IOException;
29 import java.nio.charset.CharsetDecoder;
30 import java.nio.charset.CodingErrorAction;
31 import java.nio.charset.CoderResult;
32 import java.nio.charset.Charset;
33 import java.nio.charset.UnsupportedCharsetException;
34 import java.nio.*;
35
36 /**
37 * {@link OutputStream} that writes to {@link Writer}
38 * by assuming the platform default encoding.
39 *
40 * @author Kohsuke Kawaguchi
41 * @deprecated since 2008-05-28.
42 * Use the one in stapler.
43 */
44 public class WriterOutputStream extends OutputStream {
45 private final Writer writer;
46 private final CharsetDecoder decoder;
47
48 private java.nio.ByteBuffer buf = java.nio.ByteBuffer.allocate(1024);
49 private CharBuffer out = CharBuffer.allocate(1024);
50
51 public WriterOutputStream(Writer out) {
52 this.writer = out;
53 decoder = DEFAULT_CHARSET.newDecoder();
54 decoder.onMalformedInput(CodingErrorAction.REPLACE);
55 decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
56 }
57
58 public void write(int b) throws IOException {
59 if(buf.remaining()==0)
60 decode(false);
61 buf.put((byte)b);
62 }
63
64 public void write(byte b[], int off, int len) throws IOException {
65 while(len>0) {
66 if(buf.remaining()==0)
67 decode(false);
68 int sz = Math.min(buf.remaining(),len);
69 buf.put(b,off,sz);
70 off += sz;
71 len -= sz;
72 }
73 }
74
75 public void flush() throws IOException {
76 decode(false);
77 flushOutput();
78 writer.flush();
79 }
80
81 private void flushOutput() throws IOException {
82 writer.write(out.array(),0,out.position());
83 out.clear();
84 }
85
86 public void close() throws IOException {
87 decode(true);
88 flushOutput();
89 writer.close();
90
91 buf.rewind();
92 }
93
94 /**
95 * Decodes the contents of {@link #buf} as much as possible to {@link #out}.
96 * If necessary {@link #out} is further sent to {@link #writer}.
97 *
98 * <p>
99 * When this method returns, the {@link #buf} is back to the 'accumulation'
100 * mode.
101 *
102 * @param last
103 * if true, tell the decoder that all the input bytes are ready.
104 */
105 private void decode(boolean last) throws IOException {
106 buf.flip();
107 while(true) {
108 CoderResult r = decoder.decode(buf, out, last);
109 if(r==CoderResult.OVERFLOW) {
110 flushOutput();
111 continue;
112 }
113 if(r==CoderResult.UNDERFLOW) {
114 buf.compact();
115 return;
116 }
117 // otherwise treat it as an error
118 r.throwException();
119 }
120 }
121
122 private static final Charset DEFAULT_CHARSET = getDefaultCharset();
123
124 private static Charset getDefaultCharset() {
125 try {
126 String encoding = System.getProperty("file.encoding");
127 return Charset.forName(encoding);
128 } catch (UnsupportedCharsetException e) {
129 return Charset.forName("UTF-8");
130 }
131 }
132 }
133