Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify API #309

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UncheckedIOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.CopyOption;
Expand All @@ -36,7 +35,6 @@
import org.apache.commons.io.Charsets;
import org.apache.commons.io.build.AbstractOrigin;
import org.apache.commons.io.file.PathUtils;
import org.apache.commons.io.function.Uncheck;
import org.apache.commons.io.output.DeferredFileOutputStream;

/**
Expand Down Expand Up @@ -291,19 +289,19 @@ public DiskFileItem delete() throws IOException {
* and cached.
*
* @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.
* @throws IOException if an I/O error occurs.
* @throws OutOfMemoryError See {@link Files#readAllBytes(Path)}: If an array of the required size cannot be allocated, for example the file is larger
* that {@code 2GB}
*/
@Override
public byte[] get() throws UncheckedIOException {
public byte[] get() throws IOException {
if (isInMemory()) {
if (cachedContent == null && dfos != null) {
cachedContent = dfos.getData();
}
return cachedContent != null ? cachedContent.clone() : new byte[0];
}
return Uncheck.get(() -> Files.readAllBytes(dfos.getFile().toPath()));
return Files.readAllBytes(dfos.getFile().toPath());
}

/**
Expand Down Expand Up @@ -442,9 +440,10 @@ public long getSize() {
* </p>
*
* @return The contents of the file, as a string.
* @throws IOException if an I/O error occurs
*/
@Override
public String getString() {
public String getString() throws IOException {
return new String(get(), getCharset());
}

Expand All @@ -453,6 +452,7 @@ public String getString() {
*
* @param charset The charset to use.
* @return The contents of the file, as a string.
* @throws IOException if an I/O error occurs
*/
@Override
public String getString(final Charset charset) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UncheckedIOException;
import java.nio.charset.Charset;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
Expand Down Expand Up @@ -57,9 +56,9 @@ public interface FileItem<F extends FileItem<F>> extends FileItemHeadersProvider
* Gets the contents of the file item as a byte array.
*
* @return The contents of the file item as a byte array.
* @throws UncheckedIOException if an I/O error occurs
* @throws IOException if an I/O error occurs
*/
byte[] get() throws UncheckedIOException;
byte[] get() throws IOException;

/**
* Gets the content type passed by the browser or {@code null} if not defined.
Expand Down Expand Up @@ -113,8 +112,10 @@ public interface FileItem<F extends FileItem<F>> extends FileItemHeadersProvider
* item.
*
* @return The contents of the item, as a string.
*
* @throws IOException if an I/O error occurs
*/
String getString();
String getString() throws IOException;

/**
* Gets the contents of the file item as a String, using the specified encoding. This method uses {@link #get()} to retrieve the contents of the item.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public void testContentTypeAttachment() throws IOException {
* @throws FileUploadException Test failure.
*/
@Test
public void testEmptyFile() throws FileUploadException {
public void testEmptyFile() throws IOException {
// @formatter:off
final var fileItems = parseUpload (upload,
"-----1234\r\n" +
Expand Down Expand Up @@ -349,7 +349,7 @@ public void testFoldedHeaders() throws IOException {
* @throws FileUploadException Test failure.
*/
@Test
public void testIE5MacBug() throws FileUploadException {
public void testIE5MacBug() throws IOException {
final var fileItems = parseUpload(upload,
// @formatter:off
"-----1234\r\n" +
Expand Down
Loading