Search in sources :

Example 6 with Base64OutputStream

use of android.util.Base64OutputStream in project Conversations by siacs.

the class FileBackend method getStoredPepAvatar.

public Avatar getStoredPepAvatar(String hash) {
    if (hash == null) {
        return null;
    }
    Avatar avatar = new Avatar();
    final File file = getAvatarFile(hash);
    FileInputStream is = null;
    try {
        avatar.size = file.length();
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(file.getAbsolutePath(), options);
        is = new FileInputStream(file);
        ByteArrayOutputStream mByteArrayOutputStream = new ByteArrayOutputStream();
        Base64OutputStream mBase64OutputStream = new Base64OutputStream(mByteArrayOutputStream, Base64.DEFAULT);
        MessageDigest digest = MessageDigest.getInstance("SHA-1");
        DigestOutputStream os = new DigestOutputStream(mBase64OutputStream, digest);
        byte[] buffer = new byte[4096];
        int length;
        while ((length = is.read(buffer)) > 0) {
            os.write(buffer, 0, length);
        }
        os.flush();
        os.close();
        avatar.sha1sum = CryptoHelper.bytesToHex(digest.digest());
        avatar.image = new String(mByteArrayOutputStream.toByteArray());
        avatar.height = options.outHeight;
        avatar.width = options.outWidth;
        avatar.type = options.outMimeType;
        return avatar;
    } catch (NoSuchAlgorithmException | IOException e) {
        return null;
    } finally {
        close(is);
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) Base64OutputStream(android.util.Base64OutputStream) Avatar(eu.siacs.conversations.xmpp.pep.Avatar) FileInputStream(java.io.FileInputStream) Paint(android.graphics.Paint) DigestOutputStream(java.security.DigestOutputStream) BitmapFactory(android.graphics.BitmapFactory) MessageDigest(java.security.MessageDigest) File(java.io.File) DownloadableFile(eu.siacs.conversations.entities.DownloadableFile)

Example 7 with Base64OutputStream

use of android.util.Base64OutputStream in project Pix-Art-Messenger by kriztan.

the class FileBackend method getPepAvatar.

private Avatar getPepAvatar(Bitmap bitmap, Bitmap.CompressFormat format, int quality) {
    try {
        ByteArrayOutputStream mByteArrayOutputStream = new ByteArrayOutputStream();
        Base64OutputStream mBase64OutputStream = new Base64OutputStream(mByteArrayOutputStream, Base64.DEFAULT);
        MessageDigest digest = MessageDigest.getInstance("SHA-1");
        DigestOutputStream mDigestOutputStream = new DigestOutputStream(mBase64OutputStream, digest);
        if (!bitmap.compress(format, quality, mDigestOutputStream)) {
            return null;
        }
        mDigestOutputStream.flush();
        mDigestOutputStream.close();
        long chars = mByteArrayOutputStream.size();
        if (format != Bitmap.CompressFormat.PNG && quality >= 50 && chars >= Config.AVATAR_CHAR_LIMIT) {
            int q = quality - 2;
            Log.d(Config.LOGTAG, "avatar char length was " + chars + " reducing quality to " + q);
            return getPepAvatar(bitmap, format, q);
        }
        Log.d(Config.LOGTAG, "settled on char length " + chars + " with quality=" + quality);
        final Avatar avatar = new Avatar();
        avatar.sha1sum = CryptoHelper.bytesToHex(digest.digest());
        avatar.image = new String(mByteArrayOutputStream.toByteArray());
        if (format.equals(Bitmap.CompressFormat.WEBP)) {
            avatar.type = "image/webp";
        } else if (format.equals(Bitmap.CompressFormat.JPEG)) {
            avatar.type = "image/jpeg";
        } else if (format.equals(Bitmap.CompressFormat.PNG)) {
            avatar.type = "image/png";
        }
        avatar.width = bitmap.getWidth();
        avatar.height = bitmap.getHeight();
        return avatar;
    } catch (Exception e) {
        return null;
    }
}
Also used : DigestOutputStream(java.security.DigestOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Base64OutputStream(android.util.Base64OutputStream) MessageDigest(java.security.MessageDigest) Paint(android.graphics.Paint) Avatar(de.pixart.messenger.xmpp.pep.Avatar) FileNotFoundException(java.io.FileNotFoundException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) FileWriterException(de.pixart.messenger.utils.FileWriterException) IOException(java.io.IOException)

Example 8 with Base64OutputStream

use of android.util.Base64OutputStream in project stetho by facebook.

the class ResponseBodyFileManager method openResponseBodyFile.

public OutputStream openResponseBodyFile(String requestId, boolean base64Encode) throws IOException {
    OutputStream out = mContext.openFileOutput(getFilename(requestId), Context.MODE_PRIVATE);
    out.write(base64Encode ? 1 : 0);
    if (base64Encode) {
        return new Base64OutputStream(out, Base64.DEFAULT);
    } else {
        return out;
    }
}
Also used : OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Base64OutputStream(android.util.Base64OutputStream) Base64OutputStream(android.util.Base64OutputStream)

Example 9 with Base64OutputStream

use of android.util.Base64OutputStream in project android_packages_apps_Dialer by LineageOS.

the class BinaryTempFileBody method writeTo.

@Override
public void writeTo(OutputStream out) throws IOException, MessagingException {
    InputStream in = getInputStream();
    Base64OutputStream base64Out = new Base64OutputStream(out, Base64.CRLF | Base64.NO_CLOSE);
    IOUtils.copy(in, base64Out);
    base64Out.close();
    file.delete();
    in.close();
}
Also used : FileInputStream(java.io.FileInputStream) FilterInputStream(java.io.FilterInputStream) InputStream(java.io.InputStream) Base64OutputStream(android.util.Base64OutputStream)

Aggregations

Base64OutputStream (android.util.Base64OutputStream)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 IOException (java.io.IOException)6 Paint (android.graphics.Paint)4 DigestOutputStream (java.security.DigestOutputStream)4 MessageDigest (java.security.MessageDigest)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 FileInputStream (java.io.FileInputStream)3 BitmapFactory (android.graphics.BitmapFactory)2 Avatar (de.pixart.messenger.xmpp.pep.Avatar)2 Avatar (eu.siacs.conversations.xmpp.pep.Avatar)2 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 InputStream (java.io.InputStream)2 Uri (android.net.Uri)1 NetworkPeerManager (com.facebook.stetho.inspector.network.NetworkPeerManager)1 ByteBufferList (com.koushikdutta.async.ByteBufferList)1 DataEmitter (com.koushikdutta.async.DataEmitter)1 FilteredDataEmitter (com.koushikdutta.async.FilteredDataEmitter)1 DownloadableFile (de.pixart.messenger.entities.DownloadableFile)1