Exemplo n.º 1
0
 /**
  * Returns an InputStream that will decode the Base64 encoded content on the fly. The data is
  * stored encoded in memory and only decoded as the InputStream is consumed.
  */
 public InputStream getInputStream() throws IOException {
   String content = super.getProperty("data");
   if (content == null) return null;
   ByteArrayInputStream in = new ByteArrayInputStream(content.getBytes("UTF-8"));
   InputStream bin = new Base64InputStream(in);
   if (has("compression")) {
     String comp = getProperty("compression");
     bin = Compression.wrap(bin, comp);
   }
   return bin;
 }
Exemplo n.º 2
0
 /**
  * Set the Content as a Base64 Encoded string. Calling this method will perform a blocking read
  * that will consume the InputStream and generate a Base64 Encoded String.
  *
  * <p>If a Hasher class is provided, a hash digest for the input data prior to encoding will be
  * generated and stored in a property value whose name is the value returned by Hasher.name();
  * e.g., HashHelper.Md5 will add "md5":"{hex digest}" to the JSON object.
  *
  * <p>The content may be optionally compressed prior to base64 encoding by passing in one or
  * more CompressionCodecs. If compression is used, a "compression" property will be added to the
  * object whose value is a comma separated list of the applied compression codecs in the order
  * of application. The getInputStream method will automatically search for the "compression"
  * property and attempt to automatically decompress the stream when reading.
  *
  * <p>This will also automatically set the "length" property equal to the total number of
  * uncompressed, unencoded octets.
  */
 public M data(InputStream data, Hasher hash, CompressionCodec... comps) throws IOException {
   ByteArrayOutputStream out = new ByteArrayOutputStream();
   OutputStream bout = new Base64OutputStream(out, true, 0, null);
   if (comps.length > 0) {
     bout = Compression.wrap(bout, comps);
     String comp = Compression.describe(null, comps);
     set("compression", comp.substring(1, comp.length() - 1));
   }
   byte[] d = new byte[1024];
   int r = -1, len = 0;
   while ((r = data.read(d)) > -1) {
     len += r;
     if (hash != null) hash.update(d, 0, r);
     bout.write(d, 0, r);
     bout.flush();
   }
   bout.close();
   set("length", len);
   String c = new String(out.toByteArray(), "UTF-8");
   set("data", c);
   if (hash != null) set(hash.name(), hash.get());
   return (M) this;
 }