diff --git a/CHANGELOG.md b/CHANGELOG.md index 5b84958..98a864b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ Proteus Changelog. ## Unreleased ### No issue +**Improve uploading of binary files** + + +[c305504d2d62424](https://github.com/noboomu/proteus/commit/c305504d2d62424) Joshua Bauer *2021-11-09 23:40:40* + **Bump compiler version for JDK 17 support.** diff --git a/proteus-core/src/main/java/io/sinistral/proteus/server/Extractors.java b/proteus-core/src/main/java/io/sinistral/proteus/server/Extractors.java index 9fb3b2f..cf762b6 100644 --- a/proteus-core/src/main/java/io/sinistral/proteus/server/Extractors.java +++ b/proteus-core/src/main/java/io/sinistral/proteus/server/Extractors.java @@ -210,14 +210,7 @@ private static java.util.Optional formValueBuffer(final HttpServerEx { FormData.FileItem fileItem = fi.getFileItem(); - if (fileItem.isInMemory()) - { - return DataOps.streamToBuffer(fileItem.getInputStream()); - } - else - { - return DataOps.readAllBytes(fileItem.getFile().toFile().toPath()); - } + return DataOps.fileItemToBuffer(fileItem); } } catch (Exception e) @@ -260,7 +253,7 @@ private static T formValueModel(final FormData.FormValue formValue, final Ty { try { - ByteBuffer byteBuffer = DataOps.streamToBuffer(formValue.getFileItem().getInputStream()); + ByteBuffer byteBuffer = DataOps.fileItemToBuffer(formValue.getFileItem()); return parseTypedXML(type, byteBuffer.array()); @@ -288,7 +281,7 @@ else if (formValue.getHeaders().get(Headers.CONTENT_TYPE) == null || (formValue. { try { - ByteBuffer byteBuffer = DataOps.streamToBuffer(formValue.getFileItem().getInputStream()); + ByteBuffer byteBuffer = DataOps.fileItemToBuffer(formValue.getFileItem()); return parseTypedJson(type, byteBuffer.array()); @@ -319,7 +312,7 @@ private static T formValueModel(final FormData.FormValue formValue, final Cl { try { - ByteBuffer byteBuffer = DataOps.streamToBuffer(formValue.getFileItem().getInputStream()); + ByteBuffer byteBuffer = DataOps.fileItemToBuffer(formValue.getFileItem()); return parseTypedXML(type, byteBuffer.array()); @@ -347,7 +340,7 @@ else if (formValue.getHeaders().get(Headers.CONTENT_TYPE) == null || (formValue. { try { - ByteBuffer byteBuffer = DataOps.streamToBuffer(formValue.getFileItem().getInputStream()); + ByteBuffer byteBuffer = DataOps.fileItemToBuffer(formValue.getFileItem()); return parseTypedJson(type, byteBuffer.array()); diff --git a/proteus-core/src/main/java/io/sinistral/proteus/utilities/DataOps.java b/proteus-core/src/main/java/io/sinistral/proteus/utilities/DataOps.java index a16c0c6..5df8801 100644 --- a/proteus-core/src/main/java/io/sinistral/proteus/utilities/DataOps.java +++ b/proteus-core/src/main/java/io/sinistral/proteus/utilities/DataOps.java @@ -1,5 +1,7 @@ package io.sinistral.proteus.utilities; +import io.undertow.server.handlers.form.FormData; + import java.io.IOException; import java.io.InputStream; import java.nio.ByteBuffer; @@ -27,10 +29,20 @@ public static void writeStreamToPath(InputStream inputStream, Path path) throws } public static ByteBuffer streamToBuffer(InputStream stream) throws IOException { + return ByteBuffer.wrap(stream.readAllBytes()); + } + + public static ByteBuffer fileItemToBuffer(FormData.FileItem fileItem) throws Exception + { + if(fileItem.isInMemory()) + { + return ByteBuffer.wrap(fileItem.getInputStream().readAllBytes()); + } + else + { + return readAllBytes(fileItem.getFile()); + } - final io.sinistral.proteus.utilities.AsyncByteOutputStream byteArrayOutputStream = new io.sinistral.proteus.utilities.AsyncByteOutputStream(65536); - stream.transferTo(byteArrayOutputStream); - return ByteBuffer.wrap(byteArrayOutputStream.toByteArray()); } public static ByteBuffer readAllBytes(Path fp) throws IOException {