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); } }
protected Object deserialize(File file) throws SerializerException { FileInputStream fis = null; ObjectInputStream ois = null; try { fis = new FileInputStream(file); ois = new ObjectInputStream(fis); return ois.readObject(); } catch (Exception e) { throw new SerializerException(e); } finally { Util.ensureClosed(fis); Util.ensureClosed(ois); } } }
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 byte[] readBytes(File file) throws IOException { ByteArrayOutputStream output = null; try { output = new ByteArrayOutputStream(); appendFileToStream(file, output); return output.toByteArray(); } finally { ensureClosed(output); } }
@Override protected boolean execute(Conversation conversation) { ByteArrayInputStream is = null; try { is = new ByteArrayInputStream(content); sendAttachmentFile(is, mimeType); } finally { Util.ensureClosed(is); } return true; } }, "send attachment file");
@Override protected void serialize(FileOutputStream stream, Object object) throws Exception { ByteArrayOutputStream bos = null; ObjectOutputStream oos = null; try { bos = new ByteArrayOutputStream(); oos = new ObjectOutputStream(bos); oos.writeObject(object); final byte[] unencryptedBytes = bos.toByteArray(); final byte[] encryptedBytes = Encryptor.encrypt(encryptionKey, unencryptedBytes); stream.write(encryptedBytes); // TODO: should we write using a buffer? } finally { Util.ensureClosed(bos); Util.ensureClosed(oos); } }
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 String readStringFromInputStream(InputStream is, String charEncoding) { Reader reader = null; StringBuilder out = new StringBuilder(); final char[] buf = new char[8196]; try { reader = new InputStreamReader(is, charEncoding); while (true) { int read = reader.read(buf, 0, 8196); if (read < 0) { break; } out.append(buf, 0, read); } } catch (Exception e) { // } finally { Util.ensureClosed(reader); } return out.toString(); }
public static void writeText(File file, List<String> text, boolean append) throws IOException { if (text == null) { throw new IllegalArgumentException("'text' is null"); } PrintStream output = null; try { output = openTextWrite(file, append); for (String line : text) { output.println(line); } } finally { ensureClosed(output); } }
/** * Reads an object from a file */ public static <T extends SerializableObject> T deserialize(File file, Class<T> cls) throws IOException { FileInputStream stream = null; try { stream = new FileInputStream(file); DataInputStream in = new DataInputStream(stream); try { Constructor<T> constructor = cls.getDeclaredConstructor(DataInput.class); constructor.setAccessible(true); return constructor.newInstance(in); } catch (Exception e) { throw new IOException("Unable to instantiate class: " + cls, e); } } finally { Util.ensureClosed(stream); } }
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 void writeBytes(File file, byte[] bytes) throws IOException { if (file == null) { throw new IllegalArgumentException("'file' is null"); } if (bytes == null) { throw new IllegalArgumentException("'bytes' is null"); } File parentFile = file.getParentFile(); if (!parentFile.exists() && !parentFile.mkdirs()) { throw new IOException("Parent file could not be created: " + parentFile); } ByteArrayInputStream input = null; FileOutputStream output = null; try { input = new ByteArrayInputStream(bytes); output = new FileOutputStream(file); copy(input, output); } finally { ensureClosed(input); ensureClosed(output); } }
private static List<MessageEntry> readFromLegacyFile(File file) throws IOException { DataInputStream dis = null; try { dis = new DataInputStream(new FileInputStream(file)); byte version = dis.readByte(); if (version != VERSION) { throw new IOException("Unsupported binary version: " + version); } int entryCount = dis.readInt(); List<MessageEntry> entries = new ArrayList<>(); for (int i = 0; i < entryCount; ++i) { entries.add(new MessageEntry(dis)); } return entries; } finally { Util.ensureClosed(dis); } }
/** * 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; } }
/** * 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); } }
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); } } }
@Override protected Object deserialize(File file) throws SerializerException { try { final byte[] encryptedBytes = Util.readBytes(file); final byte[] unencryptedBytes = Encryptor.decrypt(encryptionKey, encryptedBytes); ByteArrayInputStream bis = null; ObjectInputStream ois = null; try { bis = new ByteArrayInputStream(unencryptedBytes); ois = new ObjectInputStream(bis); return ois.readObject(); } catch (Exception e) { throw new SerializerException(e); } finally { Util.ensureClosed(bis); Util.ensureClosed(ois); } } catch (Exception e) { throw new SerializerException(e); } } }
public static String loadRawTextResourceAsString(Context context, int resourceId) { BufferedReader reader = null; try { StringBuilder builder = new StringBuilder(); reader = new BufferedReader(new InputStreamReader(context.getResources().openRawResource(resourceId))); char[] buf = new char[READ_BUF_LEN]; int count; while ((count = reader.read(buf, 0, READ_BUF_LEN)) != -1) { builder.append(buf, 0, count); } return builder.toString(); } catch (IOException e) { ApptentiveLog.e(e, "Error reading from raw resource with ID \"%d\"", resourceId); } finally { Util.ensureClosed(reader); } return null; }
public static String loadTextAssetAsString(Context context, String path) { AssetManager assetManager = context.getResources().getAssets(); BufferedReader reader = null; try { StringBuilder builder = new StringBuilder(); reader = new BufferedReader(new InputStreamReader(assetManager.open(path))); char[] buf = new char[READ_BUF_LEN]; int count; while ((count = reader.read(buf, 0, READ_BUF_LEN)) != -1) { builder.append(buf, 0, count); } return builder.toString(); } catch (IOException e) { ApptentiveLog.e(e, "Error reading from file \"%s\"", path); } finally { Util.ensureClosed(reader); } return null; }
@Test public void eventLabelCreation() { BufferedReader reader = null; try { reader = openBufferedReaderFromFileAsset(TEST_DATA_DIR + "testEventLabelCreation.txt"); // Make sure the test file isn't empty. reader.mark(Integer.MAX_VALUE); assertNotNull(reader.readLine()); reader.reset(); String vendor; while ((vendor = reader.readLine()) != null) { String interaction = reader.readLine(); String eventName = reader.readLine(); String expected = reader.readLine(); String result = EngagementModule.generateEventLabel(vendor, interaction, eventName); ApptentiveLog.i(".\nexpected: %s\nresult: %s", expected, result); assertTrue(result.equals(expected)); } } catch (IOException e) { ApptentiveLog.e(e, "Error reading asset."); throw new RuntimeException(e); } finally { Util.ensureClosed(reader); } } }