title: Base64 Encode Images in Rhino
body: <p>I'm attempting to create a build process that will base64 encode the images in my CSS file.  I use rhino for my build process so natural thought was to just do this after I get done building in rhino.</p>
  
  <p>So far I've got the image reading done but getting stuck at the Base64 part ... Everything I try in JavaScript gets mad about the byte[] and everything in Java I try has problems with the Rhino.  </p>
  
  <p>Since its rhino I'm limited on the libraries I can use and can't use the apache commons.</p>
  
  <p>Anyone got any ideas?</p>
  
  <pre class="lang-js prettyprint-override"><code>prodcss = prodcss.replace(/url\(['"]*([^"')]*)['"]*\)/g, function(match) {
      var path = "build/" + match.substring(4, match.length - 1);
  
      // Read the image to a byte[]
      var file = new java.io.File(path);
      var bufferedImage = javax.imageio.ImageIO.read(file);
      var raster = bufferedImage.getRaster();
      var data = raster.getDataBuffer().getData();
  
              var base64 = ''; // Whats the best way to accomplish this ...
  
      return "url(data:image/png;base64," + base64 + ")";
  });
  </code></pre>
  
  <p>Appreciate the help.</p>