Skip to content

Commit

Permalink
Made RqMultipart.Base delete its temporary file when its body is closed.
Browse files Browse the repository at this point in the history
For yegor256#254

I thus added a decorator to InputStream to permit deletion of the temp file
when calling close() as advised by @dmzaytsev

Thanks to @yegor256 and @dmzaytsev for the hints.
  • Loading branch information
Yohann Ferreira committed May 29, 2015
1 parent dcfd545 commit 0223786
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/main/java/org/takes/rq/RqMultipart.java
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ private ConcurrentMap<String, List<Request>> buildRequests(
/**
* Make a request.
* Scans the origin request until the boundary reached. Caches
* the content into a temporary file and returns it as a new request.
* the content into a temporary file and returns it as a new request.
* @param boundary Boundary
* @return Request
* @throws IOException If fails
Expand All @@ -235,7 +235,6 @@ private Request make(final byte[] boundary) throws IOException {
final File file = File.createTempFile(
RqMultipart.class.getName(), ".tmp"
);
file.deleteOnExit();
final FileChannel channel = new RandomAccessFile(
file,
"rw"
Expand All @@ -251,9 +250,12 @@ private Request make(final byte[] boundary) throws IOException {
channel.close();
}
return new RqLive(
new CapInputStream(
new FileInputStream(file),
file.length()
new TempInputStream(
new CapInputStream(
new FileInputStream(file),
file.length()
),
file
)
);
}
Expand Down
98 changes: 98 additions & 0 deletions src/main/java/org/takes/rq/TempInputStream.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2015 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.takes.rq;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
* Input stream using a temporary cache file.
*
* <p>All implementations of this interface must be immutable and thread-safe.
*
* @author Yegor Bugayenko (yegor@teamed.io)
* @version $Id$
* @since 0.16
*/
final class TempInputStream extends InputStream {

/**
* Original stream.
*/
private final transient InputStream origin;

/**
* Temporary file used as a cache.
*/
private final transient File file;

/**
* Ctor.
* @param stream Original stream
* @param temp Temporary file used as a cache.
*/
TempInputStream(final InputStream stream, final File temp) {
super();
this.origin = stream;
this.file = temp;
}

/**
* Closes the Input stream, deleting the now useless temporary file.
* @throws IOException if some problem occurs.
*/
@Override
public void close() throws IOException {
super.close();
this.origin.close();
this.file.delete();
}

@Override
public int read() throws IOException {
return this.origin.read();
}

@Override
public int read(final byte[] buf) throws IOException {
return this.origin.read(buf, 0, buf.length);
}

@Override
public int read(final byte[] buf, final int off,
final int len) throws IOException {
return this.origin.read(buf, off, len);
}

@Override
public long skip(final long num) throws IOException {
return this.origin.skip(num);
}

@Override
public int available() throws IOException {
return this.origin.available();
}
}
28 changes: 28 additions & 0 deletions src/test/java/org/takes/rq/RqMultipartTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URI;
import java.util.Arrays;
Expand Down Expand Up @@ -368,4 +369,31 @@ public void handlesRequestInTime() throws IOException {
temp.delete();
}

/**
* Test the TempInputStream class for correct deletion of the temp file,
* as used in multipart requests.
* @throws IOException if some problem occurs.
*/
@Test
public void testTempFileDeletion() throws IOException {
final File file = File.createTempFile("tempfile", ".tmp");
final BufferedWriter out = new BufferedWriter(new FileWriter(file));
out.write("takes is fun!\n");
out.write("Temp file deletion test.\n");
out.close();
final InputStream body = new TempInputStream(
new FileInputStream(file), file
);
MatcherAssert.assertThat(
"File exists.",
file.exists(),
Matchers.equalTo(true)
);
body.close();
MatcherAssert.assertThat(
"File doesn't exist anymore.",
file.exists(),
Matchers.equalTo(false)
);
}
}

0 comments on commit 0223786

Please sign in to comment.