AppendableWriter.java

  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. import java.io.IOException;
  19. import java.io.Writer;
  20. import java.util.Objects;

  21. /**
  22.  * Writer implementation that writes the data to an {@link Appendable}
  23.  * Object.
  24.  * <p>
  25.  * For example, can be used with a {@link StringBuilder}
  26.  * or {@link StringBuffer}.
  27.  * </p>
  28.  *
  29.  * @since 2.7
  30.  * @see Appendable
  31.  * @param <T> The type of the {@link Appendable} wrapped by this AppendableWriter.
  32.  */
  33. public class AppendableWriter <T extends Appendable> extends Writer {

  34.     private final T appendable;

  35.     /**
  36.      * Constructs a new instance with the specified appendable.
  37.      *
  38.      * @param appendable the appendable to write to
  39.      */
  40.     public AppendableWriter(final T appendable) {
  41.         this.appendable = appendable;
  42.     }

  43.     /**
  44.      * Appends the specified character to the underlying appendable.
  45.      *
  46.      * @param c the character to append
  47.      * @return this writer
  48.      * @throws IOException upon error
  49.      */
  50.     @Override
  51.     public Writer append(final char c) throws IOException {
  52.         appendable.append(c);
  53.         return this;
  54.     }

  55.     /**
  56.      * Appends the specified character sequence to the underlying appendable.
  57.      *
  58.      * @param csq the character sequence to append
  59.      * @return this writer
  60.      * @throws IOException upon error
  61.      */
  62.     @Override
  63.     public Writer append(final CharSequence csq) throws IOException {
  64.         appendable.append(csq);
  65.         return this;
  66.     }

  67.     /**
  68.      * Appends a subsequence of the specified character sequence to the underlying appendable.
  69.      *
  70.      * @param csq the character sequence from which a subsequence will be appended
  71.      * @param start the index of the first character in the subsequence
  72.      * @param end the index of the character following the last character in the subsequence
  73.      * @return this writer
  74.      * @throws IOException upon error
  75.      */
  76.     @Override
  77.     public Writer append(final CharSequence csq, final int start, final int end) throws IOException {
  78.         appendable.append(csq, start, end);
  79.         return this;
  80.     }

  81.     /**
  82.      * Closes the stream. This implementation does nothing.
  83.      *
  84.      * @throws IOException upon error
  85.      */
  86.     @Override
  87.     public void close() throws IOException {
  88.         // noop
  89.     }

  90.     /**
  91.      * Flushes the stream. This implementation does nothing.
  92.      *
  93.      * @throws IOException upon error
  94.      */
  95.     @Override
  96.     public void flush() throws IOException {
  97.         // noop
  98.     }

  99.     /**
  100.      * Gets the target appendable.
  101.      *
  102.      * @return the target appendable
  103.      */
  104.     public T getAppendable() {
  105.         return appendable;
  106.     }

  107.     /**
  108.      * Writes a portion of an array of characters to the underlying appendable.
  109.      *
  110.      * @param cbuf an array with the characters to write
  111.      * @param off offset from which to start writing characters
  112.      * @param len number of characters to write
  113.      * @throws IOException upon error
  114.      */
  115.     @Override
  116.     public void write(final char[] cbuf, final int off, final int len) throws IOException {
  117.         Objects.requireNonNull(cbuf, "cbuf");
  118.         if (len < 0 || off + len > cbuf.length) {
  119.             throw new IndexOutOfBoundsException("Array Size=" + cbuf.length +
  120.                     ", offset=" + off + ", length=" + len);
  121.         }
  122.         for (int i = 0; i < len; i++) {
  123.             appendable.append(cbuf[off + i]);
  124.         }
  125.     }

  126.     /**
  127.      * Writes a character to the underlying appendable.
  128.      *
  129.      * @param c the character to write
  130.      * @throws IOException upon error
  131.      */
  132.     @Override
  133.     public void write(final int c) throws IOException {
  134.         appendable.append((char) c);
  135.     }

  136.     /**
  137.      * Writes a portion of a String to the underlying appendable.
  138.      *
  139.      * @param str a string
  140.      * @param off offset from which to start writing characters
  141.      * @param len number of characters to write
  142.      * @throws IOException upon error
  143.      */
  144.     @Override
  145.     public void write(final String str, final int off, final int len) throws IOException {
  146.         // appendable.append will add "null" for a null String; add an explicit null check
  147.         Objects.requireNonNull(str, "str");
  148.         appendable.append(str, off, off + len);
  149.     }

  150. }