Skip to content

Commit

Permalink
Improve handling of FormData files and buffers
Browse files Browse the repository at this point in the history
  • Loading branch information
noboomu committed Nov 15, 2021
1 parent c305504 commit a71c05b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.**


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,7 @@ private static java.util.Optional<ByteBuffer> 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)
Expand Down Expand Up @@ -260,7 +253,7 @@ private static <T> 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());

Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -319,7 +312,7 @@ private static <T> 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());

Expand Down Expand Up @@ -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());

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit a71c05b

Please sign in to comment.