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

Use InputStream.readAllBytes() in FileCopyUtils.copyToByteArray() #30155

Closed
wants to merge 1 commit into from

Conversation

stsypanov
Copy link
Contributor

@stsypanov stsypanov commented Mar 21, 2023

InputStream.readAllBytes() allows us to avoid the creation of an intermediate ByteArrayOutputStream and is likely to perform better.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Mar 21, 2023
@sbrannen sbrannen self-assigned this Mar 21, 2023
@sbrannen sbrannen added in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Mar 21, 2023
@sbrannen sbrannen added this to the 6.0.8 milestone Mar 21, 2023
@sbrannen sbrannen changed the title Use InputStream.readAllBytes() in FileCopyUtils Use InputStream.readAllBytes() in FileCopyUtils.copyToByteArray Mar 21, 2023
@sbrannen sbrannen changed the title Use InputStream.readAllBytes() in FileCopyUtils.copyToByteArray Use InputStream.readAllBytes() in FileCopyUtils.copyToByteArray() Mar 21, 2023
@sbrannen sbrannen closed this in 4e896c8 Mar 21, 2023
@sbrannen
Copy link
Member

This has been merged into main.

Thanks

@stsypanov stsypanov deleted the readAllBytes branch March 21, 2023 16:01
mdeinum pushed a commit to mdeinum/spring-framework that referenced this pull request Jun 29, 2023
InputStream.readAllBytes() allows us to avoid the creation of an
intermediate ByteArrayOutputStream and is likely to perform better.

Closes spring-projectsgh-30155
@edudar-chwy
Copy link

One caveat is that it might be faster but also greedier. With this implementation, when one tries to read all bytes from an uploaded file, the underlying ChannelInputStream allocates a direct buffer of the file's size. With large enough files, that requires a pretty significant MaxDirectMemorySize setting. The memory calculator used by java-buildpack blocks out that memory, and this is not optimal when running in low-footprint containers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants