Invoking as static method is just as hardcoded as inline code.
Certainly not.
private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
...
public static int copy(InputStream input, OutputStream output)
throws IOException {
long count = copyLarge(input, output);
if (count > Integer.MAX_VALUE) {
return -1;
}
return (int) count;
}
public static long copyLarge(InputStream input, OutputStream output)
throws IOException {
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
long count = 0;
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
count += n;
}
return count;
}
and shared code, shorter, more maintainable, well-tested code,
not reinvent the wheel,
> support, regular bug fix
and improvments...
And certainly many many other reasons that using a lib (particularly Apache
Commons) should be a reflex
All good arguments. For non-trivial stuff.
But one has to stop at some point.
Replacing 5 lines of code with a simple while loop with a
library call does not have that large benefits.
If the developers having to touch the code can not figure
those 5 lines out, then the project is doomed no matter what.
I would still pick the library call if it were in the
standard Java library, because it cost nothing.
But an external library actually do carry cost.
For anybody except the most hopeless developers more cost
than maintaining a while loop.
Obviously if that particular library are used many
other places in the app, then the cost drops to zero.
But I would not use it this case to justify adding the
library to the project.
Arne