001/*
002 * Copyright (C) 2007 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 */
014
015package com.google.common.io;
016
017import static com.google.common.base.Preconditions.checkNotNull;
018import static com.google.common.base.Preconditions.checkPositionIndexes;
019
020import com.google.common.annotations.GwtIncompatible;
021import com.google.common.annotations.J2ktIncompatible;
022import com.google.errorprone.annotations.CanIgnoreReturnValue;
023import java.io.Closeable;
024import java.io.EOFException;
025import java.io.IOException;
026import java.io.Reader;
027import java.io.Writer;
028import java.nio.CharBuffer;
029import java.util.ArrayList;
030import java.util.List;
031import org.checkerframework.checker.nullness.qual.Nullable;
032
033/**
034 * Provides utility methods for working with character streams.
035 *
036 * <p>Some of the methods in this class take arguments with a generic type of {@code Readable &
037 * Closeable}. A {@link java.io.Reader} implements both of those interfaces. Similarly for {@code
038 * Appendable & Closeable} and {@link java.io.Writer}.
039 *
040 * @author Chris Nokleberg
041 * @author Bin Zhu
042 * @author Colin Decker
043 * @since 1.0
044 */
045@J2ktIncompatible
046@GwtIncompatible
047public final class CharStreams {
048
049  // 2K chars (4K bytes)
050  private static final int DEFAULT_BUF_SIZE = 0x800;
051
052  /** Creates a new {@code CharBuffer} for buffering reads or writes. */
053  static CharBuffer createBuffer() {
054    return CharBuffer.allocate(DEFAULT_BUF_SIZE);
055  }
056
057  private CharStreams() {}
058
059  /**
060   * Copies all characters between the {@link Readable} and {@link Appendable} objects. Does not
061   * close or flush either object.
062   *
063   * @param from the object to read from
064   * @param to the object to write to
065   * @return the number of characters copied
066   * @throws IOException if an I/O error occurs
067   */
068  @CanIgnoreReturnValue
069  public static long copy(Readable from, Appendable to) throws IOException {
070    // The most common case is that from is a Reader (like InputStreamReader or StringReader) so
071    // take advantage of that.
072    if (from instanceof Reader) {
073      // optimize for common output types which are optimized to deal with char[]
074      if (to instanceof StringBuilder) {
075        return copyReaderToBuilder((Reader) from, (StringBuilder) to);
076      } else {
077        return copyReaderToWriter((Reader) from, asWriter(to));
078      }
079    }
080
081    checkNotNull(from);
082    checkNotNull(to);
083    long total = 0;
084    CharBuffer buf = createBuffer();
085    while (from.read(buf) != -1) {
086      Java8Compatibility.flip(buf);
087      to.append(buf);
088      total += buf.remaining();
089      Java8Compatibility.clear(buf);
090    }
091    return total;
092  }
093
094  // TODO(lukes): consider allowing callers to pass in a buffer to use, some callers would be able
095  // to reuse buffers, others would be able to size them more appropriately than the constant
096  // defaults
097
098  /**
099   * Copies all characters between the {@link Reader} and {@link StringBuilder} objects. Does not
100   * close or flush the reader.
101   *
102   * <p>This is identical to {@link #copy(Readable, Appendable)} but optimized for these specific
103   * types. CharBuffer has poor performance when being written into or read out of so round tripping
104   * all the bytes through the buffer takes a long time. With these specialized types we can just
105   * use a char array.
106   *
107   * @param from the object to read from
108   * @param to the object to write to
109   * @return the number of characters copied
110   * @throws IOException if an I/O error occurs
111   */
112  @CanIgnoreReturnValue
113  static long copyReaderToBuilder(Reader from, StringBuilder to) throws IOException {
114    checkNotNull(from);
115    checkNotNull(to);
116    char[] buf = new char[DEFAULT_BUF_SIZE];
117    int nRead;
118    long total = 0;
119    while ((nRead = from.read(buf)) != -1) {
120      to.append(buf, 0, nRead);
121      total += nRead;
122    }
123    return total;
124  }
125
126  /**
127   * Copies all characters between the {@link Reader} and {@link Writer} objects. Does not close or
128   * flush the reader or writer.
129   *
130   * <p>This is identical to {@link #copy(Readable, Appendable)} but optimized for these specific
131   * types. CharBuffer has poor performance when being written into or read out of so round tripping
132   * all the bytes through the buffer takes a long time. With these specialized types we can just
133   * use a char array.
134   *
135   * @param from the object to read from
136   * @param to the object to write to
137   * @return the number of characters copied
138   * @throws IOException if an I/O error occurs
139   */
140  @CanIgnoreReturnValue
141  static long copyReaderToWriter(Reader from, Writer to) throws IOException {
142    checkNotNull(from);
143    checkNotNull(to);
144    char[] buf = new char[DEFAULT_BUF_SIZE];
145    int nRead;
146    long total = 0;
147    while ((nRead = from.read(buf)) != -1) {
148      to.write(buf, 0, nRead);
149      total += nRead;
150    }
151    return total;
152  }
153
154  /**
155   * Reads all characters from a {@link Readable} object into a {@link String}. Does not close the
156   * {@code Readable}.
157   *
158   * @param r the object to read from
159   * @return a string containing all the characters
160   * @throws IOException if an I/O error occurs
161   */
162  public static String toString(Readable r) throws IOException {
163    return toStringBuilder(r).toString();
164  }
165
166  /**
167   * Reads all characters from a {@link Readable} object into a new {@link StringBuilder} instance.
168   * Does not close the {@code Readable}.
169   *
170   * @param r the object to read from
171   * @return a {@link StringBuilder} containing all the characters
172   * @throws IOException if an I/O error occurs
173   */
174  private static StringBuilder toStringBuilder(Readable r) throws IOException {
175    StringBuilder sb = new StringBuilder();
176    if (r instanceof Reader) {
177      copyReaderToBuilder((Reader) r, sb);
178    } else {
179      copy(r, sb);
180    }
181    return sb;
182  }
183
184  /**
185   * Reads all of the lines from a {@link Readable} object. The lines do not include
186   * line-termination characters, but do include other leading and trailing whitespace.
187   *
188   * <p>Does not close the {@code Readable}. If reading files or resources you should use the {@link
189   * Files#readLines} and {@link Resources#readLines} methods.
190   *
191   * @param r the object to read from
192   * @return a mutable {@link List} containing all the lines
193   * @throws IOException if an I/O error occurs
194   */
195  public static List<String> readLines(Readable r) throws IOException {
196    List<String> result = new ArrayList<>();
197    LineReader lineReader = new LineReader(r);
198    String line;
199    while ((line = lineReader.readLine()) != null) {
200      result.add(line);
201    }
202    return result;
203  }
204
205  /**
206   * Streams lines from a {@link Readable} object, stopping when the processor returns {@code false}
207   * or all lines have been read and returning the result produced by the processor. Does not close
208   * {@code readable}. Note that this method may not fully consume the contents of {@code readable}
209   * if the processor stops processing early.
210   *
211   * @throws IOException if an I/O error occurs
212   * @since 14.0
213   */
214  @CanIgnoreReturnValue // some processors won't return a useful result
215  @ParametricNullness
216  public static <T extends @Nullable Object> T readLines(
217      Readable readable, LineProcessor<T> processor) throws IOException {
218    checkNotNull(readable);
219    checkNotNull(processor);
220
221    LineReader lineReader = new LineReader(readable);
222    String line;
223    while ((line = lineReader.readLine()) != null) {
224      if (!processor.processLine(line)) {
225        break;
226      }
227    }
228    return processor.getResult();
229  }
230
231  /**
232   * Reads and discards data from the given {@code Readable} until the end of the stream is reached.
233   * Returns the total number of chars read. Does not close the stream.
234   *
235   * @since 20.0
236   */
237  @CanIgnoreReturnValue
238  public static long exhaust(Readable readable) throws IOException {
239    long total = 0;
240    long read;
241    CharBuffer buf = createBuffer();
242    while ((read = readable.read(buf)) != -1) {
243      total += read;
244      Java8Compatibility.clear(buf);
245    }
246    return total;
247  }
248
249  /**
250   * Discards {@code n} characters of data from the reader. This method will block until the full
251   * amount has been skipped. Does not close the reader.
252   *
253   * @param reader the reader to read from
254   * @param n the number of characters to skip
255   * @throws EOFException if this stream reaches the end before skipping all the characters
256   * @throws IOException if an I/O error occurs
257   */
258  public static void skipFully(Reader reader, long n) throws IOException {
259    checkNotNull(reader);
260    while (n > 0) {
261      long amt = reader.skip(n);
262      if (amt == 0) {
263        throw new EOFException();
264      }
265      n -= amt;
266    }
267  }
268
269  /**
270   * Returns a {@link Writer} that simply discards written chars.
271   *
272   * @since 15.0
273   */
274  public static Writer nullWriter() {
275    return NullWriter.INSTANCE;
276  }
277
278  private static final class NullWriter extends Writer {
279
280    private static final NullWriter INSTANCE = new NullWriter();
281
282    @Override
283    public void write(int c) {}
284
285    @Override
286    public void write(char[] cbuf) {
287      checkNotNull(cbuf);
288    }
289
290    @Override
291    public void write(char[] cbuf, int off, int len) {
292      checkPositionIndexes(off, off + len, cbuf.length);
293    }
294
295    @Override
296    public void write(String str) {
297      checkNotNull(str);
298    }
299
300    @Override
301    public void write(String str, int off, int len) {
302      checkPositionIndexes(off, off + len, str.length());
303    }
304
305    @Override
306    public Writer append(@Nullable CharSequence csq) {
307      return this;
308    }
309
310    @Override
311    public Writer append(@Nullable CharSequence csq, int start, int end) {
312      checkPositionIndexes(start, end, csq == null ? "null".length() : csq.length());
313      return this;
314    }
315
316    @Override
317    public Writer append(char c) {
318      return this;
319    }
320
321    @Override
322    public void flush() {}
323
324    @Override
325    public void close() {}
326
327    @Override
328    public String toString() {
329      return "CharStreams.nullWriter()";
330    }
331  }
332
333  /**
334   * Returns a Writer that sends all output to the given {@link Appendable} target. Closing the
335   * writer will close the target if it is {@link Closeable}, and flushing the writer will flush the
336   * target if it is {@link java.io.Flushable}.
337   *
338   * @param target the object to which output will be sent
339   * @return a new Writer object, unless target is a Writer, in which case the target is returned
340   */
341  public static Writer asWriter(Appendable target) {
342    if (target instanceof Writer) {
343      return (Writer) target;
344    }
345    return new AppendableWriter(target);
346  }
347}