protected void serialize(FileOutputStream stream, Object object) throws Exception { ObjectOutputStream oos = null; try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); oos = new ObjectOutputStream(bos); oos.writeObject(object); stream.write(bos.toByteArray()); } finally { Util.ensureClosed(oos); } }
public static synchronized void updateVersionHistory(int newVersionCode, String newVersionName) { updateVersionHistory(newVersionCode, newVersionName, Util.currentTimeSeconds()); }
/** * Generates a random name for a conversation messages file */ private @NonNull File generateMessagesFilename() { return Util.getEncryptedFilename(new File(conversationsStorageDir, "messages-" + Util.generateRandomFilename())); }
private static String readResponse(InputStream is, boolean gzipped) throws IOException { if (is == null) { return null; } try { if (gzipped) { is = new GZIPInputStream(is); } return Util.readStringFromInputStream(is, "UTF-8"); } finally { Util.ensureClosed(is); } }
public static void writeText(File file, String text) throws IOException { if (text == null) { throw new IllegalArgumentException("'text' is null"); } PrintStream output = null; try { output = openTextWrite(file, false); // TODO: make a parameter output.print(text); } finally { ensureClosed(output); } }
public static void appendFileToStream(File file, OutputStream outputStream) throws IOException { if (file == null) { throw new IllegalArgumentException("'file' is null"); } if (!file.exists()) { throw new FileNotFoundException("File does not exist: " + file); } if (file.isDirectory()) { throw new FileNotFoundException("File is directory: " + file); } FileInputStream input = null; try { input = new FileInputStream(file); copy(input, outputStream); } finally { ensureClosed(input); } }
public static <T extends SerializableObject> T deserialize(File file, Class<T> cls, EncryptionKey encryptionKey) throws IOException { try { final byte[] encryptedBytes = Util.readBytes(file); final byte[] unencryptedBytes = Encryptor.decrypt(encryptionKey, encryptedBytes); ByteArrayInputStream stream = null; try { stream = new ByteArrayInputStream(unencryptedBytes); DataInputStream in = new DataInputStream(stream); Constructor<T> constructor = cls.getDeclaredConstructor(DataInput.class); constructor.setAccessible(true); return constructor.newInstance(in); } finally { Util.ensureClosed(stream); } } catch (Exception e) { throw new IOException("Unable to instantiate class: " + cls, e); } } }
public static byte[] readBytes(File file) throws IOException { ByteArrayOutputStream output = null; try { output = new ByteArrayOutputStream(); appendFileToStream(file, output); return output.toByteArray(); } finally { ensureClosed(output); } }
if (Util.isMimeTypeImage(storedFile.getMimeType())) { ApptentiveLog.v(PAYLOADS, "Appending image attachment."); ImageUtil.appendScaledDownImageToStream(storedFile.getSourceUriOrPath(), attachmentBytes); } else { ApptentiveLog.v(PAYLOADS, "Appending non-image attachment."); Util.appendFileToStream(new File(storedFile.getSourceUriOrPath()), attachmentBytes); continue; } finally { Util.ensureClosed(fileInputStream);
protected ConversationItem(PayloadType type) { super(type); double seconds = Util.currentTimeSeconds(); int utcOffset = Util.getUtcOffset(); setClientCreatedAt(seconds); setClientCreatedAtUtcOffset(utcOffset); }
/** * Writes an object ot an encrypted file */ public static void serialize(File file, SerializableObject object, @NonNull EncryptionKey encryptionKey) throws IOException { ByteArrayOutputStream bos = null; DataOutputStream dos = null; try { bos = new ByteArrayOutputStream(); dos = new DataOutputStream(bos); object.writeExternal(dos); final byte[] unencryptedBytes = bos.toByteArray(); final byte[] encryptedBytes = Encryptor.encrypt(encryptionKey, unencryptedBytes); Util.writeAtomically(file, encryptedBytes); } catch (Exception e) { throw new IOException(e); } finally { Util.ensureClosed(bos); Util.ensureClosed(dos); } }
/** * This method creates a cached file exactly copying from the input stream. * * @param sourceUrl the source file path or uri string * @param localFilePath the cache file path string * @param mimeType the mimeType of the source inputstream * @return null if failed, otherwise a StoredFile object */ public static StoredFile createLocalStoredFile(String sourceUrl, String localFilePath, String mimeType) { InputStream is = null; try { Context context = ApptentiveInternal.getInstance().getApplicationContext(); if (URLUtil.isContentUrl(sourceUrl) && context != null) { Uri uri = Uri.parse(sourceUrl); is = context.getContentResolver().openInputStream(uri); } else { File file = new File(sourceUrl); is = new FileInputStream(file); } return createLocalStoredFile(is, sourceUrl, localFilePath, mimeType); } catch (FileNotFoundException e) { return null; } finally { ensureClosed(is); } }
@Test public void getEncryptedFilename() { File file = new File("/data/user/0/test.bin"); File encryptedFile = Util.getEncryptedFilename(file); assertEquals(new File("/data/user/0/test.bin.encrypted"), encryptedFile); File decryptedFile = Util.getUnencryptedFilename(encryptedFile); assertEquals(file, decryptedFile); } }
public ConversationMetadataItem(DataInput in) throws IOException { localConversationId = in.readUTF(); conversationId = readNullableUTF(in); conversationToken = readNullableUTF(in); dataFile = getEncryptedFilename(new File(in.readUTF())); messagesFile = getEncryptedFilename(new File(in.readUTF())); conversationState = ConversationState.valueOf(in.readByte()); String conversationEncryptionKeyHex = readNullableUTF(in); conversationEncryptionKey = conversationEncryptionKeyHex != null ? new EncryptionKey(conversationEncryptionKeyHex) : null; userId = readNullableUTF(in); }
public void migrateLegacyStorage() { try { File unencryptedFile = Util.getUnencryptedFilename(file); if (unencryptedFile.exists()) { try { List<MessageEntry> entries = readFromLegacyFile(unencryptedFile); messageEntries.addAll(entries); writeToFile(); } finally { boolean deleted = unencryptedFile.delete(); ApptentiveLog.d(CONVERSATION, "Deleted legacy message storage: %b", deleted); } } } catch (Exception e) { ApptentiveLog.e(CONVERSATION, e, "Exception while migrating messages"); logException(e); } }
public static @Nullable String readText(File file) { try { FileInputStream stream = new FileInputStream(file); return Util.readStringFromInputStream(stream, "UTF-8"); } catch (Exception e) { return null; } } }
EventPayload expected = new EventPayload(loadTextAssetAsString(TEST_DATA_DIR + "testExtendedDataEvents.json")); expected.setClientCreatedAtUtcOffset(Util.getUtcOffset());
private void setStatusBarColor() { // Changing status bar color is a post-21 feature if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { int statusBarDefaultColor = ApptentiveInternal.getInstance().getDefaultStatusBarColor(); int overlayColor = ContextCompat.getColor(this, R.color.apptentive_activity_frame_dark); getWindow().setStatusBarColor(Util.alphaMixColors(statusBarDefaultColor, overlayColor)); } }
Resources.Theme theme = Util.buildApptentiveInteractionTheme(context); if (theme != null) { TypedValue icon = new TypedValue();
/** * Reads error response and returns it as a string. Handles gzipped streams. * * @param connection Current connection * @return Error response as String * @throws IOException */ public static String getErrorResponse(HttpURLConnection connection, boolean isZipped) throws IOException { if (connection != null) { InputStream is = null; try { is = connection.getErrorStream(); if (is != null) { if (isZipped) { is = new GZIPInputStream(is); } } return Util.readStringFromInputStream(is, "UTF-8"); } finally { Util.ensureClosed(is); } } return null; } }