KeepOpenInputStream extends FilterInputStream { public static void closeStream(InputStream is) { try { if (is instanceof KeepOpenInputStream) { ((KeepOpenInputStream) is).doClose(); } else { is.close(); } } catch (IOException e) { LOG.warn("Failed to close input stream", e); } } KeepOpenInputStream(InputStream is); @Override void close(); void doClose(); static void closeStream(InputStream is);  }
@Test public void testCloseStream() throws IOException { when(is.markSupported()).thenReturn(true); InputStream is2 = mock(InputStream.class); KeepOpenInputStream keepOpenInputStream = new KeepOpenInputStream(is); verify(is).markSupported(); KeepOpenInputStream.closeStream(is2); verify(is2).close(); KeepOpenInputStream.closeStream(keepOpenInputStream); verify(is).close(); verifyNoMoreInteractions(is, is2); }