001/*
002 * Copyright (C) 2012 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.collect.Streams.stream;
019
020import com.google.common.annotations.GwtIncompatible;
021import com.google.common.annotations.J2ktIncompatible;
022import com.google.common.base.Ascii;
023import com.google.common.base.Optional;
024import com.google.common.base.Splitter;
025import com.google.common.collect.AbstractIterator;
026import com.google.common.collect.ImmutableList;
027import com.google.errorprone.annotations.CanIgnoreReturnValue;
028import com.google.errorprone.annotations.MustBeClosed;
029import java.io.BufferedReader;
030import java.io.Closeable;
031import java.io.IOException;
032import java.io.InputStream;
033import java.io.Reader;
034import java.io.StringReader;
035import java.io.UncheckedIOException;
036import java.io.Writer;
037import java.nio.charset.Charset;
038import java.util.ArrayList;
039import java.util.Iterator;
040import java.util.List;
041import java.util.function.Consumer;
042import java.util.stream.Stream;
043import org.jspecify.annotations.Nullable;
044
045/**
046 * A readable source of characters, such as a text file. Unlike a {@link Reader}, a {@code
047 * CharSource} is not an open, stateful stream of characters that can be read and closed. Instead,
048 * it is an immutable <i>supplier</i> of {@code Reader} instances.
049 *
050 * <p>{@code CharSource} provides two kinds of methods:
051 *
052 * <ul>
053 *   <li><b>Methods that return a reader:</b> These methods should return a <i>new</i>, independent
054 *       instance each time they are called. The caller is responsible for ensuring that the
055 *       returned reader is closed.
056 *   <li><b>Convenience methods:</b> These are implementations of common operations that are
057 *       typically implemented by opening a reader using one of the methods in the first category,
058 *       doing something and finally closing the reader that was opened.
059 * </ul>
060 *
061 * <p>Several methods in this class, such as {@link #readLines()}, break the contents of the source
062 * into lines. Like {@link BufferedReader}, these methods break lines on any of {@code \n}, {@code
063 * \r} or {@code \r\n}, do not include the line separator in each line and do not consider there to
064 * be an empty line at the end if the contents are terminated with a line separator.
065 *
066 * <p>Any {@link ByteSource} containing text encoded with a specific {@linkplain Charset character
067 * encoding} may be viewed as a {@code CharSource} using {@link ByteSource#asCharSource(Charset)}.
068 *
069 * <p><b>Note:</b> In general, {@code CharSource} is intended to be used for "file-like" sources
070 * that provide readers that are:
071 *
072 * <ul>
073 *   <li><b>Finite:</b> Many operations, such as {@link #length()} and {@link #read()}, will either
074 *       block indefinitely or fail if the source creates an infinite reader.
075 *   <li><b>Non-destructive:</b> A <i>destructive</i> reader will consume or otherwise alter the
076 *       source as they are read from it. A source that provides such readers will not be reusable,
077 *       and operations that read from the stream (including {@link #length()}, in some
078 *       implementations) will prevent further operations from completing as expected.
079 * </ul>
080 *
081 * @since 14.0
082 * @author Colin Decker
083 */
084@J2ktIncompatible
085@GwtIncompatible
086public abstract class CharSource {
087
088  /** Constructor for use by subclasses. */
089  protected CharSource() {}
090
091  /**
092   * Returns a {@link ByteSource} view of this char source that encodes chars read from this source
093   * as bytes using the given {@link Charset}.
094   *
095   * <p>If {@link ByteSource#asCharSource} is called on the returned source with the same charset,
096   * the default implementation of this method will ensure that the original {@code CharSource} is
097   * returned, rather than round-trip encoding. Subclasses that override this method should behave
098   * the same way.
099   *
100   * @since 20.0
101   */
102  public ByteSource asByteSource(Charset charset) {
103    return new AsByteSource(charset);
104  }
105
106  /**
107   * Opens a new {@link Reader} for reading from this source. This method returns a new, independent
108   * reader each time it is called.
109   *
110   * <p>The caller is responsible for ensuring that the returned reader is closed.
111   *
112   * @throws IOException if an I/O error occurs while opening the reader
113   */
114  public abstract Reader openStream() throws IOException;
115
116  /**
117   * Opens a new {@link BufferedReader} for reading from this source. This method returns a new,
118   * independent reader each time it is called.
119   *
120   * <p>The caller is responsible for ensuring that the returned reader is closed.
121   *
122   * @throws IOException if an I/O error occurs while of opening the reader
123   */
124  public BufferedReader openBufferedStream() throws IOException {
125    Reader reader = openStream();
126    return (reader instanceof BufferedReader)
127        ? (BufferedReader) reader
128        : new BufferedReader(reader);
129  }
130
131  /**
132   * Opens a new {@link Stream} for reading text one line at a time from this source. This method
133   * returns a new, independent stream each time it is called.
134   *
135   * <p>The returned stream is lazy and only reads from the source in the terminal operation. If an
136   * I/O error occurs while the stream is reading from the source or when the stream is closed, an
137   * {@link UncheckedIOException} is thrown.
138   *
139   * <p>Like {@link BufferedReader#readLine()}, this method considers a line to be a sequence of
140   * text that is terminated by (but does not include) one of {@code \r\n}, {@code \r} or {@code
141   * \n}. If the source's content does not end in a line termination sequence, it is treated as if
142   * it does.
143   *
144   * <p>The caller is responsible for ensuring that the returned stream is closed. For example:
145   *
146   * {@snippet :
147   * try (Stream<String> lines = source.lines()) {
148   *   lines.map(...)
149   *       .filter(...)
150   *       .forEach(...);
151   * }
152   * }
153   *
154   * @throws IOException if an I/O error occurs while opening the stream
155   * @since 22.0 (but only since 33.4.0 in the Android flavor)
156   */
157  @MustBeClosed
158  public Stream<String> lines() throws IOException {
159    BufferedReader reader = openBufferedStream();
160    return reader.lines().onClose(() -> closeUnchecked(reader));
161  }
162
163  @IgnoreJRERequirement // helper for lines()
164  /*
165   * If we make these calls inline inside the lambda inside lines(), we get an Animal Sniffer error,
166   * despite the @IgnoreJRERequirement annotation there. For details, see ImmutableSortedMultiset.
167   */
168  private static void closeUnchecked(Closeable closeable) {
169    try {
170      closeable.close();
171    } catch (IOException e) {
172      throw new UncheckedIOException(e);
173    }
174  }
175
176  /**
177   * Returns the size of this source in chars, if the size can be easily determined without actually
178   * opening the data stream.
179   *
180   * <p>The default implementation returns {@link Optional#absent}. Some sources, such as a {@code
181   * CharSequence}, may return a non-absent value. Note that in such cases, it is <i>possible</i>
182   * that this method will return a different number of chars than would be returned by reading all
183   * of the chars.
184   *
185   * <p>Additionally, for mutable sources such as {@code StringBuilder}s, a subsequent read may
186   * return a different number of chars if the contents are changed.
187   *
188   * @since 19.0
189   */
190  public Optional<Long> lengthIfKnown() {
191    return Optional.absent();
192  }
193
194  /**
195   * Returns the length of this source in chars, even if doing so requires opening and traversing an
196   * entire stream. To avoid a potentially expensive operation, see {@link #lengthIfKnown}.
197   *
198   * <p>The default implementation calls {@link #lengthIfKnown} and returns the value if present. If
199   * absent, it will fall back to a heavyweight operation that will open a stream, {@link
200   * Reader#skip(long) skip} to the end of the stream, and return the total number of chars that
201   * were skipped.
202   *
203   * <p>Note that for sources that implement {@link #lengthIfKnown} to provide a more efficient
204   * implementation, it is <i>possible</i> that this method will return a different number of chars
205   * than would be returned by reading all of the chars.
206   *
207   * <p>In either case, for mutable sources such as files, a subsequent read may return a different
208   * number of chars if the contents are changed.
209   *
210   * @throws IOException if an I/O error occurs while reading the length of this source
211   * @since 19.0
212   */
213  public long length() throws IOException {
214    Optional<Long> lengthIfKnown = lengthIfKnown();
215    if (lengthIfKnown.isPresent()) {
216      return lengthIfKnown.get();
217    }
218
219    Closer closer = Closer.create();
220    try {
221      Reader reader = closer.register(openStream());
222      return countBySkipping(reader);
223    } catch (Throwable e) {
224      throw closer.rethrow(e);
225    } finally {
226      closer.close();
227    }
228  }
229
230  private static long countBySkipping(Reader reader) throws IOException {
231    long count = 0;
232    long read;
233    while ((read = reader.skip(Long.MAX_VALUE)) != 0) {
234      count += read;
235    }
236    return count;
237  }
238
239  /**
240   * Appends the contents of this source to the given {@link Appendable} (such as a {@link Writer}).
241   * Does not close {@code appendable} if it is {@code Closeable}.
242   *
243   * @return the number of characters copied
244   * @throws IOException if an I/O error occurs while reading from this source or writing to {@code
245   *     appendable}
246   */
247  @CanIgnoreReturnValue
248  public long copyTo(Appendable appendable) throws IOException {
249    checkNotNull(appendable);
250
251    Closer closer = Closer.create();
252    try {
253      Reader reader = closer.register(openStream());
254      return CharStreams.copy(reader, appendable);
255    } catch (Throwable e) {
256      throw closer.rethrow(e);
257    } finally {
258      closer.close();
259    }
260  }
261
262  /**
263   * Copies the contents of this source to the given sink.
264   *
265   * @return the number of characters copied
266   * @throws IOException if an I/O error occurs while reading from this source or writing to {@code
267   *     sink}
268   */
269  @CanIgnoreReturnValue
270  public long copyTo(CharSink sink) throws IOException {
271    checkNotNull(sink);
272
273    Closer closer = Closer.create();
274    try {
275      Reader reader = closer.register(openStream());
276      Writer writer = closer.register(sink.openStream());
277      return CharStreams.copy(reader, writer);
278    } catch (Throwable e) {
279      throw closer.rethrow(e);
280    } finally {
281      closer.close();
282    }
283  }
284
285  /**
286   * Reads the contents of this source as a string.
287   *
288   * @throws IOException if an I/O error occurs while reading from this source
289   */
290  public String read() throws IOException {
291    Closer closer = Closer.create();
292    try {
293      Reader reader = closer.register(openStream());
294      return CharStreams.toString(reader);
295    } catch (Throwable e) {
296      throw closer.rethrow(e);
297    } finally {
298      closer.close();
299    }
300  }
301
302  /**
303   * Reads the first line of this source as a string. Returns {@code null} if this source is empty.
304   *
305   * <p>Like {@link BufferedReader#readLine()}, this method considers a line to be a sequence of
306   * text that is terminated by (but does not include) one of {@code \r\n}, {@code \r} or {@code
307   * \n}. If the source's content does not end in a line termination sequence, it is treated as if
308   * it does.
309   *
310   * @throws IOException if an I/O error occurs while reading from this source
311   */
312  public @Nullable String readFirstLine() throws IOException {
313    Closer closer = Closer.create();
314    try {
315      BufferedReader reader = closer.register(openBufferedStream());
316      return reader.readLine();
317    } catch (Throwable e) {
318      throw closer.rethrow(e);
319    } finally {
320      closer.close();
321    }
322  }
323
324  /**
325   * Reads all the lines of this source as a list of strings. The returned list will be empty if
326   * this source is empty.
327   *
328   * <p>Like {@link BufferedReader#readLine()}, this method considers a line to be a sequence of
329   * text that is terminated by (but does not include) one of {@code \r\n}, {@code \r} or {@code
330   * \n}. If the source's content does not end in a line termination sequence, it is treated as if
331   * it does.
332   *
333   * @throws IOException if an I/O error occurs while reading from this source
334   */
335  public ImmutableList<String> readLines() throws IOException {
336    Closer closer = Closer.create();
337    try {
338      BufferedReader reader = closer.register(openBufferedStream());
339      List<String> result = new ArrayList<>();
340      String line;
341      while ((line = reader.readLine()) != null) {
342        result.add(line);
343      }
344      return ImmutableList.copyOf(result);
345    } catch (Throwable e) {
346      throw closer.rethrow(e);
347    } finally {
348      closer.close();
349    }
350  }
351
352  /**
353   * Reads lines of text from this source, processing each line as it is read using the given {@link
354   * LineProcessor processor}. Stops when all lines have been processed or the processor returns
355   * {@code false} and returns the result produced by the processor.
356   *
357   * <p>Like {@link BufferedReader#readLine()}, this method considers a line to be a sequence of
358   * text that is terminated by (but does not include) one of {@code \r\n}, {@code \r} or {@code
359   * \n}. If the source's content does not end in a line termination sequence, it is treated as if
360   * it does.
361   *
362   * @throws IOException if an I/O error occurs while reading from this source or if {@code
363   *     processor} throws an {@code IOException}
364   * @since 16.0
365   */
366  @CanIgnoreReturnValue // some processors won't return a useful result
367  @ParametricNullness
368  public <T extends @Nullable Object> T readLines(LineProcessor<T> processor) throws IOException {
369    checkNotNull(processor);
370
371    Closer closer = Closer.create();
372    try {
373      Reader reader = closer.register(openStream());
374      return CharStreams.readLines(reader, processor);
375    } catch (Throwable e) {
376      throw closer.rethrow(e);
377    } finally {
378      closer.close();
379    }
380  }
381
382  /**
383   * Reads all lines of text from this source, running the given {@code action} for each line as it
384   * is read.
385   *
386   * <p>Like {@link BufferedReader#readLine()}, this method considers a line to be a sequence of
387   * text that is terminated by (but does not include) one of {@code \r\n}, {@code \r} or {@code
388   * \n}. If the source's content does not end in a line termination sequence, it is treated as if
389   * it does.
390   *
391   * @throws IOException if an I/O error occurs while reading from this source or if {@code action}
392   *     throws an {@code UncheckedIOException}
393   * @since 22.0 (but only since 33.4.0 in the Android flavor)
394   */
395  public void forEachLine(Consumer<? super String> action) throws IOException {
396    try (Stream<String> lines = lines()) {
397      // The lines should be ordered regardless in most cases, but use forEachOrdered to be sure
398      lines.forEachOrdered(action);
399    } catch (UncheckedIOException e) {
400      throw e.getCause();
401    }
402  }
403
404  /**
405   * Returns whether the source has zero chars. The default implementation first checks {@link
406   * #lengthIfKnown}, returning true if it's known to be zero and false if it's known to be
407   * non-zero. If the length is not known, it falls back to opening a stream and checking for EOF.
408   *
409   * <p>Note that, in cases where {@code lengthIfKnown} returns zero, it is <i>possible</i> that
410   * chars are actually available for reading. This means that a source may return {@code true} from
411   * {@code isEmpty()} despite having readable content.
412   *
413   * @throws IOException if an I/O error occurs
414   * @since 15.0
415   */
416  public boolean isEmpty() throws IOException {
417    Optional<Long> lengthIfKnown = lengthIfKnown();
418    if (lengthIfKnown.isPresent()) {
419      return lengthIfKnown.get() == 0L;
420    }
421    Closer closer = Closer.create();
422    try {
423      Reader reader = closer.register(openStream());
424      return reader.read() == -1;
425    } catch (Throwable e) {
426      throw closer.rethrow(e);
427    } finally {
428      closer.close();
429    }
430  }
431
432  /**
433   * Concatenates multiple {@link CharSource} instances into a single source. Streams returned from
434   * the source will contain the concatenated data from the streams of the underlying sources.
435   *
436   * <p>Only one underlying stream will be open at a time. Closing the concatenated stream will
437   * close the open underlying stream.
438   *
439   * @param sources the sources to concatenate
440   * @return a {@code CharSource} containing the concatenated data
441   * @since 15.0
442   */
443  public static CharSource concat(Iterable<? extends CharSource> sources) {
444    return new ConcatenatedCharSource(sources);
445  }
446
447  /**
448   * Concatenates multiple {@link CharSource} instances into a single source. Streams returned from
449   * the source will contain the concatenated data from the streams of the underlying sources.
450   *
451   * <p>Only one underlying stream will be open at a time. Closing the concatenated stream will
452   * close the open underlying stream.
453   *
454   * <p>Note: The input {@code Iterator} will be copied to an {@code ImmutableList} when this method
455   * is called. This will fail if the iterator is infinite and may cause problems if the iterator
456   * eagerly fetches data for each source when iterated (rather than producing sources that only
457   * load data through their streams). Prefer using the {@link #concat(Iterable)} overload if
458   * possible.
459   *
460   * @param sources the sources to concatenate
461   * @return a {@code CharSource} containing the concatenated data
462   * @throws NullPointerException if any of {@code sources} is {@code null}
463   * @since 15.0
464   */
465  public static CharSource concat(Iterator<? extends CharSource> sources) {
466    return concat(ImmutableList.copyOf(sources));
467  }
468
469  /**
470   * Concatenates multiple {@link CharSource} instances into a single source. Streams returned from
471   * the source will contain the concatenated data from the streams of the underlying sources.
472   *
473   * <p>Only one underlying stream will be open at a time. Closing the concatenated stream will
474   * close the open underlying stream.
475   *
476   * @param sources the sources to concatenate
477   * @return a {@code CharSource} containing the concatenated data
478   * @throws NullPointerException if any of {@code sources} is {@code null}
479   * @since 15.0
480   */
481  public static CharSource concat(CharSource... sources) {
482    return concat(ImmutableList.copyOf(sources));
483  }
484
485  /**
486   * Returns a view of the given character sequence as a {@link CharSource}. The behavior of the
487   * returned {@code CharSource} and any {@code Reader} instances created by it is unspecified if
488   * the {@code charSequence} is mutated while it is being read, so don't do that.
489   *
490   * @since 15.0 (since 14.0 as {@code CharStreams.asCharSource(String)})
491   */
492  public static CharSource wrap(CharSequence charSequence) {
493    return charSequence instanceof String
494        ? new StringCharSource((String) charSequence)
495        : new CharSequenceCharSource(charSequence);
496  }
497
498  /**
499   * Returns an immutable {@link CharSource} that contains no characters.
500   *
501   * @since 15.0
502   */
503  public static CharSource empty() {
504    return EmptyCharSource.INSTANCE;
505  }
506
507  /** A byte source that reads chars from this source and encodes them as bytes using a charset. */
508  private final class AsByteSource extends ByteSource {
509
510    final Charset charset;
511
512    AsByteSource(Charset charset) {
513      this.charset = checkNotNull(charset);
514    }
515
516    @Override
517    public CharSource asCharSource(Charset charset) {
518      if (charset.equals(this.charset)) {
519        return CharSource.this;
520      }
521      return super.asCharSource(charset);
522    }
523
524    @Override
525    public InputStream openStream() throws IOException {
526      return new ReaderInputStream(CharSource.this.openStream(), charset, 8192);
527    }
528
529    @Override
530    public String toString() {
531      return CharSource.this.toString() + ".asByteSource(" + charset + ")";
532    }
533  }
534
535  private static class CharSequenceCharSource extends CharSource {
536
537    private static final Splitter LINE_SPLITTER = Splitter.onPattern("\r\n|\n|\r");
538
539    final CharSequence seq;
540
541    CharSequenceCharSource(CharSequence seq) {
542      this.seq = checkNotNull(seq);
543    }
544
545    @Override
546    public Reader openStream() {
547      return new CharSequenceReader(seq);
548    }
549
550    @Override
551    public String read() {
552      return seq.toString();
553    }
554
555    @Override
556    public boolean isEmpty() {
557      return seq.length() == 0;
558    }
559
560    @Override
561    public long length() {
562      return seq.length();
563    }
564
565    @Override
566    public Optional<Long> lengthIfKnown() {
567      return Optional.of((long) seq.length());
568    }
569
570    /**
571     * Returns an iterator over the lines in the string. If the string ends in a newline, a final
572     * empty string is not included, to match the behavior of BufferedReader/LineReader.readLine().
573     */
574    private Iterator<String> linesIterator() {
575      return new AbstractIterator<String>() {
576        final Iterator<String> lines = LINE_SPLITTER.split(seq).iterator();
577
578        @Override
579        protected @Nullable String computeNext() {
580          if (lines.hasNext()) {
581            String next = lines.next();
582            // skip last line if it's empty
583            if (lines.hasNext() || !next.isEmpty()) {
584              return next;
585            }
586          }
587          return endOfData();
588        }
589      };
590    }
591
592    @Override
593    public Stream<String> lines() {
594      return stream(linesIterator());
595    }
596
597    @Override
598    public @Nullable String readFirstLine() {
599      Iterator<String> lines = linesIterator();
600      return lines.hasNext() ? lines.next() : null;
601    }
602
603    @Override
604    public ImmutableList<String> readLines() {
605      return ImmutableList.copyOf(linesIterator());
606    }
607
608    @Override
609    @ParametricNullness
610    public <T extends @Nullable Object> T readLines(LineProcessor<T> processor) throws IOException {
611      Iterator<String> lines = linesIterator();
612      while (lines.hasNext()) {
613        if (!processor.processLine(lines.next())) {
614          break;
615        }
616      }
617      return processor.getResult();
618    }
619
620    @Override
621    public String toString() {
622      return "CharSource.wrap(" + Ascii.truncate(seq, 30, "...") + ")";
623    }
624  }
625
626  /**
627   * Subclass specialized for string instances.
628   *
629   * <p>Since Strings are immutable and built into the jdk we can optimize some operations
630   *
631   * <ul>
632   *   <li>use {@link StringReader} instead of {@link CharSequenceReader}. It is faster since it can
633   *       use {@link String#getChars(int, int, char[], int)} instead of copying characters one by
634   *       one with {@link CharSequence#charAt(int)}.
635   *   <li>use {@link Appendable#append(CharSequence)} in {@link #copyTo(Appendable)} and {@link
636   *       #copyTo(CharSink)}. We know this is correct since strings are immutable and so the length
637   *       can't change, and it is faster because many writers and appendables are optimized for
638   *       appending string instances.
639   * </ul>
640   */
641  private static class StringCharSource extends CharSequenceCharSource {
642    StringCharSource(String seq) {
643      super(seq);
644    }
645
646    @Override
647    public Reader openStream() {
648      return new StringReader((String) seq);
649    }
650
651    @Override
652    public long copyTo(Appendable appendable) throws IOException {
653      appendable.append(seq);
654      return seq.length();
655    }
656
657    @Override
658    public long copyTo(CharSink sink) throws IOException {
659      checkNotNull(sink);
660      Closer closer = Closer.create();
661      try {
662        Writer writer = closer.register(sink.openStream());
663        writer.write((String) seq);
664        return seq.length();
665      } catch (Throwable e) {
666        throw closer.rethrow(e);
667      } finally {
668        closer.close();
669      }
670    }
671  }
672
673  private static final class EmptyCharSource extends StringCharSource {
674
675    private static final EmptyCharSource INSTANCE = new EmptyCharSource();
676
677    private EmptyCharSource() {
678      super("");
679    }
680
681    @Override
682    public String toString() {
683      return "CharSource.empty()";
684    }
685  }
686
687  private static final class ConcatenatedCharSource extends CharSource {
688
689    private final Iterable<? extends CharSource> sources;
690
691    ConcatenatedCharSource(Iterable<? extends CharSource> sources) {
692      this.sources = checkNotNull(sources);
693    }
694
695    @Override
696    public Reader openStream() throws IOException {
697      return new MultiReader(sources.iterator());
698    }
699
700    @Override
701    public boolean isEmpty() throws IOException {
702      for (CharSource source : sources) {
703        if (!source.isEmpty()) {
704          return false;
705        }
706      }
707      return true;
708    }
709
710    @Override
711    public Optional<Long> lengthIfKnown() {
712      long result = 0L;
713      for (CharSource source : sources) {
714        Optional<Long> lengthIfKnown = source.lengthIfKnown();
715        if (!lengthIfKnown.isPresent()) {
716          return Optional.absent();
717        }
718        result += lengthIfKnown.get();
719      }
720      return Optional.of(result);
721    }
722
723    @Override
724    public long length() throws IOException {
725      long result = 0L;
726      for (CharSource source : sources) {
727        result += source.length();
728      }
729      return result;
730    }
731
732    @Override
733    public String toString() {
734      return "CharSource.concat(" + sources + ")";
735    }
736  }
737}