Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
WriterOutputStream |
|
| 1.2857142857142858;1.286 |
1 | /* | |
2 | * Copyright 2007-2009 The Kuali Foundation | |
3 | * | |
4 | * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php | |
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.kuali.rice.core.util; | |
17 | ||
18 | import java.io.IOException; | |
19 | import java.io.OutputStream; | |
20 | import java.io.Writer; | |
21 | ||
22 | /** | |
23 | * This class wraps a Writer in an Outputstream. Supports setting a custom encoding | |
24 | * in the constructor. Otherwise the default encoding is used. | |
25 | * | |
26 | * @author Kuali Rice Team (rice.collab@kuali.org) | |
27 | * | |
28 | */ | |
29 | public class WriterOutputStream extends OutputStream { | |
30 | ||
31 | private Writer writer; | |
32 | private String encoding; | |
33 | 0 | private byte[] tmpBuffer = new byte[1]; |
34 | ||
35 | 0 | public WriterOutputStream(Writer writer, String encoding) { |
36 | 0 | this.writer = writer; |
37 | 0 | this.encoding = encoding; |
38 | 0 | } |
39 | ||
40 | public WriterOutputStream(Writer writer) { | |
41 | 0 | this(writer, null); |
42 | 0 | } |
43 | ||
44 | @Override | |
45 | public synchronized void write(int data) throws IOException { | |
46 | 0 | tmpBuffer[0] = (byte)data; |
47 | 0 | writer.write(new String(tmpBuffer)); |
48 | 0 | } |
49 | ||
50 | @Override | |
51 | public void close() throws IOException { | |
52 | 0 | writer.close(); |
53 | 0 | } |
54 | ||
55 | @Override | |
56 | public void flush() throws IOException { | |
57 | 0 | writer.flush(); |
58 | 0 | } |
59 | ||
60 | @Override | |
61 | public void write(byte[] data, int offset, int length) throws IOException { | |
62 | 0 | if (encoding == null) { |
63 | 0 | writer.write(new String(data, offset, length)); |
64 | } else { | |
65 | 0 | writer.write(new String(data, offset, length, encoding)); |
66 | } | |
67 | 0 | } |
68 | ||
69 | @Override | |
70 | public void write(byte[] data) throws IOException { | |
71 | 0 | if (encoding == null) { |
72 | 0 | writer.write(new String(data)); |
73 | } else { | |
74 | 0 | writer.write(new String(data, encoding)); |
75 | } | |
76 | ||
77 | 0 | } |
78 | ||
79 | } |