01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: /* $Id$ */
19:
20: package org.apache.fop.util;
21:
22: import java.io.IOException;
23: import java.io.OutputStream;
24: import java.io.Writer;
25:
26: /**
27: * An OutputStream wrapper for a Writer.
28: */
29: public class WriterOutputStream extends OutputStream {
30:
31: private Writer writer;
32: private String encoding;
33:
34: /**
35: * Creates a new WriterOutputStream.
36: * @param writer the Writer to write to
37: */
38: public WriterOutputStream(Writer writer) {
39: this (writer, null);
40: }
41:
42: /**
43: * Creates a new WriterOutputStream.
44: * @param writer the Writer to write to
45: * @param encoding the encoding to use, or null if the default encoding should be used
46: */
47: public WriterOutputStream(Writer writer, String encoding) {
48: this .writer = writer;
49: this .encoding = encoding;
50: }
51:
52: /**
53: * {@inheritDoc}
54: */
55: public void close() throws IOException {
56: writer.close();
57: }
58:
59: /**
60: * {@inheritDoc}
61: */
62: public void flush() throws IOException {
63: writer.flush();
64: }
65:
66: /**
67: * {@inheritDoc}
68: */
69: public void write(byte[] buf, int offset, int length)
70: throws IOException {
71: if (encoding != null) {
72: writer.write(new String(buf, offset, length, encoding));
73: } else {
74: writer.write(new String(buf, offset, length));
75: }
76: }
77:
78: /**
79: * {@inheritDoc}
80: */
81: public void write(byte[] buf) throws IOException {
82: write(buf, 0, buf.length);
83: }
84:
85: /**
86: * {@inheritDoc}
87: */
88: public void write(int b) throws IOException {
89: write(new byte[] { (byte) b });
90: }
91:
92: }
|