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.BufferedReader;
024import java.io.Closeable;
025import java.io.EOFException;
026import java.io.IOException;
027import java.io.Reader;
028import java.io.Writer;
029import java.nio.CharBuffer;
030import java.util.ArrayList;
031import java.util.List;
032import org.jspecify.annotations.Nullable;
033
034/**
035 * Provides utility methods for working with character streams.
036 *
037 * @author Chris Nokleberg
038 * @author Bin Zhu
039 * @author Colin Decker
040 * @since 1.0
041 */
042@J2ktIncompatible
043@GwtIncompatible
044public final class CharStreams {
045
046  // 2K chars (4K bytes)
047  private static final int DEFAULT_BUF_SIZE = 0x800;
048
049  /** Creates a new {@code CharBuffer} for buffering reads or writes. */
050  static CharBuffer createBuffer() {
051    return CharBuffer.allocate(DEFAULT_BUF_SIZE);
052  }
053
054  private CharStreams() {}
055
056  /**
057   * Copies all characters between the {@link Readable} and {@link Appendable} objects. Does not
058   * close or flush either object.
059   *
060   * <p><b>Java 10+ users:</b> If the input is a {@link Reader} and the output is a {@link Writer},
061   * prefer {@link Reader#transferTo(Writer)}.
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   * <p><b>Java 25+ users:</b> If the input is a {@link Reader}, prefer {@link
159   * Reader#readAllAsString()}.
160   *
161   * @param r the object to read from
162   * @return a string containing all the characters
163   * @throws IOException if an I/O error occurs
164   */
165  public static String toString(Readable r) throws IOException {
166    return toStringBuilder(r).toString();
167  }
168
169  /**
170   * Reads all characters from a {@link Readable} object into a new {@link StringBuilder} instance.
171   * Does not close the {@code Readable}.
172   *
173   * @param r the object to read from
174   * @return a {@link StringBuilder} containing all the characters
175   * @throws IOException if an I/O error occurs
176   */
177  private static StringBuilder toStringBuilder(Readable r) throws IOException {
178    StringBuilder sb = new StringBuilder();
179    if (r instanceof Reader) {
180      copyReaderToBuilder((Reader) r, sb);
181    } else {
182      copy(r, sb);
183    }
184    return sb;
185  }
186
187  /**
188   * Reads all of the lines from a {@link Readable} object. The lines do not include
189   * line-termination characters, but do include other leading and trailing whitespace.
190   *
191   * <p>Does not close the {@code Readable}. If reading files or resources you should use the {@link
192   * Files#readLines} and {@link Resources#readLines} methods.
193   *
194   * <p>This method prioritizes convenience over performance: It reads the entire input into memory
195   * immediately. To instead read and process lines individually, use an alternative like {@link
196   * BufferedReader#lines()}.
197   *
198   * <p><b>Java 25+ users:</b> If the input is a {@link Reader}, you may prefer {@link
199   * Reader#readAllLines()}. The two may have different performance characteristics for different
200   * {@link Reader} classes and Java versions, but both methods read the entire input into memory
201   * immediately, so we discourage use of both when memory usage is a concern.
202   *
203   * @param r the object to read from
204   * @return a mutable {@link List} containing all the lines
205   * @throws IOException if an I/O error occurs
206   */
207  public static List<String> readLines(Readable r) throws IOException {
208    List<String> result = new ArrayList<>();
209    LineReader lineReader = new LineReader(r);
210    String line;
211    while ((line = lineReader.readLine()) != null) {
212      result.add(line);
213    }
214    return result;
215  }
216
217  /**
218   * Streams lines from a {@link Readable} object, stopping when the processor returns {@code false}
219   * or all lines have been read and returning the result produced by the processor. Does not close
220   * {@code readable}. Note that this method may not fully consume the contents of {@code readable}
221   * if the processor stops processing early.
222   *
223   * <p><b>Users who can use {@code Stream}:</b> If your input is a {@link Reader}, consider
224   * wrapping it with a {@link BufferedReader} and operating on the {@link java.util.stream.Stream}
225   * of lines returned by {@link BufferedReader#lines()}.
226   *
227   * @throws IOException if an I/O error occurs
228   * @since 14.0
229   */
230  @CanIgnoreReturnValue // some processors won't return a useful result
231  @ParametricNullness
232  public static <T extends @Nullable Object> T readLines(
233      Readable readable, LineProcessor<T> processor) throws IOException {
234    checkNotNull(readable);
235    checkNotNull(processor);
236
237    LineReader lineReader = new LineReader(readable);
238    String line;
239    while ((line = lineReader.readLine()) != null) {
240      if (!processor.processLine(line)) {
241        break;
242      }
243    }
244    return processor.getResult();
245  }
246
247  /**
248   * Reads and discards data from the given {@code Readable} until the end of the stream is reached.
249   * Returns the total number of chars read. Does not close the stream.
250   *
251   * @since 20.0
252   */
253  @CanIgnoreReturnValue
254  public static long exhaust(Readable readable) throws IOException {
255    long total = 0;
256    long read;
257    CharBuffer buf = createBuffer();
258    while ((read = readable.read(buf)) != -1) {
259      total += read;
260      Java8Compatibility.clear(buf);
261    }
262    return total;
263  }
264
265  /**
266   * Discards {@code n} characters of data from the reader. This method will block until the full
267   * amount has been skipped. Does not close the reader.
268   *
269   * @param reader the reader to read from
270   * @param n the number of characters to skip
271   * @throws EOFException if this stream reaches the end before skipping all the characters
272   * @throws IOException if an I/O error occurs
273   */
274  public static void skipFully(Reader reader, long n) throws IOException {
275    checkNotNull(reader);
276    while (n > 0) {
277      long amt = reader.skip(n);
278      if (amt == 0) {
279        throw new EOFException();
280      }
281      n -= amt;
282    }
283  }
284
285  /**
286   * Returns a {@link Writer} that simply discards written chars.
287   *
288   * <p><b>Java 11+ users:</b> use {@link Writer#nullWriter()} instead. Note that the {@link
289   * CharStreams} method returns a singleton writer whose {@code close} method has no effect, while
290   * the {@link Writer#nullWriter()} method returns a new instance whose methods throw after the
291   * instance is {@link Writer#close() closed}.
292   *
293   * @since 15.0
294   */
295  public static Writer nullWriter() {
296    return NullWriter.INSTANCE;
297  }
298
299  private static final class NullWriter extends Writer {
300
301    private static final NullWriter INSTANCE = new NullWriter();
302
303    @Override
304    public void write(int c) {}
305
306    @Override
307    public void write(char[] cbuf) {
308      checkNotNull(cbuf);
309    }
310
311    @Override
312    public void write(char[] cbuf, int off, int len) {
313      checkPositionIndexes(off, off + len, cbuf.length);
314    }
315
316    @Override
317    public void write(String str) {
318      checkNotNull(str);
319    }
320
321    @Override
322    public void write(String str, int off, int len) {
323      checkPositionIndexes(off, off + len, str.length());
324    }
325
326    @Override
327    public Writer append(@Nullable CharSequence csq) {
328      return this;
329    }
330
331    @Override
332    public Writer append(@Nullable CharSequence csq, int start, int end) {
333      checkPositionIndexes(start, end, csq == null ? "null".length() : csq.length());
334      return this;
335    }
336
337    @Override
338    public Writer append(char c) {
339      return this;
340    }
341
342    @Override
343    public void flush() {}
344
345    @Override
346    public void close() {}
347
348    @Override
349    public String toString() {
350      return "CharStreams.nullWriter()";
351    }
352  }
353
354  /**
355   * Returns a Writer that sends all output to the given {@link Appendable} target. Closing the
356   * writer will close the target if it is {@link Closeable}, and flushing the writer will flush the
357   * target if it is {@link java.io.Flushable}.
358   *
359   * @param target the object to which output will be sent
360   * @return a new Writer object, unless target is a Writer, in which case the target is returned
361   */
362  public static Writer asWriter(Appendable target) {
363    if (target instanceof Writer) {
364      return (Writer) target;
365    }
366    return new AppendableWriter(target);
367  }
368}