/** * Finalizes this object prior to garbage collection. The * {@code close} method is called to close any open input * source. This method should not be called from application * code. * * @exception Throwable if an error occurs during superclass * finalization. */ @Override protected void finalize() throws Throwable { if (!closed) { try { close(); } catch (IOException ignore) { // Ignroe } } super.finalize(); } }
@Test public void testCloseUnderlyingStream() throws IOException { final boolean[] closed = new boolean[1]; ByteArrayInputStream input = new ByteArrayInputStream(makeRandomArray(256)) { @Override public void close() throws IOException { closed[0] = true; super.close(); } }; SeekableInputStream stream = makeInputStream(input); try { FileUtil.read(stream); // Read until EOF assertEquals("EOF not reached (test case broken)", -1, stream.read()); assertFalse("Underlying stream closed before close", closed[0]); } finally { stream.close(); } assertTrue("Underlying stream not closed", closed[0]); }
/** * Finalizes this object prior to garbage collection. The * {@code close} method is called to close any open input * source. This method should not be called from application * code. * * @exception Throwable if an error occurs during superclass * finalization. */ @Override protected void finalize() throws Throwable { if (!closed) { try { close(); } catch (IOException ignore) { // Ignroe } } super.finalize(); } }
/** * Finalizes this object prior to garbage collection. The * {@code close} method is called to close any open input * source. This method should not be called from application * code. * * @exception Throwable if an error occurs during superclass * finalization. */ @Override protected void finalize() throws Throwable { if (!mClosed) { try { close(); } catch (IOException ignore) { // Ignroe } } super.finalize(); } }
public void testCloseUnderlyingStream() throws IOException { final boolean[] closed = new boolean[1]; ByteArrayInputStream input = new ByteArrayInputStream(makeRandomArray(256)) { @Override public void close() throws IOException { closed[0] = true; super.close(); } }; SeekableInputStream stream = makeInputStream(input); try { FileUtil.read(stream); // Read until EOF assertEquals("EOF not reached (test case broken)", -1, stream.read()); assertFalse("Underlying stream closed before close", closed[0]); } finally { stream.close(); } assertTrue("Underlying stream not closed", closed[0]); }
@Test public void testCloseUnderlyingStream() throws IOException { final boolean[] closed = new boolean[1]; ByteArrayInputStream input = new ByteArrayInputStream(makeRandomArray(256)) { @Override public void close() throws IOException { closed[0] = true; super.close(); } }; SeekableInputStream stream = makeInputStream(input); try { FileUtil.read(stream); // Read until EOF assertEquals("EOF not reached (test case broken)", -1, stream.read()); assertFalse("Underlying stream closed before close", closed[0]); } finally { stream.close(); } assertTrue("Underlying stream not closed", closed[0]); }