SharedFileBackedOutputStream extends FileBackedOutputStream { @SuppressWarnings("unchecked") @Override public void cleanup() { Preconditions.checkState(usageCount.get() > 0); if (usageCount.decrementAndGet() == 0) { super.cleanup(); if (onCleanupCallback != null) { onCleanupCallback.accept(onCleanupContext); } } } SharedFileBackedOutputStream(int fileThreshold, String fileDirectory); void incrementUsageCount(); int getUsageCount(); void setOnCleanupCallback(Consumer<T> callback, T context); @SuppressWarnings("unchecked") @Override void cleanup();  }
@Test public void testSingleUsage() throws IOException { LOG.info("testSingleUsage starting"); try (SharedFileBackedOutputStream fbos = new SharedFileBackedOutputStream(5, TEMP_DIR)) { byte[] bytes = new byte[]{0, 1, 2, 3, 4, 5, 6}; fbos.write(bytes); assertNotNull("Expected temp file created", FileBackedOutputStreamTest.findTempFileName(TEMP_DIR)); fbos.cleanup(); assertNull("Found unexpected temp file", FileBackedOutputStreamTest.findTempFileName(TEMP_DIR)); } LOG.info("testSingleUsage ending"); }