rewrite icon indicating copy to clipboard operation
rewrite copied to clipboard

Add recipe to add bulk read method to InputStream implementations

Open pstreef opened this issue 1 month ago • 0 comments

Java's default InputStream.read(byte[], int, int) calls the single-byte read() in a loop, causing up to 350x slower performance for bulk reads.

This recipe detects InputStream subclasses that:

  • Override read() but not read(byte[], int, int)
  • Delegate to another InputStream

For simple delegation patterns, it adds the missing bulk read method. For complex bodies (side effects, transformations), it adds a search marker for manual review.

Handles both anonymous and named classes, ternary and if-statement null check styles, and skips FilterInputStream subclasses.

What's changed?

Adding a recipe to detect and update (simple cases of) InputStream implementations that are missing the bulk read method.

What's your motivation?

I found 3 such instances in OpenRewrite itself which caused huge performance gains when fixed.

Anything in particular you'd like reviewers to focus on?

Is this recipe useful in its current form, should it maybe only detect? or is only fixing simple cases good too?

Anyone you would like to review specifically?

@timtebeek

Have you considered any alternatives or workarounds?

Not creating this recipe

Any additional context

  • https://github.com/openrewrite/rewrite/pull/6381
  • https://github.com/openrewrite/rewrite/pull/6377

Checklist

  • [x] I've added unit tests to cover both positive and negative cases
  • [x] I've read and applied the recipe conventions and best practices
  • [x] I've used the IntelliJ IDEA auto-formatter on affected files

pstreef avatar Dec 05 '25 11:12 pstreef