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