• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 The Guava Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.common.io;
18 
19 import static com.google.common.base.Preconditions.checkNotNull;
20 import static com.google.common.io.TestOption.CLOSE_THROWS;
21 import static com.google.common.io.TestOption.OPEN_THROWS;
22 import static com.google.common.io.TestOption.READ_THROWS;
23 import static com.google.common.io.TestOption.SKIP_THROWS;
24 
25 import com.google.common.collect.ImmutableSet;
26 import java.io.FilterInputStream;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.util.Arrays;
30 
31 /** @author Colin Decker */
32 public class TestInputStream extends FilterInputStream {
33 
34   private final ImmutableSet<TestOption> options;
35   private boolean closed;
36 
TestInputStream(InputStream in, TestOption... options)37   public TestInputStream(InputStream in, TestOption... options) throws IOException {
38     this(in, Arrays.asList(options));
39   }
40 
TestInputStream(InputStream in, Iterable<TestOption> options)41   public TestInputStream(InputStream in, Iterable<TestOption> options) throws IOException {
42     super(checkNotNull(in));
43     this.options = ImmutableSet.copyOf(options);
44     throwIf(OPEN_THROWS);
45   }
46 
closed()47   public boolean closed() {
48     return closed;
49   }
50 
51   @Override
read()52   public int read() throws IOException {
53     throwIf(closed);
54     throwIf(READ_THROWS);
55     return in.read();
56   }
57 
58   @Override
read(byte[] b, int off, int len)59   public int read(byte[] b, int off, int len) throws IOException {
60     throwIf(closed);
61     throwIf(READ_THROWS);
62     return in.read(b, off, len);
63   }
64 
65   @Override
skip(long n)66   public long skip(long n) throws IOException {
67     throwIf(closed);
68     throwIf(SKIP_THROWS);
69     return in.skip(n);
70   }
71 
72   @Override
available()73   public int available() throws IOException {
74     throwIf(closed);
75     return options.contains(TestOption.AVAILABLE_ALWAYS_ZERO) ? 0 : in.available();
76   }
77 
78   @Override
close()79   public void close() throws IOException {
80     closed = true;
81     throwIf(CLOSE_THROWS);
82     in.close();
83   }
84 
throwIf(TestOption option)85   private void throwIf(TestOption option) throws IOException {
86     throwIf(options.contains(option));
87   }
88 
throwIf(boolean condition)89   private static void throwIf(boolean condition) throws IOException {
90     if (condition) {
91       throw new IOException();
92     }
93   }
94 }
95