Search in sources :

Example 1 with Base64OutputStream

use of android.util.Base64OutputStream in project AndroidAsync by koush.

the class NetworkEventReporterWrapper method interpretResponseEmitter.

public DataEmitter interpretResponseEmitter(final String requestId, @Nullable DataEmitter body, final boolean b64Encode) {
    final NetworkPeerManager peerManager = getPeerManagerIfEnabled();
    if (peerManager == null)
        return null;
    final WritableByteChannel channel;
    try {
        if (b64Encode) {
            final Base64OutputStream b64out = new Base64OutputStream(peerManager.getResponseBodyFileManager().openResponseBodyFile(requestId, false), Base64.DEFAULT);
            channel = Channels.newChannel(b64out);
        } else {
            channel = ((FileOutputStream) peerManager.getResponseBodyFileManager().openResponseBodyFile(requestId, false)).getChannel();
        }
    } catch (IOException e) {
        return null;
    }
    FilteredDataEmitter ret = new FilteredDataEmitter() {

        ByteBufferList pending = new ByteBufferList();

        @Override
        protected void report(Exception e) {
            super.report(e);
            StreamUtility.closeQuietly(channel);
            if (e == null)
                responseReadFinished(requestId);
            else
                responseReadFailed(requestId, e.toString());
        }

        @Override
        public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
            int amount = bb.remaining();
            ByteBuffer[] original = bb.getAllArray();
            ByteBuffer[] copy = new ByteBuffer[original.length];
            for (int i = 0; i < original.length; i++) {
                copy[i] = original[i].duplicate();
            }
            try {
                for (ByteBuffer c : copy) {
                    channel.write(c);
                }
            } catch (IOException ignored) {
                StreamUtility.closeQuietly(channel);
            }
            pending.addAll(original);
            dataReceived(requestId, amount, amount);
            super.onDataAvailable(emitter, pending);
        }
    };
    ret.setDataEmitter(body);
    return ret;
}
Also used : FilteredDataEmitter(com.koushikdutta.async.FilteredDataEmitter) ByteBufferList(com.koushikdutta.async.ByteBufferList) NetworkPeerManager(com.facebook.stetho.inspector.network.NetworkPeerManager) DataEmitter(com.koushikdutta.async.DataEmitter) FilteredDataEmitter(com.koushikdutta.async.FilteredDataEmitter) WritableByteChannel(java.nio.channels.WritableByteChannel) IOException(java.io.IOException) Base64OutputStream(android.util.Base64OutputStream) ByteBuffer(java.nio.ByteBuffer) IOException(java.io.IOException)

Example 2 with Base64OutputStream

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

the class Base64Body method writeTo.

/**
 * This method consumes the input stream, so can only be called once
 *
 * @param out Stream to write to
 * @throws IllegalStateException If called more than once
 * @throws IOException
 * @throws MessagingException
 */
@Override
public void writeTo(OutputStream out) throws IllegalStateException, IOException, MessagingException {
    if (mAlreadyWritten) {
        throw new IllegalStateException("Base64Body can only be written once");
    }
    mAlreadyWritten = true;
    try {
        final Base64OutputStream b64out = new Base64OutputStream(out, Base64.DEFAULT);
        IOUtils.copyLarge(mSource, b64out);
    } finally {
        mSource.close();
    }
}
Also used : Base64OutputStream(android.util.Base64OutputStream)

Example 3 with Base64OutputStream

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

the class FileBackend method getStoredPepAvatar.

public Avatar getStoredPepAvatar(String hash) {
    if (hash == null) {
        return null;
    }
    Avatar avatar = new Avatar();
    File file = new File(getAvatarPath(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 (IOException e) {
        return null;
    } catch (NoSuchAlgorithmException e) {
        return null;
    } finally {
        close(is);
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Base64OutputStream(android.util.Base64OutputStream) Avatar(de.pixart.messenger.xmpp.pep.Avatar) FileInputStream(java.io.FileInputStream) Paint(android.graphics.Paint) DigestOutputStream(java.security.DigestOutputStream) BitmapFactory(android.graphics.BitmapFactory) MessageDigest(java.security.MessageDigest) DownloadableFile(de.pixart.messenger.entities.DownloadableFile) File(java.io.File)

Example 4 with Base64OutputStream

use of android.util.Base64OutputStream in project kdeconnect-android by KDE.

the class ContactsHelper method photoId64Encoded.

public static String photoId64Encoded(Context context, String photoId) {
    if (photoId == null) {
        return "";
    }
    Uri photoUri = Uri.parse(photoId);
    ByteArrayOutputStream encodedPhoto = new ByteArrayOutputStream();
    try (InputStream input = context.getContentResolver().openInputStream(photoUri);
        Base64OutputStream output = new Base64OutputStream(encodedPhoto, Base64.DEFAULT)) {
        IOUtils.copy(input, output, 1024);
        return encodedPhoto.toString();
    } catch (Exception ex) {
        Log.e(LOG_TAG, ex.toString());
        return "";
    }
}
Also used : InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Base64OutputStream(android.util.Base64OutputStream) Uri(android.net.Uri) IOException(java.io.IOException)

Example 5 with Base64OutputStream

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

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 (OutOfMemoryError e) {
        Log.d(Config.LOGTAG, "unable to convert avatar to base64 due to low memory");
        return null;
    } 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(eu.siacs.conversations.xmpp.pep.Avatar) FileWriterException(eu.siacs.conversations.utils.FileWriterException) FileNotFoundException(java.io.FileNotFoundException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException)

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