Skip to content

Commit

Permalink
[RESTEASY-3286] Use the new file utilities to create temporary files.
Browse files Browse the repository at this point in the history
https://issues.redhat.com/browse/RESTEASY-3286
Signed-off-by: James R. Perkins <jperkins@redhat.com>
  • Loading branch information
jamezp committed Feb 3, 2023
1 parent fcbd0d4 commit 277ea6d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import java.io.BufferedInputStream;
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.nio.file.Files;
import java.nio.file.Path;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
Expand Down Expand Up @@ -143,17 +143,17 @@ private static class CustomTempFileStorageProvider extends AbstractStorageProvid

public StorageOutputStream createStorageOutputStream() throws IOException
{
return new TempFileStorageOutputStream(createTempFile(prefix, suffix, directory));
return new TempFileStorageOutputStream(createTempFile(prefix, suffix, directory.toPath()));
}

private static File createTempFile(String prefix, String suffix, File directory) throws IOException
private static Path createTempFile(String prefix, String suffix, Path directory) throws IOException
{
boolean java2SecurityEnabled = System.getSecurityManager() != null;
if (java2SecurityEnabled)
{
try {
return AccessController.doPrivileged((PrivilegedExceptionAction<File>) () ->
File.createTempFile(prefix, suffix, directory));
return AccessController.doPrivileged((PrivilegedExceptionAction<Path>) () ->
Files.createTempFile(directory, prefix, suffix));
} catch (PrivilegedActionException pae) {
Throwable cause = pae.getCause();
if (cause instanceof IOException)
Expand All @@ -162,17 +162,17 @@ private static File createTempFile(String prefix, String suffix, File directory)
} else throw new RuntimeException(cause);
}
}
return File.createTempFile(prefix, suffix, directory);
return Files.createTempFile(directory, prefix, suffix);
}

private static FileOutputStream createFileOutputStream(File file) throws IOException
private static OutputStream createFileOutputStream(final Path file) throws IOException
{
boolean java2SecurityEnabled = System.getSecurityManager() != null;
if (java2SecurityEnabled)
{
try {
return AccessController.doPrivileged((PrivilegedExceptionAction<FileOutputStream>) () ->
new FileOutputStream(file));
return AccessController.doPrivileged((PrivilegedExceptionAction<OutputStream>) () ->
Files.newOutputStream(file));
} catch (PrivilegedActionException pae) {
Throwable cause = pae.getCause();
if (cause instanceof IOException)
Expand All @@ -181,16 +181,16 @@ private static FileOutputStream createFileOutputStream(File file) throws IOExcep
} else throw new RuntimeException(cause);
}
}
return new FileOutputStream(file);
return Files.newOutputStream(file);
}

private static final class TempFileStorageOutputStream extends StorageOutputStream
{
private File file;
private final Path file;

private OutputStream out;
private final OutputStream out;

TempFileStorageOutputStream(final File file) throws IOException
TempFileStorageOutputStream(final Path file) throws IOException
{
this.file = file;
this.out = createFileOutputStream(file);
Expand Down Expand Up @@ -220,11 +220,11 @@ protected Storage toStorage0() throws IOException
private static final class TempFileStorage implements Storage
{

private File file;
private Path file;

private static final Set<File> filesToDelete = new HashSet<File>();
private static final Set<Path> filesToDelete = new HashSet<>();

TempFileStorage(final File file)
TempFileStorage(final Path file)
{
this.file = file;
}
Expand All @@ -247,12 +247,14 @@ public void delete()
file = null;
}

for (Iterator<File> iterator = filesToDelete.iterator(); iterator.hasNext();)
for (Iterator<Path> iterator = filesToDelete.iterator(); iterator.hasNext();)
{
File f = iterator.next();
if (f.delete())
{
Path f = iterator.next();
try {
Files.deleteIfExists(f);
iterator.remove();
} catch (IOException e) {
LogMessages.LOGGER.debugf(e, "Failed to delete file %s", f);
}
}
}
Expand All @@ -263,7 +265,7 @@ public InputStream getInputStream() throws IOException
if (file == null)
throw new IllegalStateException("storage has been deleted");

return new BufferedInputStream(new FileInputStream(file));
return new BufferedInputStream(Files.newInputStream(file));
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,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 java.util.concurrent.CompletionStage;

import javax.activation.DataSource;
Expand Down Expand Up @@ -46,11 +46,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 @@ -72,9 +77,7 @@ public InputStream getInputStream() throws IOException
InputStream bis = new ByteArrayInputStream(byteBuffer, byteBufferOffset, byteBufferLength);
if (tempFile == null)
return bis;
@SuppressWarnings("resource")
InputStream fis = new FileInputStream(tempFile);
return new SequenceInputStream(bis, fis);
return new SequenceInputStream(bis, Files.newInputStream(tempFile));
}

@Override
Expand Down Expand Up @@ -103,28 +106,21 @@ 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);
FileOutputStream fos = new FileOutputStream(tempFile);
Cleanables cleanables = ResteasyContext.getContextData(Cleanables.class);
if (cleanables != null)
{
cleanables.addCleanable(new TempFileCleanable(tempFile));
}
fos.write(buffer, 0, count);
try
{
tempFile = Files.createTempFile("resteasy-provider-datasource", null);
try (OutputStream fos = Files.newOutputStream(tempFile)) {
Cleanables cleanables = ResteasyContext.getContextData(Cleanables.class);
if (cleanables != null) {
cleanables.addCleanable(new TempFileCleanable(tempFile));
}
fos.write(buffer, 0, count);
ProviderHelper.writeTo(in, fos);
}
finally
{
fos.close();
}
}
}

Expand Down Expand Up @@ -299,21 +295,15 @@ public CompletionStage<Void> asyncWriteTo(DataSource dataSource, Class<?> type,

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);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package org.jboss.resteasy.plugins.providers;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
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;
import java.util.concurrent.CompletionStage;

import javax.ws.rs.Consumes;
Expand Down Expand Up @@ -62,14 +63,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 @@ -80,7 +80,7 @@ public File readFrom(Class<File> type, Type genericType,
}

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

Cleanables cleanables = ResteasyContext.getContextData(Cleanables.class);
if (cleanables != null)
Expand All @@ -89,23 +89,15 @@ 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));

try
{
if (NoContent.isContentLengthZero(httpHeaders)) return downloadedFile.toFile();
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 @@ -315,17 +307,17 @@ protected CompletionStage<Void> writeIt(File uploadFile, AsyncOutputStream entit

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

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

@Override
public void clean() throws Exception
{
file.delete();
Files.deleteIfExists(file);
}
}
}

0 comments on commit 277ea6d

Please sign in to comment.