View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      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  package org.apache.commons.io.output;
18  
19  import java.io.IOException;
20  import java.io.OutputStream;
21  
22  /**
23   * OutputStream implementation that writes the data to an {@link Appendable}
24   * Object.
25   * <p>
26   * For example, can be used with any {@link java.io.Writer} or a {@link java.lang.StringBuilder}
27   * or {@link java.lang.StringBuffer}.
28   *
29   * @since 2.5
30   * @see java.lang.Appendable
31   * @version $Id$
32   */
33  public class AppendableOutputStream <T extends Appendable> extends OutputStream {
34  
35      private final T appendable;
36  
37      /**
38       * Construct a new instance with the specified appendable.
39       *
40       * @param appendable the appendable to write to
41       */
42      public AppendableOutputStream(final T appendable) {
43          this.appendable = appendable;
44      }
45  
46      /**
47       * Write a character to the underlying appendable.
48       *
49       * @param b the character to write
50       * @throws IOException upon error
51       */
52      @Override
53      public void write(final int b) throws IOException {
54          appendable.append((char)b);
55      }
56  
57      /**
58       * Return the target appendable.
59       *
60       * @return the target appendable
61       */
62      public T getAppendable() {
63          return appendable;
64      }
65  
66  }