Skip to content

Commit

Permalink
Merge pull request #3410 from jamezp/RESTEASY-3286-3.15
Browse files Browse the repository at this point in the history
[RESTEASY-3286] Use the new NIO file utilities to create temporary files
  • Loading branch information
jamezp committed Feb 2, 2023
2 parents feb2ac1 + 23993b9 commit 5e2e668
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 64 deletions.
Expand Up @@ -13,14 +13,14 @@

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.SequenceInputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.nio.file.Files;
import java.nio.file.Path;

import javax.activation.DataSource;
import javax.ws.rs.Consumes;
Expand All @@ -44,11 +44,16 @@ protected static class SequencedDataSource implements DataSource
private final byte[] byteBuffer;
private final int byteBufferOffset;
private final int byteBufferLength;
private final File tempFile;
private final Path tempFile;
private final String type;

protected SequencedDataSource(final byte[] byteBuffer, final int byteBufferOffset,
final int byteBufferLength, final File tempFile, final String type)
final int byteBufferLength, final File tempFile, final String type) {
this(byteBuffer, byteBufferOffset, byteBufferLength, tempFile.toPath(), type);
}

protected SequencedDataSource(final byte[] byteBuffer, final int byteBufferOffset,
final int byteBufferLength, final Path tempFile, final String type)
{
super();
this.byteBuffer = byteBuffer;
Expand All @@ -70,8 +75,7 @@ public InputStream getInputStream() throws IOException
InputStream bis = new ByteArrayInputStream(byteBuffer, byteBufferOffset, byteBufferLength);
if (tempFile == null)
return bis;
InputStream fis = new FileInputStream(tempFile);
return new SequenceInputStream(bis, fis);
return new SequenceInputStream(bis, Files.newInputStream(tempFile));
}

