View Javadoc
1   /*
2    * Portions of this software was developed by employees of the National Institute
3    * of Standards and Technology (NIST), an agency of the Federal Government and is
4    * being made available as a public service. Pursuant to title 17 United States
5    * Code Section 105, works of NIST employees are not subject to copyright
6    * protection in the United States. This software may be subject to foreign
7    * copyright. Permission in the United States and in foreign countries, to the
8    * extent that NIST may hold copyright, to use, copy, modify, create derivative
9    * works, and distribute this software and its documentation without fee is hereby
10   * granted on a non-exclusive basis, provided that this notice and disclaimer
11   * of warranty appears in all copies.
12   *
13   * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER
14   * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY
15   * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
16   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM
17   * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE
18   * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE.  IN NO EVENT
19   * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT,
20   * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM,
21   * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
22   * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR
23   * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT
24   * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
25   */
26  
27  package gov.nist.secauto.metaschema.databind.test.util;
28  
29  import java.io.IOException;
30  import java.io.InputStream;
31  import java.io.OutputStream;
32  
33  import edu.umd.cs.findbugs.annotations.NonNull;
34  
35  public class CloseDetectingInputStream
36      extends InputStream {
37  
38    private final InputStream delegate;
39    private boolean closed;
40  
41    /**
42     * Create a new input stream that will proxy calls to the provided
43     * {@code delegate}.
44     *
45     * @param delegate
46     *          the underlying input stream
47     */
48    public CloseDetectingInputStream(@NonNull InputStream delegate) {
49      this.delegate = delegate;
50    }
51  
52    /**
53     * Indicates if {@link #close()} has been called.
54     *
55     * @return {@code true} if {@link #close()} has been called, or {@code false}
56     *         otherwise
57     */
58    public boolean isClosed() {
59      return closed;
60    }
61  
62    @Override
63    public int read() throws IOException {
64      return delegate.read();
65    }
66  
67    @Override
68    public int read(byte[] byteArray) throws IOException {
69      return delegate.read(byteArray);
70    }
71  
72    @Override
73    public int read(byte[] byteArray, int off, int len) throws IOException {
74      return delegate.read(byteArray, off, len);
75    }
76  
77    @Override
78    public byte[] readAllBytes() throws IOException {
79      return delegate.readAllBytes();
80    }
81  
82    @Override
83    public byte[] readNBytes(int len) throws IOException {
84      return delegate.readNBytes(len);
85    }
86  
87    @Override
88    public int readNBytes(byte[] byteArray, int off, int len) throws IOException {
89      return delegate.readNBytes(byteArray, off, len);
90    }
91  
92    @Override
93    public long skip(long numBytes) throws IOException {
94      return delegate.skip(numBytes);
95    }
96  
97    @Override
98    public int available() throws IOException {
99      return delegate.available();
100   }
101 
102   @Override
103   public void close() throws IOException {
104     delegate.close();
105     closed = true;
106   }
107 
108   @Override
109   public synchronized void mark(int readlimit) {
110     delegate.mark(readlimit);
111   }
112 
113   @Override
114   public synchronized void reset() throws IOException {
115     delegate.reset();
116   }
117 
118   @Override
119   public boolean markSupported() {
120     return delegate.markSupported();
121   }
122 
123   @Override
124   public long transferTo(OutputStream out) throws IOException {
125     return delegate.transferTo(out);
126   }
127 
128   @Override
129   public int hashCode() {
130     return delegate.hashCode();
131   }
132 
133   @Override
134   public boolean equals(Object obj) {
135     return delegate.equals(obj);
136   }
137 
138   @Override
139   public String toString() {
140     return delegate.toString();
141   }
142 
143 }