private void updateUserProfile() { final String username = Preferences.USERNAME.getString(PreferenceManager.getDefaultSharedPreferences(this)); if(username != null) { final User user = getRealm().where(User.class).equalTo(User.USER_ID, username).findFirst(); if (user != null) { profileDrawerItem.withName(user.getDisplayName()); final String encodedImage = user.getAvatar(); if (encodedImage != null) { Bitmap avatarBitmap = BitmapFactory.decodeStream(new Base64InputStream(new ByteArrayInputStream(encodedImage.getBytes()), Base64.DEFAULT)); profileDrawerItem.withIcon(avatarBitmap); } else { profileDrawerItem.withIcon(R.mipmap.ic_launcher); } if (accountHeader != null) accountHeader.updateProfile(profileDrawerItem); } else { profileDrawerItem.withIcon(R.mipmap.ic_launcher); } } else { profileDrawerItem.withIcon(R.mipmap.ic_launcher); } }
@SuppressWarnings("unchecked") public static <T> T decodeString(String string) { byte[] bytes = string.getBytes(); if (bytes.length == 0) { return null; } ByteArrayInputStream byteArray = new ByteArrayInputStream(bytes); Base64InputStream base64InputStream = new Base64InputStream(byteArray, android.util.Base64.DEFAULT); ObjectInputStream in; try { in = new ObjectInputStream(base64InputStream); return (T) in.readObject(); } catch (IOException | ClassNotFoundException | ClassCastException e) { e.printStackTrace(); } return null; }
@Override protected Boolean doInBackground(Void... params) { FileOutputStream fos = null; try { mTempFile = File.createTempFile("decoded", "mediadata"); fos = new FileOutputStream(mTempFile); InputStream stream = new ByteArrayInputStream(mData.getBytes()); Base64InputStream decoder = new Base64InputStream(stream, Base64.DEFAULT); byte[] buffer = new byte[1024]; int len; while ((len = decoder.read(buffer)) != -1) { fos.write(buffer, 0, len); } decoder.close(); return true; } catch (IOException e) { return false; } finally { StreamUtil.closeQuietly(fos); } }
/** * Converts a string byte array representation of an object to the object * * @param stringByteArray a string byte array representation of the object * @return the object */ @Nullable public static <T> T fromStringByteArray(String stringByteArray) { if (stringByteArray == null) { return null; } byte[] bytes = stringByteArray.getBytes(); if (bytes.length == 0) { return null; } ByteArrayInputStream byteArray = new ByteArrayInputStream(bytes); Base64InputStream base64InputStream = new Base64InputStream(byteArray, Base64.DEFAULT); ObjectInputStream in; try { in = new ObjectInputStream(base64InputStream); //noinspection unchecked return (T) in.readObject(); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } return null; }
public static PersonalKey fromBase64(String data) { ObjectInputStream is = null; try { ByteArrayInputStream buf = new ByteArrayInputStream(data.getBytes()); Base64InputStream dec = new Base64InputStream(buf, Base64.NO_WRAP); is = new ObjectInputStream(dec); PGPDecryptedKeyPairRing pair = PGP.unserialize(is); dec.close(); return new PersonalKey(pair, null); } catch (Exception e) { // shouldn't happen - crash throw new RuntimeException(e); } finally { try { if (is != null) is.close(); } catch (IOException ignored) { } } }
public void loadList(final String url, final SuccessCallBack successCallBack) { mNetworkBridge.load(url, new NetworkBridgeCallback() { @Override public void success(String data) { Bitmap bitmap = BitmapFactory.decodeStream( new Base64InputStream( new ByteArrayInputStream( data.getBytes()), Base64.DEFAULT)); mLruCache.put(url, bitmap); successCallBack.notify(url, bitmap); } @Override public void fail() { } }); }
public static void base64(final Context context){ new Thread(new Runnable() { @Override public void run() { String ENCRYPTED_FILE_NAME = "encrypted_key.mp4"; File mEncryptedFile = new File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), ENCRYPTED_FILE_NAME); String out = Environment.getExternalStorageDirectory()+"/test_base64.mp4"; try { FileInputStream fileInputStream = new FileInputStream(mEncryptedFile); Base64InputStream base64InputStream = new Base64InputStream(fileInputStream,Base64.DEFAULT); byte[] buffer = new byte[1024 * 500]; int readCount; long total = 0; FileOutputStream fos = new FileOutputStream(out,false); long length =fileInputStream.available(); while ((readCount = base64InputStream.read(buffer)) != -1) { // 处理下载的数据 fos.write(buffer, 0, readCount); total = total + readCount; System.out.println("字节" + total + "总共长度" + length + "--进度:" + total * 100 / length); } } catch (IOException e) { e.printStackTrace(); } } }).start(); }
/** * Get an input stream based on file path or uri content://, http://, file:// * * @param path path to file * @return an input stream * @throws IOException */ private InputStream getPathFromUri(String path) throws IOException { if (path.startsWith("data:")) { // data:image/png;base64,[ENCODED_IMAGE] String dataInfos = path.substring(0, path.indexOf(',')); dataInfos = dataInfos.substring(dataInfos.indexOf(':') + 1); String baseEncoding = dataInfos.substring(dataInfos.indexOf(';') + 1); // [ENCODED_IMAGE] if("base64".equalsIgnoreCase(baseEncoding)) { String img = path.substring(path.indexOf(',') + 1); byte[] encodedData = img.getBytes(); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(encodedData, 0, encodedData.length); Base64InputStream base64InputStream = new Base64InputStream(byteArrayInputStream, Base64.DEFAULT); return base64InputStream; } else { LOG.d(LOG_TAG, "Could not decode image. The found base encoding is " + baseEncoding); } } if (path.startsWith("content:")) { Uri uri = Uri.parse(path); return mApp.getActivity().getContentResolver().openInputStream(uri); } if (path.startsWith(ASSET_URL_PREFIX)) { String assetRelativePath = path.replace(ASSET_URL_PREFIX, ""); return mApp.getActivity().getAssets().open(assetRelativePath); } if (path.startsWith("http:") || path.startsWith("https:") || path.startsWith("file:")) { URL url = new URL(path); return url.openStream(); } return new FileInputStream(path); }
private void initSyncService(IliasSyncListener iliasSyncListener, SyncProgressListener syncProgressListener) { syncService = new SyncService(iliasSyncListener, syncProgressListener, getIliasPropertiesCallback, getPasswordCallback, asyncErrorHandler); syncService.setBase64InputStreamFactory(new ObjectDoInterfaceX<InputStream, InputStream>() { @Override public InputStream doSomething(InputStream object) { return new Base64InputStream(object, Base64.CRLF); } }); }
/** * @see http * ://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/ * apache /xmlgraphics/util/uri/DataURIResolver.java */ private static InputStream openDataUriStream(String uri) { int commaPos = uri.indexOf(','); if (commaPos < 0) { PXLog.w(TAG, "Data uri is malformed: " + uri); return null; } String header = uri.substring(0, commaPos); String data = uri.substring(commaPos + 1); if (header.endsWith(";base64")) { byte[] bytes = data.getBytes(); ByteArrayInputStream encodedStream = new ByteArrayInputStream(bytes); return new Base64InputStream(encodedStream, Base64.DEFAULT); } else { String encoding = "UTF-8"; final int charsetpos = header.indexOf(";charset="); if (charsetpos > 0) { encoding = header.substring(charsetpos + 9); } try { return new ByteArrayInputStream(URLDecoder.decode(data, encoding) .getBytes(encoding)); } catch (Exception e) { PXLog.e(TAG, e, "Unable to decode data uri contents: " + uri); } } return null; }
private String loadFromFile(int resourceId) throws IOException { InputStreamReader inputStreamReader = null; try { // we're doing this absurd thing with encoding the json file in base64 because phabricator // chokes on it otherwise. inputStreamReader = new InputStreamReader( new Base64InputStream(getResources().openRawResource(resourceId), Base64.DEFAULT), "UTF-8"); StringBuilder sb = new StringBuilder(); char[] buffer = new char[8 * 1024]; int bytesRead; while ((bytesRead = inputStreamReader.read(buffer)) != -1) { sb.append(buffer, 0, bytesRead); } return sb.toString(); } finally { try { if (inputStreamReader != null) { inputStreamReader.close(); } } catch (IOException ignored) { //ignored } } }
public static Object stringToObject(String encodedObject) { try { return new ObjectInputStream(new Base64InputStream( new ByteArrayInputStream(encodedObject.getBytes()), Base64.DEFAULT)).readObject(); } catch (Exception e) { e.printStackTrace(); } return null; }
public static <T> T GSONToObject(Class<T> type, String data){ try{ ByteArrayInputStream bais = new ByteArrayInputStream(data.getBytes()); Base64InputStream baseis = new Base64InputStream(bais, Base64.DEFAULT); InflaterInputStream iis = new InflaterInputStream(baseis); InputStreamReader isr = new InputStreamReader(iis); T obj = gSON.fromJson(isr, type); isr.close(); return obj; }catch (Exception ex){ ex.printStackTrace(); } return null; }