@Test public void testDeserializePastMaxSize() { byte[] payload = new byte[Data.MAX_DATA_BYTES + 1]; boolean caughtIllegalStateException = false; try { Data.fromByteArray(payload); } catch (IllegalStateException e) { caughtIllegalStateException = true; } finally { assertThat(caughtIllegalStateException, is(true)); } }
@TypeConverter public static Data fromByteArray(byte[] bytes) throws IllegalStateException { if (bytes.length > MAX_DATA_BYTES) { throw new IllegalStateException( "Data cannot occupy more than " + MAX_DATA_BYTES + "KB when serialized"); } Map<String, Object> map = new HashMap<>(); ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes); ObjectInputStream objectInputStream = null; try { objectInputStream = new ObjectInputStream(inputStream); for (int i = objectInputStream.readInt(); i > 0; i--) { map.put(objectInputStream.readUTF(), objectInputStream.readObject()); } } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } finally { if (objectInputStream != null) { try { objectInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } return new Data(map); }
Data { @TypeConverter public static Data fromByteArray(byte[] bytes) throws IllegalStateException { if (bytes.length > MAX_DATA_BYTES) { throw new IllegalStateException( "Data cannot occupy more than " + MAX_DATA_BYTES + "KB when serialized"); } Map<String, Object> map = new HashMap<>(); ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes); ObjectInputStream objectInputStream = null; try { objectInputStream = new ObjectInputStream(inputStream); for (int i = objectInputStream.readInt(); i > 0; i--) { map.put(objectInputStream.readUTF(), objectInputStream.readObject()); } } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } finally { if (objectInputStream != null) { try { objectInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } return new Data(map); } }
Data { @TypeConverter public static Data fromByteArray(byte[] bytes) throws IllegalStateException { if (bytes.length > MAX_DATA_BYTES) { throw new IllegalStateException( "Data cannot occupy more than " + MAX_DATA_BYTES + "KB when serialized"); } Map<String, Object> map = new HashMap<>(); ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes); ObjectInputStream objectInputStream = null; try { objectInputStream = new ObjectInputStream(inputStream); for (int i = objectInputStream.readInt(); i > 0; i--) { map.put(objectInputStream.readUTF(), objectInputStream.readObject()); } } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } finally { if (objectInputStream != null) { try { objectInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } return new Data(map); } Data();   Data(Map<String, ?> values); }
Data { @TypeConverter public static Data fromByteArray(byte[] bytes) throws IllegalStateException { if (bytes.length > MAX_DATA_BYTES) { throw new IllegalStateException( "Data cannot occupy more than " + MAX_DATA_BYTES + "KB when serialized"); } Map<String, Object> map = new HashMap<>(); ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes); ObjectInputStream objectInputStream = null; try { objectInputStream = new ObjectInputStream(inputStream); for (int i = objectInputStream.readInt(); i > 0; i--) { map.put(objectInputStream.readUTF(), objectInputStream.readObject()); } } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } finally { if (objectInputStream != null) { try { objectInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } return new Data(map); } 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 Data fromByteArray(byte[] bytes) throws IllegalStateException { if (bytes.length > MAX_DATA_BYTES) { throw new IllegalStateException( "Data cannot occupy more than " + MAX_DATA_BYTES + "KB when serialized"); } Map<String, Object> map = new HashMap<>(); ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes); ObjectInputStream objectInputStream = null; try { objectInputStream = new ObjectInputStream(inputStream); for (int i = objectInputStream.readInt(); i > 0; i--) { map.put(objectInputStream.readUTF(), objectInputStream.readObject()); } } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } finally { if (objectInputStream != null) { try { objectInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } return new Data(map); } 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; }