Coverage Summary for Class: MultiInputStream (com.google.common.io)

Class Class, % Method, % Line, %
MultiInputStream 0% (0/1) 0% (0/8) 0% (0/37)


1 /* 2  * Copyright (C) 2007 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 java.io.IOException; 21 import java.io.InputStream; 22 import java.util.Iterator; 23 import javax.annotation.CheckForNull; 24  25 /** 26  * An {@link InputStream} that concatenates multiple substreams. At most one stream will be open at 27  * a time. 28  * 29  * @author Chris Nokleberg 30  * @since 1.0 31  */ 32 @GwtIncompatible 33 @ElementTypesAreNonnullByDefault 34 final class MultiInputStream extends InputStream { 35  36  private Iterator<? extends ByteSource> it; 37  @CheckForNull private InputStream in; 38  39  /** 40  * Creates a new instance. 41  * 42  * @param it an iterator of I/O suppliers that will provide each substream 43  */ 44  public MultiInputStream(Iterator<? extends ByteSource> it) throws IOException { 45  this.it = checkNotNull(it); 46  advance(); 47  } 48  49  @Override 50  public void close() throws IOException { 51  if (in != null) { 52  try { 53  in.close(); 54  } finally { 55  in = null; 56  } 57  } 58  } 59  60  /** Closes the current input stream and opens the next one, if any. */ 61  private void advance() throws IOException { 62  close(); 63  if (it.hasNext()) { 64  in = it.next().openStream(); 65  } 66  } 67  68  @Override 69  public int available() throws IOException { 70  if (in == null) { 71  return 0; 72  } 73  return in.available(); 74  } 75  76  @Override 77  public boolean markSupported() { 78  return false; 79  } 80  81  @Override 82  public int read() throws IOException { 83  while (in != null) { 84  int result = in.read(); 85  if (result != -1) { 86  return result; 87  } 88  advance(); 89  } 90  return -1; 91  } 92  93  @Override 94  public int read(byte[] b, int off, int len) throws IOException { 95  checkNotNull(b); 96  while (in != null) { 97  int result = in.read(b, off, len); 98  if (result != -1) { 99  return result; 100  } 101  advance(); 102  } 103  return -1; 104  } 105  106  @Override 107  public long skip(long n) throws IOException { 108  if (in == null || n <= 0) { 109  return 0; 110  } 111  long result = in.skip(n); 112  if (result != 0) { 113  return result; 114  } 115  if (read() == -1) { 116  return 0; 117  } 118  return 1 + in.skip(n - 1); 119  } 120 }