Call FileBackedOutputStream.reset on InputStream.close

This ensures that we delete temporary files created by Wire logging.
This commit is contained in:
Andrew Gaul 2013-03-13 10:43:41 -07:00
parent 10827445f7
commit 2d44332559
1 changed files with 10 additions and 3 deletions

View File

@ -26,6 +26,7 @@ import java.io.ByteArrayInputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FilterInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -89,14 +90,20 @@ public abstract class Wire {
public InputStream copy(final String header, InputStream instream) { public InputStream copy(final String header, InputStream instream) {
int limit = 256 * 1024; int limit = 256 * 1024;
FileBackedOutputStream out = null; final FileBackedOutputStream out = new FileBackedOutputStream(limit);
try { try {
out = new FileBackedOutputStream(limit);
long bytesRead = ByteStreams.copy(instream, out); long bytesRead = ByteStreams.copy(instream, out);
if (bytesRead >= limit) if (bytesRead >= limit)
logger.debug("over limit %d/%d: wrote temp file", bytesRead, limit); logger.debug("over limit %d/%d: wrote temp file", bytesRead, limit);
wire(header, out.getSupplier().getInput()); wire(header, out.getSupplier().getInput());
return out.getSupplier().getInput(); // we must call FileBackedOutputStream.reset to remove temporary file
return new FilterInputStream(out.getSupplier().getInput()) {
@Override
public void close() throws IOException {
super.close();
out.reset();
}
};
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException("Error tapping line", e); throw new RuntimeException("Error tapping line", e);
} finally { } finally {