@Test public void testReadFromFile() throws Exception { final SettingsProto.Settings message = new SettingsProto.Settings(); message.scrobbleEnabled = true; message.theme = 666; final String fileName = "protoUtilsReadFromFile"; final Context context = RuntimeEnvironment.application; context.deleteFile(fileName); final OutputStream os = context.openFileOutput(fileName, Context.MODE_PRIVATE); ByteStreams.copy(new ByteArrayInputStream(ProtoUtils.toByteArray(message)), os); os.close(); final SettingsProto.Settings read = ProtoUtils.readFromFile( context, fileName, new SettingsProto.Settings()); assertNotNull(read); assertEquals(message.scrobbleEnabled, read.scrobbleEnabled); assertEquals(message.theme, read.theme); }
@Nullable public static <T extends MessageNano> T readFromFile(@NonNull final Context context, @NonNull final String fileName, @NonNull final T obj) { T message = null; try { final byte[] bytes = FileUtils.readPrivateFile(context, fileName); message = MessageNano.mergeFrom(obj, bytes); } catch (IOException e) { Log.w(TAG, e); } return message; }
ProtoUtils { @Nullable public static <T extends MessageNano> T readFromFile(@NonNull final Context context, @NonNull final String fileName, @NonNull final T obj) { T message = null; try { final byte[] bytes = FileUtils.readPrivateFile(context, fileName); message = MessageNano.mergeFrom(obj, bytes); } catch (IOException e) { Log.w(TAG, e); } return message; } }
ProtoUtils { @Nullable public static <T extends MessageNano> T readFromFile(@NonNull final Context context, @NonNull final String fileName, @NonNull final T obj) { T message = null; try { final byte[] bytes = FileUtils.readPrivateFile(context, fileName); message = MessageNano.mergeFrom(obj, bytes); } catch (IOException e) { Log.w(TAG, e); } return message; } private  ProtoUtils(); }
ProtoUtils { @Nullable public static <T extends MessageNano> T readFromFile(@NonNull final Context context, @NonNull final String fileName, @NonNull final T obj) { T message = null; try { final byte[] bytes = FileUtils.readPrivateFile(context, fileName); message = MessageNano.mergeFrom(obj, bytes); } catch (IOException e) { Log.w(TAG, e); } return message; } private  ProtoUtils(); @NonNull static byte[] toByteArray(@NonNull final MessageNano messageNano); static void writeToFile(@NonNull final Context context,
            @NonNull final String fileName,
            @NonNull final MessageNano messageNano); @Nullable static T readFromFile(@NonNull final Context context,
            @NonNull final String fileName,
            @NonNull final T obj); @NonNull static T readFromFileNonNull(@NonNull final Context context,
            @NonNull final String fileName,
            @NonNull final T obj); }
ProtoUtils { @Nullable public static <T extends MessageNano> T readFromFile(@NonNull final Context context, @NonNull final String fileName, @NonNull final T obj) { T message = null; try { final byte[] bytes = FileUtils.readPrivateFile(context, fileName); message = MessageNano.mergeFrom(obj, bytes); } catch (IOException e) { Log.w(TAG, e); } return message; } private  ProtoUtils(); @NonNull static byte[] toByteArray(@NonNull final MessageNano messageNano); static void writeToFile(@NonNull final Context context,
            @NonNull final String fileName,
            @NonNull final MessageNano messageNano); @Nullable static T readFromFile(@NonNull final Context context,
            @NonNull final String fileName,
            @NonNull final T obj); @NonNull static T readFromFileNonNull(@NonNull final Context context,
            @NonNull final String fileName,
            @NonNull final T obj);  }