    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.io.output;
    018
    019import java.io.IOException;
    020import java.io.OutputStream;
    021import java.io.Writer;
    022
    023/**
    024 * OutputStream implementation that writes the data to an {@link Appendable}
    025 * Object.
    026 * <p>
    027 * For example, can be used with any {@link Writer} or a {@link StringBuilder}
    028 * or {@link StringBuffer}.
    029 * </p>
    030 *
    031 * @since 2.5
    032 * @see Appendable
    033 * @param <T> The type of the {@link Appendable} wrapped by this AppendableOutputStream.
    034 */
    035public class AppendableOutputStream <T extends Appendable> extends OutputStream {
    036
    037    private final T appendable;
    038
    039    /**
    040     * Constructs a new instance with the specified appendable.
    041     *
    042     * @param appendable the appendable to write to
    043     */
    044    public AppendableOutputStream(final T appendable) {
    045        this.appendable = appendable;
    046    }
    047
    048    /**
    049     * Gets the target appendable.
    050     *
    051     * @return the target appendable
    052     */
    053    public T getAppendable() {
    054        return appendable;
    055    }
    056
    057    /**
    058     * Write a character to the underlying appendable.
    059     *
    060     * @param b the character to write
    061     * @throws IOException upon error
    062     */
    063    @Override
    064    public void write(final int b) throws IOException {
    065        appendable.append((char) b);
    066    }
    067
    068}