@Override
Expand Down Expand Up @@ -100,28 +104,22 @@ public static DataSource readDataSource(final InputStream in, final MediaType me
byte[] memoryBuffer = new byte[4096];
int readCount = in.read(memoryBuffer, 0, memoryBuffer.length);

File tempFile = null;
Path tempFile = null;
if (readCount > 0)
{
byte[] buffer = new byte[4096];
int count = in.read(buffer, 0, buffer.length);
if (count > -1) {
tempFile = File.createTempFile("resteasy-provider-datasource", null);
tempFile = Files.createTempFile("resteasy-provider-datasource", null);
Cleanables cleanables = ResteasyProviderFactory.getContextData(Cleanables.class);
if (cleanables != null)
{
cleanables.addCleanable(new TempFileCleanable(tempFile));
}
FileOutputStream fos = new FileOutputStream(tempFile);
fos.write(buffer, 0, count);
try
{
try (OutputStream fos = Files.newOutputStream(tempFile)) {
fos.write(buffer, 0, count);
ProviderHelper.writeTo(in, fos);
}
finally
{
fos.close();
}
}
}

Expand Down Expand Up @@ -280,21 +278,15 @@ public void writeTo(DataSource dataSource,

private static class TempFileCleanable implements Cleanable {

private File tempFile;
private final Path tempFile;

TempFileCleanable(final File tempFile) {
TempFileCleanable(final Path tempFile) {
this.tempFile = tempFile;
}

@Override
public void clean() throws Exception {
if(tempFile.exists())
{
if (!tempFile.delete()) //set delete on exit only if the file can't be deleted now
{
tempFile.deleteOnExit();
}
}
Files.deleteIfExists(tempFile);
}
}
}
Expand Up @@ -18,15 +18,16 @@
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
* @author <a href="mailto:mlittle@redhat.com">Mark Little</a>
Expand Down Expand Up @@ -61,14 +62,13 @@ public File readFrom(Class<File> type, Type genericType,
throws IOException
{
LogMessages.LOGGER.debugf("Provider : %s, Method : readFrom", getClass().getName());
File downloadedFile = null;
Path downloadedFile = null;

if (_downloadDirectory != null)
{
try
{
downloadedFile = File.createTempFile(PREFIX, SUFFIX, new File(
_downloadDirectory));
downloadedFile = Files.createTempFile(Paths.get(_downloadDirectory), PREFIX, SUFFIX);
}
catch (final IOException ex)
{
Expand All @@ -79,7 +79,7 @@ public File readFrom(Class<File> type, Type genericType,
}

if (downloadedFile == null)
downloadedFile = File.createTempFile(PREFIX, SUFFIX);
downloadedFile = Files.createTempFile(PREFIX, SUFFIX);

Cleanables cleanables = ResteasyProviderFactory.getContextData(Cleanables.class);
if (cleanables != null)
Expand All @@ -88,23 +88,17 @@ public File readFrom(Class<File> type, Type genericType,
}
else
{
LogMessages.LOGGER.temporaryFileCreated(downloadedFile.getPath());
LogMessages.LOGGER.temporaryFileCreated(downloadedFile.toString());
}

if (NoContent.isContentLengthZero(httpHeaders)) return downloadedFile;
OutputStream output = new BufferedOutputStream(new FileOutputStream(
downloadedFile));
if (NoContent.isContentLengthZero(httpHeaders)) return downloadedFile.toFile();

try
try (OutputStream output = Files.newOutputStream(downloadedFile))
{
ProviderHelper.writeTo(entityStream, output);
}
finally
{
output.close();
}

return downloadedFile;
return downloadedFile.toFile();
}

public boolean isWriteable(Class<?> type, Type genericType,
Expand Down Expand Up @@ -221,17 +215,17 @@ protected void writeIt(File uploadFile, OutputStream entityStream) throws IOExce

private static class FileHolder implements Cleanable
{
File file;
Path file;

FileHolder(final File file)
FileHolder(final Path file)
{
this.file = file;
}

@Override
public void clean() throws Exception
{
file.delete();
Files.deleteIfExists(file);
}
}
}
Expand Up @@ -20,6 +20,7 @@
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.nio.file.Files;
import java.util.HashSet;
import java.util.Set;

Expand Down Expand Up @@ -102,7 +103,7 @@ public void testDeploymentInfo() throws Exception
@Test
public void testAddResourcePrefixPath() throws Exception
{
File staticFile = File.createTempFile("tmp", "index.html");
File staticFile = Files.createTempFile("tmp", "index.html").toFile();
staticFile.deleteOnExit();
BufferedWriter writer = new BufferedWriter( new FileWriter( staticFile ) );
final String staticFileContent = "Hello static world!";
Expand Down
Expand Up @@ -4,6 +4,7 @@
import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.nio.file.Files;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
Expand Down Expand Up @@ -86,7 +87,7 @@ public void testXOPGet() throws Exception {
*/
@Test
public void testXOPSend() throws Exception {
File tmpFile = File.createTempFile("pre", ".tmp");
File tmpFile = Files.createTempFile("pre", ".tmp").toFile();
tmpFile.deleteOnExit();
Writer writer = new FileWriter(tmpFile);
writer.write("testXOPSend");
Expand Down
Expand Up @@ -4,6 +4,7 @@
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;

import javax.activation.DataHandler;
import javax.activation.DataSource;
Expand All @@ -28,7 +29,7 @@ public Response putFile(XOPMultipartProxyPutFileRequest putFileRequest) throws I

private XOPMultipartProxyGetFileResponse getResponse(String content) throws Exception {
XOPMultipartProxyGetFileResponse response = new XOPMultipartProxyGetFileResponse();
File out = File.createTempFile("tmp", ".txt");
File out = Files.createTempFile("tmp", ".txt").toFile();
out.deleteOnExit();
try (FileWriter writer = new FileWriter(out)) {
writer.write(content);
Expand Down
Expand Up @@ -3,9 +3,10 @@
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;

@Path("/")
public class RangeResource {
Expand Down Expand Up @@ -49,34 +50,32 @@ public void deleteSmallFile() throws Exception {
}

private static File createFile() {
File file = null;
java.nio.file.Path file = null;
try {
file = File.createTempFile("tmp", "tmp");
FileOutputStream fos = new FileOutputStream(file);
for (int i = 0; i < 1000; i++) {
fos.write("hello".getBytes());
file = Files.createTempFile("tmp", "tmp");
try (BufferedWriter writer = Files.newBufferedWriter(file)) {
for (int i = 0; i < 1000; i++) {
writer.write("hello");
}
writer.write("1234");
}
fos.write("1234".getBytes());
fos.flush();
fos.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
return file;
return file.toFile();
}

private static File createSmallFile() {
File smallfile = null;
java.nio.file.Path smallfile = null;
try {
smallfile = File.createTempFile("smalltmp", "tmp");
FileOutputStream fos = new FileOutputStream(smallfile);
fos.write("123456789".getBytes());
fos.flush();
fos.close();
smallfile = Files.createTempFile("smalltmp", "tmp");
try (BufferedWriter writer = Files.newBufferedWriter(smallfile)) {
writer.write("123456789");
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return smallfile;
return smallfile.toFile();
}

}

0 comments on commit 5e2e668

Please sign in to comment.