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