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

Add newInputStream and withInputStream to java.net.URL for dealing with binary files

    XMLWordPrintableJSON

Details

    • New Feature
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • None
    • 1.5.2
    • None
    • None
    • Patch

    Description

      I recently came across a situation where I wanted to initialize a JCE Certificate directly from a URL. I wanted to do this:

      def cfUrl = args[0].toURL()
      def cf = CertificateFactory.getInstance("X.509")
      def newRootCert = cfUrl.withInputStream { cf.generateCertificate(it) }
      

      Putting aside the fact that the with* methods don't return the closure return value (which I'm going to create a separate JIRA for), there is not currently a withInputStream method for URL.

      I could do something like this to work around it, but I'd rather do it the Groovier way:

      def newRootCert = cfUrl.openStream().withStream { cf.generateCertificate(it) }
      

      I've attached a patch to add URL.newInputStream and URL.withInputStream. It doesn't have any tests because I couldn't find any existing tests for URL.withReader (or File.withInputStream for that matter) and wasn't sure what the best way to handle it would be.

      Attachments

        1. URL-withInputStream.diff
          2 kB
          Mike Dillon

        Activity

          People

            paulk Paul King
            md5 Mike Dillon
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: