Exemplo n.º 1
0
  @Override
  public void process(AssetEncounter encounter) throws IOException {
    Resource in = encounter.getResource();
    int length = in.getContentLength();
    if (length < 0) {
      length = 0;
      InputStream stream = in.openStream();
      while (stream.read() != -1) length++;
      stream.close();
    }

    byte[] data = String.valueOf(length).getBytes();
    encounter.replaceWith(new MemoryResource(null, null, data));
  }
Exemplo n.º 2
0
  private byte[] digest(Resource resource)
      throws IOException, NoSuchAlgorithmException, DigestException {
    InputStream stream = resource.openStream();
    try {
      MessageDigest digest = MessageDigest.getInstance("SHA-1");
      byte[] buf = new byte[1024];
      int len;
      while ((len = stream.read(buf)) != -1) {
        digest.update(buf, 0, len);
      }

      return digest.digest();
    } finally {
      stream.close();
    }
  }