Coverage Summary for Class: MultiReader (com.google.common.io)
| Class | Class, % | Method, % | Line, % |
|---|---|---|---|
| MultiReader | 0% (0/1) | 0% (0/6) | 0% (0/28) |
1 /* 2 * Copyright (C) 2008 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 15 package com.google.common.io; 16 17 import static com.google.common.base.Preconditions.checkNotNull; 18 19 import com.google.common.annotations.GwtIncompatible; 20 import com.google.common.base.Preconditions; 21 import java.io.IOException; 22 import java.io.Reader; 23 import java.util.Iterator; 24 import javax.annotation.CheckForNull; 25 26 /** 27 * A {@link Reader} that concatenates multiple readers. 28 * 29 * @author Bin Zhu 30 * @since 1.0 31 */ 32 @GwtIncompatible 33 @ElementTypesAreNonnullByDefault 34 class MultiReader extends Reader { 35 private final Iterator<? extends CharSource> it; 36 @CheckForNull private Reader current; 37 38 MultiReader(Iterator<? extends CharSource> readers) throws IOException { 39 this.it = readers; 40 advance(); 41 } 42 43 /** Closes the current reader and opens the next one, if any. */ 44 private void advance() throws IOException { 45 close(); 46 if (it.hasNext()) { 47 current = it.next().openStream(); 48 } 49 } 50 51 @Override 52 public int read(char[] cbuf, int off, int len) throws IOException { 53 checkNotNull(cbuf); 54 if (current == null) { 55 return -1; 56 } 57 int result = current.read(cbuf, off, len); 58 if (result == -1) { 59 advance(); 60 return read(cbuf, off, len); 61 } 62 return result; 63 } 64 65 @Override 66 public long skip(long n) throws IOException { 67 Preconditions.checkArgument(n >= 0, "n is negative"); 68 if (n > 0) { 69 while (current != null) { 70 long result = current.skip(n); 71 if (result > 0) { 72 return result; 73 } 74 advance(); 75 } 76 } 77 return 0; 78 } 79 80 @Override 81 public boolean ready() throws IOException { 82 return (current != null) && current.ready(); 83 } 84 85 @Override 86 public void close() throws IOException { 87 if (current != null) { 88 try { 89 current.close(); 90 } finally { 91 current = null; 92 } 93 } 94 } 95 }