Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-4271

InputStream.eachByte in DGM to read IS using a byte buffer

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 1.8-beta-1
    • 1.7.4, 1.8-beta-1
    • groovy-jdk
    • 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

      Attachments

        1. eachByte_buffered.patch
          3 kB
          Tim Yates

        Activity

          People

            guillaume Guillaume Sauthier
            tim_yates Tim Yates
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: