question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

StaticFile.fromFile IllegalArgumentException with large files

See original GitHub issue

The following code results in an IllegalArgumentException when the file is longer than Int.MaxValue

case req@GET -> Root / "file" =>
      StaticFile.fromFile(new File("massive-file.txt"), Some(req)).getOrElseF(NotFound())

And the exceptionis

logtype="application" transaction_id="" req_time="2017-11-15T10:53:34.195+11:00" loglevel="ERROR" threadname="scala-execution-context-global-73" classname="o.h.b.p.Stage" linenumber="Http1ServerStage.scala:226" message="Error writing body"
java.lang.IllegalArgumentException: null
	at java.nio.ByteBuffer.allocate(ByteBuffer.java:334)
	at fs2.io.file.FileHandle$$anon$2.read(FileHandle.scala:160)
	at org.http4s.StaticFile$._readAllFromFileHandle0$1(StaticFile.scala:150)
	at org.http4s.StaticFile$.readAllFromFileHandle$1(StaticFile.scala:145)
	at org.http4s.StaticFile$.$anonfun$fileToBody$7(StaticFile.scala:160)
	at org.http4s.StaticFile$.$anonfun$fileToBody$7$adapted(StaticFile.scala:160)

Some background as to how / why I discovered this. I was looking into streaming of large responses and noticed that the JVM CPU usage is very high while doing so. I wasn’t expecting this as I expect that Streaming of a large response is largely IO. To narrow down to the least possible application code I decided just to stream a large file. CPU usage is still higher than what I’d expect. I’m still investigating.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
rossabakercommented, Nov 15, 2017

That exception happens when you allocate a negative buffer size. I bet the .toInt is the villain of this story.

0reactions
rossabakercommented, Nov 15, 2017

I fixed this while looking at #1555. PR coming.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Convert file: Uri to File in Android - Stack Overflow
I am getting java.lang.IllegalArgumentException: Expected file scheme in URI: content://com.shajeel.daily_monitoring.localstorage.documents.
Read more >
File (Java Platform SE 8 ) - Oracle Help Center
Returns an array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname that satisfy the specified...
Read more >
Files (Guava: Google Core Libraries for Java 21.0 API)
Parameters: file - the file to read from; Returns: a byte array containing all the bytes from file; Throws: IllegalArgumentException - if the...
Read more >
guava/src/com/google/common/io/Files.java - Google Git
@return a byte array containing all the bytes from file. * @throws IllegalArgumentException if the file is bigger than the largest.
Read more >
FileUtil (File System API)
Finds an unused file name similar to that requested in the same folder. ... Throws: IllegalArgumentException - if listener was not listening to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found