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 StringBuilder} 27 * or {@link StringBuffer}. 28 * </p> 29 * 30 * @since 2.5 31 * @see Appendable 32 * 33 * @param <T> The type of the {@link Appendable} wrapped by this AppendableOutputStream. 34 */ 35 public class AppendableOutputStream <T extends Appendable> extends OutputStream { 36 37 private final T appendable; 38 39 /** 40 * Constructs a new instance with the specified appendable. 41 * 42 * @param appendable the appendable to write to 43 */ 44 public AppendableOutputStream(final T appendable) { 45 this.appendable = appendable; 46 } 47 48 /** 49 * Gets the target appendable. 50 * 51 * @return the target appendable 52 */ 53 public T getAppendable() { 54 return appendable; 55 } 56 57 /** 58 * Write a character to the underlying appendable. 59 * 60 * @param b the character to write 61 * @throws IOException upon error 62 */ 63 @Override 64 public void write(final int b) throws IOException { 65 appendable.append((char) b); 66 } 67 68 }