Skip to content

Commit

Permalink
[RESTEASY-1080] DataSourceProvider does not delete the temporary file…
Browse files Browse the repository at this point in the history
…s it creates
  • Loading branch information
kylape committed Jul 24, 2014
1 parent 1247f0b commit 854ec43
Showing 1 changed file with 33 additions and 2 deletions.
Expand Up @@ -43,7 +43,7 @@ protected static class SequencedDataSource implements DataSource
private final File tempFile;
private final String type;

public SequencedDataSource(byte[] byteBuffer, int byteBufferOffset,
protected SequencedDataSource(byte[] byteBuffer, int byteBufferOffset,
int byteBufferLength, File tempFile, String type)
{
super();
Expand All @@ -67,7 +67,37 @@ public InputStream getInputStream() throws IOException
if (tempFile == null)
return bis;
InputStream fis = new FileInputStream(tempFile);
return new SequenceInputStream(bis, fis);
return new SequenceInputStream(bis, fis) {
@Override
public int read(byte[] b, int off, int len) throws IOException
{
int r = super.read(b, off, len);
if(r == -1)
{
deleteTempFile();
}
return r;
}

@Override
public int read() throws IOException
{
int r = super.read();
if(r == -1)
{
deleteTempFile();
}
return r;
}
};
}

private void deleteTempFile()
{
if(tempFile.exists())
{
tempFile.delete();
}
}

@Override
Expand Down Expand Up @@ -103,6 +133,7 @@ public static DataSource readDataSource(final InputStream in, final MediaType me
int count = in.read(buffer, 0, buffer.length);
if (count > -1) {
tempFile = File.createTempFile("resteasy-provider-datasource", null);
tempFile.deleteOnExit();
FileOutputStream fos = new FileOutputStream(tempFile);
fos.write(buffer, 0, count);
try
Expand Down

0 comments on commit 854ec43

Please sign in to comment.