Skip to content
Permalink
Browse files
Merge pull request #69 from arturobernalg/feature/FILEUPLOAD-318
FILEUPLOAD-318 - Use try with resource
  • Loading branch information
jochenw committed Apr 30, 2021
2 parents 849ae27 + b33dda7 commit 68dbfbc80b20aefa52c8f29d23512d4366f73924
Showing 5 changed files with 51 additions and 43 deletions.
@@ -20,6 +20,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UncheckedIOException;
import java.io.UnsupportedEncodingException;

/**
@@ -104,8 +105,10 @@ public interface FileItem extends FileItemHeadersSupport {
* Returns the contents of the file item as an array of bytes.
*
* @return The contents of the file item as an array of bytes.
*
* @throws UncheckedIOException if an I/O error occurs
*/
byte[] get();
byte[] get() throws UncheckedIOException;

/**
* Returns the contents of the file item as a String, using the specified
@@ -118,8 +121,9 @@ public interface FileItem extends FileItemHeadersSupport {
*
* @throws UnsupportedEncodingException if the requested character
* encoding is not available.
* @throws IOException if an I/O error occurs
*/
String getString(String encoding) throws UnsupportedEncodingException;
String getString(String encoding) throws UnsupportedEncodingException, IOException;

/**
* Returns the contents of the file item as a String, using the default
@@ -23,6 +23,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UncheckedIOException;
import java.io.UnsupportedEncodingException;
import java.nio.file.Files;
import java.util.Map;
@@ -292,12 +293,14 @@ public long getSize() {
* Returns the contents of the file as an array of bytes. If the
* contents of the file were not yet cached in memory, they will be
* loaded from the disk storage and cached.
*
* @throws UncheckedIOException if an I/O error occurs
* @return The contents of the file as an array of bytes
* or {@code null} if the data cannot be read
*
* @throws UncheckedIOException if an I/O error occurs
*/
@Override
public byte[] get() {
public byte[] get() throws UncheckedIOException {
if (isInMemory()) {
if (cachedContent == null && dfos != null) {
cachedContent = dfos.getData();
@@ -306,17 +309,12 @@ public byte[] get() {
}

byte[] fileData = new byte[(int) getSize()];
InputStream fis = null;

try {
fis = Files.newInputStream(dfos.getFile().toPath());
try (InputStream fis = Files.newInputStream(dfos.getFile().toPath())) {
IOUtils.readFully(fis, fileData);
} catch (final IOException e) {
fileData = null;
} finally {
IOUtils.closeQuietly(fis);
} catch (IOException e) {
throw new UncheckedIOException(e);
}

return fileData;
}

@@ -334,7 +332,7 @@ public byte[] get() {
*/
@Override
public String getString(final String charset)
throws UnsupportedEncodingException {
throws UnsupportedEncodingException, IOException {
return new String(get(), charset);
}

@@ -349,14 +347,15 @@ public String getString(final String charset)
*/
@Override
public String getString() {
final byte[] rawdata = get();
String charset = getCharSet();
if (charset == null) {
charset = defaultCharset;
}
byte[] rawdata = new byte[0];
try {
rawdata = get();
String charset = getCharSet();
if (charset == null) {
charset = defaultCharset;
}
return new String(rawdata, charset);
} catch (final UnsupportedEncodingException e) {
} catch (final IOException e) {
return new String(rawdata);
}
}
@@ -384,13 +383,10 @@ public String getString() {
@Override
public void write(final File file) throws Exception {
if (isInMemory()) {
OutputStream fout = null;
try {
fout = Files.newOutputStream(file.toPath());
try (OutputStream fout = Files.newOutputStream(file.toPath())) {
fout.write(get());
fout.close();
} finally {
IOUtils.closeQuietly(fout);
} catch (IOException e) {
throw new IOException("Unexpected output data");
}
} else {
final File outputFile = getStoreLocation();
@@ -497,11 +493,9 @@ public void setFormField(final boolean state) {
* @return An {@link java.io.OutputStream OutputStream} that can be used
* for storing the contents of the file.
*
* @throws IOException if an error occurs.
*/
@Override
public OutputStream getOutputStream()
throws IOException {
public OutputStream getOutputStream() {
if (dfos == null) {
final File outputFile = getTempFile();
dfos = new DeferredFileOutputStream(sizeThreshold, outputFile);
@@ -22,7 +22,6 @@
import java.io.OutputStream;

import org.apache.commons.fileupload2.InvalidFileNameException;
import org.apache.commons.io.IOUtils;

/**
* Utility class for working with streams.
@@ -90,9 +89,8 @@ public static long copy(final InputStream inputStream,
final OutputStream outputStream, final boolean closeOutputStream,
final byte[] buffer)
throws IOException {
OutputStream out = outputStream;
InputStream in = inputStream;
try {
try (OutputStream out = outputStream;
InputStream in = inputStream) {
long total = 0;
for (;;) {
final int res = in.read(buffer);
@@ -112,16 +110,9 @@ public static long copy(final InputStream inputStream,
} else {
out.flush();
}
out = null;
}
in.close();
in = null;
return total;
} finally {
IOUtils.closeQuietly(in);
if (closeOutputStream) {
IOUtils.closeQuietly(out);
}
}
}

@@ -26,6 +26,7 @@
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UncheckedIOException;
import java.util.Arrays;
import org.apache.commons.io.FileUtils;

@@ -124,10 +125,15 @@ public void testBelowThreshold() {
}
assertTrue(item.isInMemory());
assertEquals(item.getSize(), testFieldValueBytes.length);
assertTrue(Arrays.equals(item.get(), testFieldValueBytes));
try {
assertTrue(Arrays.equals(item.get(), testFieldValueBytes));
} catch (UncheckedIOException e) {
fail("Unexpected IOException", e);
}
assertEquals(item.getString(), textFieldValue);
}


/**
* Test creation of a field for which the amount of data falls above the
* configured threshold, where no specific repository is configured.
@@ -182,7 +188,11 @@ public void doTestAboveThreshold(final File repository) {
}
assertFalse(item.isInMemory());
assertEquals(item.getSize(), testFieldValueBytes.length);
assertTrue(Arrays.equals(item.get(), testFieldValueBytes));
try {
assertTrue(Arrays.equals(item.get(), testFieldValueBytes));
} catch (UncheckedIOException e) {
fail("Unexpected IOException", e);
}
assertEquals(item.getString(), textFieldValue);

assertTrue(item instanceof DefaultFileItem);
@@ -30,6 +30,7 @@
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.UncheckedIOException;

import org.apache.commons.fileupload2.disk.DiskFileItemFactory;
import org.apache.commons.io.FileUtils;
@@ -81,7 +82,11 @@ public void testInMemoryObject(final byte[] testFieldValueBytes, final File repo
// Check state is as expected
assertTrue(item.isInMemory(), "Initial: in memory");
assertEquals(item.getSize(), testFieldValueBytes.length, "Initial: size");
compareBytes("Initial", item.get(), testFieldValueBytes);
try {
compareBytes("Initial", item.get(), testFieldValueBytes);
} catch (UncheckedIOException e) {
fail("Unexpected IOException", e);
}
item.delete();
}

@@ -127,7 +132,11 @@ public void testAboveThreshold() {
// Check state is as expected
assertFalse(item.isInMemory(), "Initial: in memory");
assertEquals(item.getSize(), testFieldValueBytes.length, "Initial: size");
compareBytes("Initial", item.get(), testFieldValueBytes);
try {
compareBytes("Initial", item.get(), testFieldValueBytes);
} catch (UncheckedIOException e) {
fail("Unexpected IOException", e);
}

item.delete();
}

0 comments on commit 68dbfbc

Please sign in to comment.