Skip to content

Commit

Permalink
Rethrow Exception.
Browse files Browse the repository at this point in the history
  • Loading branch information
arturobernalg committed Apr 26, 2021
1 parent 1fc5e66 commit c896a93
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 17 deletions.
7 changes: 5 additions & 2 deletions src/main/java/org/apache/commons/fileupload2/FileItem.java
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
25 changes: 14 additions & 11 deletions src/main/java/org/apache/commons/fileupload2/disk/DiskFileItem.java
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 @@ -310,7 +312,7 @@ public byte[] get() {
try (InputStream fis = Files.newInputStream(dfos.getFile().toPath())) {
IOUtils.readFully(fis, fileData);
} catch (IOException e) {
e.printStackTrace();
throw new IOException("Unexpected input data");
}
return fileData;
}
Expand All @@ -329,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 @@ -344,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 @@ -382,7 +385,7 @@ public void write(final File file) throws Exception {
try (OutputStream fout = Files.newOutputStream(file.toPath())) {
fout.write(get());
} catch (IOException e) {
e.printStackTrace();
throw new IOException("Unexpected output data");
}
} else {
final File outputFile = getStoreLocation();
Expand Down
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
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 c896a93

Please sign in to comment.