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;
021
022/**
023 * OutputStream implementation that writes the data to an {@link Appendable}
024 * Object.
025 * <p>
026 * For example, can be used with any {@link java.io.Writer} or a {@link java.lang.StringBuilder}
027 * or {@link java.lang.StringBuffer}.
028 *
029 * @since 2.5
030 * @see java.lang.Appendable
031 * @version $Id: AppendableOutputStream.java 1687815 2015-06-26 17:18:56Z krosenvold $
032 */
033public class AppendableOutputStream <T extends Appendable> extends OutputStream {
034
035    private final T appendable;
036
037    /**
038     * Construct a new instance with the specified appendable.
039     *
040     * @param appendable the appendable to write to
041     */
042    public AppendableOutputStream(T appendable) {
043        this.appendable = appendable;
044    }
045
046    /**
047     * Write a character to the underlying appendable.
048     *
049     * @param b the character to write
050     * @throws IOException upon error
051     */
052    @Override
053    public void write(int b) throws IOException {
054        appendable.append((char)b);
055    }
056
057    /**
058     * Return the target appendable.
059     *
060     * @return the target appendable
061     */
062    public T getAppendable() {
063        return appendable;
064    }
065
066}