Ejemplo n.º 1
0
 public boolean save(Avatar avatar) {
   File file;
   if (isAvatarCached(avatar)) {
     file = new File(getAvatarPath(avatar.getFilename()));
   } else {
     String filename = getAvatarPath(avatar.getFilename());
     file = new File(filename + ".tmp");
     file.getParentFile().mkdirs();
     OutputStream os = null;
     try {
       file.createNewFile();
       os = new FileOutputStream(file);
       MessageDigest digest = MessageDigest.getInstance("SHA-1");
       digest.reset();
       DigestOutputStream mDigestOutputStream = new DigestOutputStream(os, digest);
       mDigestOutputStream.write(avatar.getImageAsBytes());
       mDigestOutputStream.flush();
       mDigestOutputStream.close();
       String sha1sum = CryptoHelper.bytesToHex(digest.digest());
       if (sha1sum.equals(avatar.sha1sum)) {
         file.renameTo(new File(filename));
       } else {
         Log.d(Config.LOGTAG, "sha1sum mismatch for " + avatar.owner);
         file.delete();
         return false;
       }
     } catch (IllegalArgumentException | IOException | NoSuchAlgorithmException e) {
       return false;
     } finally {
       close(os);
     }
   }
   avatar.size = file.length();
   return true;
 }
Ejemplo n.º 2
0
 public boolean isAvatarCached(Avatar avatar) {
   File file = new File(getAvatarPath(avatar.getFilename()));
   return file.exists();
 }