@Test public void testSerializePastMaxSize() { int[] payload = new int[Data.MAX_DATA_BYTES + 1]; Data data = new Data.Builder().putIntArray("payload", payload).build(); boolean caughtIllegalStateException = false; try { Data.toByteArray(data); } catch (IllegalStateException e) { caughtIllegalStateException = true; } finally { assertThat(caughtIllegalStateException, is(true)); } }
@TypeConverter public static byte[] toByteArray(Data data) throws IllegalStateException { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ObjectOutputStream objectOutputStream = null; try { objectOutputStream = new ObjectOutputStream(outputStream); objectOutputStream.writeInt(data.size()); for (Map.Entry<String, Object> entry : data.mValues.entrySet()) { objectOutputStream.writeUTF(entry.getKey()); objectOutputStream.writeObject(entry.getValue()); } } catch (IOException e) { e.printStackTrace(); } finally { if (objectOutputStream != null) { try { objectOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (outputStream.size() > MAX_DATA_BYTES) { throw new IllegalStateException( "Data cannot occupy more than " + MAX_DATA_BYTES + "KB when serialized"); } return outputStream.toByteArray(); }
Data { @TypeConverter public static byte[] toByteArray(Data data) throws IllegalStateException { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ObjectOutputStream objectOutputStream = null; try { objectOutputStream = new ObjectOutputStream(outputStream); objectOutputStream.writeInt(data.size()); for (Map.Entry<String, Object> entry : data.mValues.entrySet()) { objectOutputStream.writeUTF(entry.getKey()); objectOutputStream.writeObject(entry.getValue()); } } catch (IOException e) { e.printStackTrace(); } finally { if (objectOutputStream != null) { try { objectOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (outputStream.size() > MAX_DATA_BYTES) { throw new IllegalStateException( "Data cannot occupy more than " + MAX_DATA_BYTES + "KB when serialized"); } return outputStream.toByteArray(); } }
Data { @TypeConverter public static byte[] toByteArray(Data data) throws IllegalStateException { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ObjectOutputStream objectOutputStream = null; try { objectOutputStream = new ObjectOutputStream(outputStream); objectOutputStream.writeInt(data.size()); for (Map.Entry<String, Object> entry : data.mValues.entrySet()) { objectOutputStream.writeUTF(entry.getKey()); objectOutputStream.writeObject(entry.getValue()); } } catch (IOException e) { e.printStackTrace(); } finally { if (objectOutputStream != null) { try { objectOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (outputStream.size() > MAX_DATA_BYTES) { throw new IllegalStateException( "Data cannot occupy more than " + MAX_DATA_BYTES + "KB when serialized"); } return outputStream.toByteArray(); } Data();   Data(Map<String, ?> values); }
Data { @TypeConverter public static byte[] toByteArray(Data data) throws IllegalStateException { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ObjectOutputStream objectOutputStream = null; try { objectOutputStream = new ObjectOutputStream(outputStream); objectOutputStream.writeInt(data.size()); for (Map.Entry<String, Object> entry : data.mValues.entrySet()) { objectOutputStream.writeUTF(entry.getKey()); objectOutputStream.writeObject(entry.getValue()); } } catch (IOException e) { e.printStackTrace(); } finally { if (objectOutputStream != null) { try { objectOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (outputStream.size() > MAX_DATA_BYTES) { throw new IllegalStateException( "Data cannot occupy more than " + MAX_DATA_BYTES + "KB when serialized"); } return outputStream.toByteArray(); } Data();   Data(Map<String, ?> values); boolean getBoolean(String key, boolean defaultValue); boolean[] getBooleanArray(String key); int getInt(String key, int defaultValue); int[] getIntArray(String key); long getLong(String key, long defaultValue); long[] getLongArray(String key); float getFloat(String key, float defaultValue); float[] getFloatArray(String key); double getDouble(String key, double defaultValue); double[] getDoubleArray(String key); String getString(String key, String defaultValue); String[] getStringArray(String key); Map<String, Object> getKeyValueMap(); @VisibleForTesting int size(); @TypeConverter static byte[] toByteArray(Data data); @TypeConverter static Data fromByteArray(byte[] bytes); @Override boolean equals(Object o); @Override int hashCode(); }
Data { @TypeConverter public static byte[] toByteArray(Data data) throws IllegalStateException { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ObjectOutputStream objectOutputStream = null; try { objectOutputStream = new ObjectOutputStream(outputStream); objectOutputStream.writeInt(data.size()); for (Map.Entry<String, Object> entry : data.mValues.entrySet()) { objectOutputStream.writeUTF(entry.getKey()); objectOutputStream.writeObject(entry.getValue()); } } catch (IOException e) { e.printStackTrace(); } finally { if (objectOutputStream != null) { try { objectOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (outputStream.size() > MAX_DATA_BYTES) { throw new IllegalStateException( "Data cannot occupy more than " + MAX_DATA_BYTES + "KB when serialized"); } return outputStream.toByteArray(); } Data();   Data(Map<String, ?> values); boolean getBoolean(String key, boolean defaultValue); boolean[] getBooleanArray(String key); int getInt(String key, int defaultValue); int[] getIntArray(String key); long getLong(String key, long defaultValue); long[] getLongArray(String key); float getFloat(String key, float defaultValue); float[] getFloatArray(String key); double getDouble(String key, double defaultValue); double[] getDoubleArray(String key); String getString(String key, String defaultValue); String[] getStringArray(String key); Map<String, Object> getKeyValueMap(); @VisibleForTesting int size(); @TypeConverter static byte[] toByteArray(Data data); @TypeConverter static Data fromByteArray(byte[] bytes); @Override boolean equals(Object o); @Override int hashCode(); static final Data EMPTY; static final int MAX_DATA_BYTES; }