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