Details
-
Improvement
-
Status: Closed
-
Minor
-
Resolution: Fixed
-
1.8-beta-1
-
None
-
Patch
Description
Currently it is possible to do:
in_stream.eachByte { b ->
// do some work with the byte b
}
When writing an md5 File hash http://groovyconsole.appspot.com/script/161001 I found that a variant of eachByte that takes a buffer size, and calls the closure with the buffer and number of bytes read would be useful...
I have attached a patch which should (hopefully) give this functionality to InputStream, URL and File (to match the current locations of eachByte)
I believe it is useful, as it changes this:
byte[] buffer = new byte[ 8192 ] int read = 0 while( ( read = in_stream.read( buffer, 0, 8192 ) ) > 0 ) { doSomethingWithTheBuffer( buffer, read ) }
To this:
in_stream.eachByte( 8192 ) { buffer, read -> doSomethingWithTheBuffer( buffer, read ) }
(and closes the stream for you at the end – again, to match the original eachByte call)
I haven't included any tests for 2 reasons;
- I can't find tests for the original eachByte methods to see the prefered method for testing this
- I can't get the current snapshot to run the tests
./gradlew test
fails with java.lang.NoClassDefFoundError: ch/qos/logback/core/spi/DeferredProcessingAware and ant fails with failed tests even on a clean checkout