Skip to content

Commit

Permalink
Merge 3f65167 into 5d3e1d8
Browse files Browse the repository at this point in the history
  • Loading branch information
arturobernalg committed Apr 26, 2021
2 parents 5d3e1d8 + 3f65167 commit 09ec89a
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 43 deletions.
7 changes: 5 additions & 2 deletions src/main/java/org/apache/commons/fileupload2/FileItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,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 IOException if an I/O error occurs
*/
byte[] get();
byte[] get() throws IOException;

/**
* Returns the contents of the file item as a String, using the specified
Expand All @@ -118,8 +120,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
Expand Down
45 changes: 19 additions & 26 deletions src/main/java/org/apache/commons/fileupload2/disk/DiskFileItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -292,12 +292,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 IOException 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 IOException if an I/O error occurs
*/
@Override
public byte[] get() {
public byte[] get() throws IOException {
if (isInMemory()) {
if (cachedContent == null && dfos != null) {
cachedContent = dfos.getData();
Expand All @@ -306,17 +308,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 IOException("Unexpected input data");
}

return fileData;
}

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

Expand All @@ -349,14 +346,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);
}
}
Expand Down Expand Up @@ -384,13 +382,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();
Expand Down Expand Up @@ -497,11 +492,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);
Expand Down
13 changes: 2 additions & 11 deletions src/main/java/org/apache/commons/fileupload2/util/Streams.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import org.apache.commons.io.FileUtils;
Expand Down Expand Up @@ -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 (IOException 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.
Expand Down Expand Up @@ -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 (IOException e) {
fail("Unexpected IOException", e);
}
assertEquals(item.getString(), textFieldValue);

assertTrue(item instanceof DefaultFileItem);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,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 (IOException e) {
fail("Unexpected IOException", e);
}
item.delete();
}

Expand Down Expand Up @@ -127,7 +131,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 (IOException e) {
fail("Unexpected IOException", e);
}

item.delete();
}
Expand Down

0 comments on commit 09ec89a

Please sign in to comment.