@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)); } }
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) @TypeConverter public static @NonNull Data fromByteArray(@NonNull byte[] bytes) throws IllegalStateException { if (bytes.length > MAX_DATA_BYTES) { throw new IllegalStateException( "Data cannot occupy more than " + MAX_DATA_BYTES + " bytes 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) { Log.e(TAG, "Error in Data#fromByteArray: ", e); } finally { if (objectInputStream != null) { try { objectInputStream.close(); } catch (IOException e) { Log.e(TAG, "Error in Data#fromByteArray: ", e); } } try { inputStream.close(); } catch (IOException e) { Log.e(TAG, "Error in Data#fromByteArray: ", e); } } return new Data(map); }
Data { @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) @TypeConverter public static @NonNull Data fromByteArray(@NonNull byte[] bytes) throws IllegalStateException { if (bytes.length > MAX_DATA_BYTES) { throw new IllegalStateException( "Data cannot occupy more than " + MAX_DATA_BYTES + " bytes 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) { Log.e(TAG, "Error in Data#fromByteArray: ", e); } finally { if (objectInputStream != null) { try { objectInputStream.close(); } catch (IOException e) { Log.e(TAG, "Error in Data#fromByteArray: ", e); } } try { inputStream.close(); } catch (IOException e) { Log.e(TAG, "Error in Data#fromByteArray: ", e); } } return new Data(map); } }
Data { @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) @TypeConverter public static @NonNull Data fromByteArray(@NonNull byte[] bytes) throws IllegalStateException { if (bytes.length > MAX_DATA_BYTES) { throw new IllegalStateException( "Data cannot occupy more than " + MAX_DATA_BYTES + " bytes 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) { Log.e(TAG, "Error in Data#fromByteArray: ", e); } finally { if (objectInputStream != null) { try { objectInputStream.close(); } catch (IOException e) { Log.e(TAG, "Error in Data#fromByteArray: ", e); } } try { inputStream.close(); } catch (IOException e) { Log.e(TAG, "Error in Data#fromByteArray: ", e); } } return new Data(map); } Data();  Data(@NonNull Data other);   Data(@NonNull Map<String, ?> values); }
Data { @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) @TypeConverter public static @NonNull Data fromByteArray(@NonNull byte[] bytes) throws IllegalStateException { if (bytes.length > MAX_DATA_BYTES) { throw new IllegalStateException( "Data cannot occupy more than " + MAX_DATA_BYTES + " bytes 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) { Log.e(TAG, "Error in Data#fromByteArray: ", e); } finally { if (objectInputStream != null) { try { objectInputStream.close(); } catch (IOException e) { Log.e(TAG, "Error in Data#fromByteArray: ", e); } } try { inputStream.close(); } catch (IOException e) { Log.e(TAG, "Error in Data#fromByteArray: ", e); } } return new Data(map); } Data();  Data(@NonNull Data other);   Data(@NonNull Map<String, ?> values); boolean getBoolean(@NonNull String key, boolean defaultValue); @Nullable boolean[] getBooleanArray(@NonNull String key); byte getByte(@NonNull String key, byte defaultValue); @Nullable byte[] getByteArray(@NonNull String key); int getInt(@NonNull String key, int defaultValue); @Nullable int[] getIntArray(@NonNull String key); long getLong(@NonNull String key, long defaultValue); @Nullable long[] getLongArray(@NonNull String key); float getFloat(@NonNull String key, float defaultValue); @Nullable float[] getFloatArray(@NonNull String key); double getDouble(@NonNull String key, double defaultValue); @Nullable double[] getDoubleArray(@NonNull String key); @Nullable String getString(@NonNull String key); @Nullable String[] getStringArray(@NonNull String key); @NonNull Map<String, Object> getKeyValueMap(); @VisibleForTesting @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) int size(); @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) @TypeConverter static @NonNull byte[] toByteArray(@NonNull Data data); @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) @TypeConverter static @NonNull Data fromByteArray(@NonNull byte[] bytes); @Override boolean equals(Object o); @Override int hashCode(); }
Data { @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) @TypeConverter public static @NonNull Data fromByteArray(@NonNull byte[] bytes) throws IllegalStateException { if (bytes.length > MAX_DATA_BYTES) { throw new IllegalStateException( "Data cannot occupy more than " + MAX_DATA_BYTES + " bytes 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) { Log.e(TAG, "Error in Data#fromByteArray: ", e); } finally { if (objectInputStream != null) { try { objectInputStream.close(); } catch (IOException e) { Log.e(TAG, "Error in Data#fromByteArray: ", e); } } try { inputStream.close(); } catch (IOException e) { Log.e(TAG, "Error in Data#fromByteArray: ", e); } } return new Data(map); } Data();  Data(@NonNull Data other);   Data(@NonNull Map<String, ?> values); boolean getBoolean(@NonNull String key, boolean defaultValue); @Nullable boolean[] getBooleanArray(@NonNull String key); byte getByte(@NonNull String key, byte defaultValue); @Nullable byte[] getByteArray(@NonNull String key); int getInt(@NonNull String key, int defaultValue); @Nullable int[] getIntArray(@NonNull String key); long getLong(@NonNull String key, long defaultValue); @Nullable long[] getLongArray(@NonNull String key); float getFloat(@NonNull String key, float defaultValue); @Nullable float[] getFloatArray(@NonNull String key); double getDouble(@NonNull String key, double defaultValue); @Nullable double[] getDoubleArray(@NonNull String key); @Nullable String getString(@NonNull String key); @Nullable String[] getStringArray(@NonNull String key); @NonNull Map<String, Object> getKeyValueMap(); @VisibleForTesting @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) int size(); @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) @TypeConverter static @NonNull byte[] toByteArray(@NonNull Data data); @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) @TypeConverter static @NonNull Data fromByteArray(@NonNull byte[] bytes); @Override boolean equals(Object o); @Override int hashCode(); static final Data EMPTY; static final int MAX_DATA_BYTES; }