com.google.protobuf.nano.MessageNano Java Examples

The following examples show how to use com.google.protobuf.nano.MessageNano. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ProtobufDecoderNano.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out)
        throws Exception {
    final byte[] array;
    final int offset;
    final int length = msg.readableBytes();
    if (msg.hasArray()) {
        array = msg.array();
        offset = msg.arrayOffset() + msg.readerIndex();
    } else {
        array = new byte[length];
        msg.getBytes(msg.readerIndex(), array, 0, length);
        offset = 0;
    }
    MessageNano prototype = clazz.getConstructor().newInstance();
    out.add(MessageNano.mergeFrom(prototype, array, offset, length));
}
 
Example #2
Source File: LauncherBackupHelper.java    From Trebuchet with GNU General Public License v3.0 5 votes vote down vote up
private void writeRowToBackup(String backupKey, MessageNano proto,
        BackupDataOutput data) throws IOException {
    byte[] blob = writeCheckedBytes(proto);
    data.writeEntityHeader(backupKey, blob.length);
    data.writeEntityData(blob, blob.length);
    mBackupDataWasUpdated = true;
    if (VERBOSE) Log.v(TAG, "Writing New entry " + backupKey);
}
 
Example #3
Source File: LauncherBackupHelper.java    From TurboLauncher with Apache License 2.0 5 votes vote down vote up
/** Wrap a proto in a CheckedMessage and compute the checksum. */
private byte[] writeCheckedBytes(MessageNano proto) {
    CheckedMessage wrapper = new CheckedMessage();
    wrapper.payload = MessageNano.toByteArray(proto);
    CRC32 checksum = new CRC32();
    checksum.update(wrapper.payload);
    wrapper.checksum = checksum.getValue();
    return MessageNano.toByteArray(wrapper);
}
 
Example #4
Source File: LauncherBackupHelper.java    From TurboLauncher with Apache License 2.0 5 votes vote down vote up
/** Deserialize a widget from persistence, after verifying checksum wrapper. */
private Widget unpackWidget(byte[] buffer, int offset, int dataSize)
        throws InvalidProtocolBufferNanoException {
    Widget widget = new Widget();
    MessageNano.mergeFrom(widget, readCheckedBytes(buffer, offset, dataSize));
    if (VERBOSE) Log.v(TAG, "unpacked widget " + widget.provider);
    return widget;
}
 
Example #5
Source File: LauncherBackupHelper.java    From TurboLauncher with Apache License 2.0 5 votes vote down vote up
/** Deserialize an icon resource from persistence, after verifying checksum wrapper. */
private static Resource unpackIcon(byte[] buffer, int offset, int dataSize)
        throws InvalidProtocolBufferNanoException {
    Resource res = new Resource();
    MessageNano.mergeFrom(res, readCheckedBytes(buffer, offset, dataSize));
    if (VERBOSE) Log.v(TAG, "unpacked icon " + res.dpi + "/" + res.data.length);
    return res;
}
 
Example #6
Source File: LauncherBackupHelper.java    From TurboLauncher with Apache License 2.0 5 votes vote down vote up
/** Deserialize a Screen from persistence, after verifying checksum wrapper. */
private ContentValues unpackScreen(byte[] buffer, int offset, int dataSize)
        throws InvalidProtocolBufferNanoException {
    Screen screen = new Screen();
    MessageNano.mergeFrom(screen, readCheckedBytes(buffer, offset, dataSize));
    if (VERBOSE) Log.v(TAG, "unpacked screen " + screen.id + "/" + screen.rank);
    ContentValues values = new ContentValues();
    values.put(WorkspaceScreens._ID, screen.id);
    values.put(WorkspaceScreens.SCREEN_RANK, screen.rank);
    return values;
}
 
Example #7
Source File: LauncherBackupHelper.java    From LB-Launcher with Apache License 2.0 5 votes vote down vote up
/** Wrap a proto in a CheckedMessage and compute the checksum. */
private byte[] writeCheckedBytes(MessageNano proto) {
    CheckedMessage wrapper = new CheckedMessage();
    wrapper.payload = MessageNano.toByteArray(proto);
    CRC32 checksum = new CRC32();
    checksum.update(wrapper.payload);
    wrapper.checksum = checksum.getValue();
    return MessageNano.toByteArray(wrapper);
}
 
Example #8
Source File: ProtoUtilsTest.java    From PainlessMusicPlayer with Apache License 2.0 5 votes vote down vote up
@Test
public void testToByteArray() throws Exception {
    final SettingsProto.Settings message = new SettingsProto.Settings();
    message.scrobbleEnabled = true;
    message.theme = 666;

    final byte[] data = ProtoUtils.toByteArray(message);
    assertNotNull(data);

    final SettingsProto.Settings fromBytes = MessageNano.mergeFrom(
            new SettingsProto.Settings(), data);

    assertEquals(message.scrobbleEnabled, fromBytes.scrobbleEnabled);
    assertEquals(message.theme, fromBytes.theme);
}
 
Example #9
Source File: ProtoUtils.java    From PainlessMusicPlayer with Apache License 2.0 5 votes vote down vote up
@NonNull
public static <T extends MessageNano> T readFromFileNonNull(@NonNull final Context context,
        @NonNull final String fileName,
        @NonNull final T obj) {
    final T message = readFromFile(context, fileName, obj);
    return message != null ? message : obj;
}
 
Example #10
Source File: ProtoUtils.java    From PainlessMusicPlayer with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #11
Source File: ProtoUtils.java    From PainlessMusicPlayer with Apache License 2.0 5 votes vote down vote up
public static void writeToFile(@NonNull final Context context,
        @NonNull final String fileName,
        @NonNull final MessageNano messageNano) {
    final byte[] output;
    try {
        output = toByteArray(messageNano);
    } catch (IOException e) {
        Log.w(TAG, e);
        return;
    }
    FileUtils.writeOrDeletePrivateFile(context, fileName, output);
}
 
Example #12
Source File: LauncherBackupHelper.java    From TurboLauncher with Apache License 2.0 5 votes vote down vote up
/** Unwrap a proto message from a CheckedMessage, verifying the checksum. */
private static byte[] readCheckedBytes(byte[] buffer, int offset, int dataSize)
        throws InvalidProtocolBufferNanoException {
    CheckedMessage wrapper = new CheckedMessage();
    MessageNano.mergeFrom(wrapper, buffer, offset, dataSize);
    CRC32 checksum = new CRC32();
    checksum.update(wrapper.payload);
    if (wrapper.checksum != checksum.getValue()) {
        throw new InvalidProtocolBufferNanoException("checksum does not match");
    }
    return wrapper.payload;
}
 
Example #13
Source File: LauncherBackupHelper.java    From Trebuchet with GNU General Public License v3.0 5 votes vote down vote up
/** Unwrap a proto message from a CheckedMessage, verifying the checksum. */
private static byte[] readCheckedBytes(byte[] buffer, int dataSize)
        throws InvalidProtocolBufferNanoException {
    CheckedMessage wrapper = new CheckedMessage();
    MessageNano.mergeFrom(wrapper, buffer, 0, dataSize);
    CRC32 checksum = new CRC32();
    checksum.update(wrapper.payload);
    if (wrapper.checksum != checksum.getValue()) {
        throw new InvalidProtocolBufferNanoException("checksum does not match");
    }
    return wrapper.payload;
}
 
Example #14
Source File: LauncherBackupHelper.java    From Trebuchet with GNU General Public License v3.0 5 votes vote down vote up
/** Wrap a proto in a CheckedMessage and compute the checksum. */
private byte[] writeCheckedBytes(MessageNano proto) {
    CheckedMessage wrapper = new CheckedMessage();
    wrapper.payload = MessageNano.toByteArray(proto);
    CRC32 checksum = new CRC32();
    checksum.update(wrapper.payload);
    wrapper.checksum = checksum.getValue();
    return MessageNano.toByteArray(wrapper);
}
 
Example #15
Source File: LauncherBackupHelper.java    From Trebuchet with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Deserialize a proto after verifying checksum wrapper.
 */
private <T extends MessageNano> T unpackProto(T proto, byte[] buffer, int dataSize)
        throws InvalidProtocolBufferNanoException {
    MessageNano.mergeFrom(proto, readCheckedBytes(buffer, dataSize));
    if (DEBUG) Log.d(TAG, "unpacked proto " + proto);
    return proto;
}
 
Example #16
Source File: StorageDelegate.java    From AndroidChromium with Apache License 2.0 5 votes vote down vote up
/**
 * Update tab entries based on metadata.
 * @param metadataBytes Metadata from last time Chrome was alive.
 * @param entryMap Map to fill with {@link DocumentTabModel.Entry}s about Tabs.
 * @param recentlyClosedTabIdList List to fill with IDs of recently closed tabs.
 */
private void updateTabEntriesFromMetadata(byte[] metadataBytes, SparseArray<Entry> entryMap,
        List<Integer> recentlyClosedTabIdList) {
    if (metadataBytes != null) {
        DocumentList list = null;
        try {
            list = MessageNano.mergeFrom(new DocumentList(), metadataBytes);
        } catch (IOException e) {
            Log.e(TAG, "I/O exception", e);
        }
        if (list == null) return;

        for (int i = 0; i < list.entries.length; i++) {
            DocumentEntry savedEntry = list.entries[i];
            int tabId = savedEntry.tabId;

            // If the tab ID isn't in the list, it must have been closed after Chrome died.
            if (entryMap.indexOfKey(tabId) < 0) {
                recentlyClosedTabIdList.add(tabId);
                continue;
            }

            // Restore information about the Tab.
            entryMap.get(tabId).canGoBack = savedEntry.canGoBack;
        }
    }
}
 
Example #17
Source File: LauncherBackupHelper.java    From LB-Launcher with Apache License 2.0 5 votes vote down vote up
/** Unwrap a proto message from a CheckedMessage, verifying the checksum. */
private static byte[] readCheckedBytes(byte[] buffer, int dataSize)
        throws InvalidProtocolBufferNanoException {
    CheckedMessage wrapper = new CheckedMessage();
    MessageNano.mergeFrom(wrapper, buffer, 0, dataSize);
    CRC32 checksum = new CRC32();
    checksum.update(wrapper.payload);
    if (wrapper.checksum != checksum.getValue()) {
        throw new InvalidProtocolBufferNanoException("checksum does not match");
    }
    return wrapper.payload;
}
 
Example #18
Source File: StorageDelegate.java    From delion with Apache License 2.0 5 votes vote down vote up
/**
 * Update tab entries based on metadata.
 * @param metadataBytes Metadata from last time Chrome was alive.
 * @param entryMap Map to fill with {@link DocumentTabModel.Entry}s about Tabs.
 * @param recentlyClosedTabIdList List to fill with IDs of recently closed tabs.
 */
private void updateTabEntriesFromMetadata(byte[] metadataBytes, SparseArray<Entry> entryMap,
        List<Integer> recentlyClosedTabIdList) {
    if (metadataBytes != null) {
        DocumentList list = null;
        try {
            list = MessageNano.mergeFrom(new DocumentList(), metadataBytes);
        } catch (IOException e) {
            Log.e(TAG, "I/O exception", e);
        }
        if (list == null) return;

        for (int i = 0; i < list.entries.length; i++) {
            DocumentEntry savedEntry = list.entries[i];
            int tabId = savedEntry.tabId;

            // If the tab ID isn't in the list, it must have been closed after Chrome died.
            if (entryMap.indexOfKey(tabId) < 0) {
                recentlyClosedTabIdList.add(tabId);
                continue;
            }

            // Restore information about the Tab.
            entryMap.get(tabId).canGoBack = savedEntry.canGoBack;
        }
    }
}
 
Example #19
Source File: ProtobufEncoderNano.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
protected void encode(
        ChannelHandlerContext ctx, MessageNano msg, List<Object> out) throws Exception {
    final int size = msg.getSerializedSize();
    final ByteBuf buffer = ctx.alloc().heapBuffer(size, size);
    final byte[] array = buffer.array();
    CodedOutputByteBufferNano cobbn = CodedOutputByteBufferNano.newInstance(array,
            buffer.arrayOffset(), buffer.capacity());
    msg.writeTo(cobbn);
    buffer.writerIndex(size);
    out.add(buffer);
}
 
Example #20
Source File: NanoUtilsTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testRoundTrip() {
  Message m = new Message();
  m.i = 2;
  m.b = true;
  m.s = "string";
  Message m2 = marshaller.parse(marshaller.stream(m));
  assertNotSame(m, m2);
  assertEquals(2, m2.i);
  assertEquals(true, m2.b);
  assertEquals("string", m2.s);
  assertTrue(MessageNano.messageNanoEquals(m, m2));
}
 
Example #21
Source File: LauncherBackupHelper.java    From LB-Launcher with Apache License 2.0 5 votes vote down vote up
/**
 * Deserialize a proto after verifying checksum wrapper.
 */
private <T extends MessageNano> T unpackProto(T proto, byte[] buffer, int dataSize)
        throws InvalidProtocolBufferNanoException {
    MessageNano.mergeFrom(proto, readCheckedBytes(buffer, dataSize));
    if (DEBUG) Log.d(TAG, "unpacked proto " + proto);
    return proto;
}
 
Example #22
Source File: AndroidService.java    From 365browser with Apache License 2.0 4 votes vote down vote up
public byte[] toByteArray() {
  return MessageNano.toByteArray(toMessageNano());
}
 
Example #23
Source File: ClientProtocol.java    From 365browser with Apache License 2.0 4 votes vote down vote up
public byte[] toByteArray() {
  return MessageNano.toByteArray(toMessageNano());
}
 
Example #24
Source File: AndroidListenerProtocol.java    From 365browser with Apache License 2.0 4 votes vote down vote up
public byte[] toByteArray() {
  return MessageNano.toByteArray(toMessageNano());
}
 
Example #25
Source File: ClientProtocol.java    From 365browser with Apache License 2.0 4 votes vote down vote up
public byte[] toByteArray() {
  return MessageNano.toByteArray(toMessageNano());
}
 
Example #26
Source File: AndroidChannel.java    From 365browser with Apache License 2.0 4 votes vote down vote up
public byte[] toByteArray() {
  return MessageNano.toByteArray(toMessageNano());
}
 
Example #27
Source File: AndroidService.java    From 365browser with Apache License 2.0 4 votes vote down vote up
public byte[] toByteArray() {
  return MessageNano.toByteArray(toMessageNano());
}
 
Example #28
Source File: AndroidService.java    From 365browser with Apache License 2.0 4 votes vote down vote up
public byte[] toByteArray() {
  return MessageNano.toByteArray(toMessageNano());
}
 
Example #29
Source File: AndroidService.java    From 365browser with Apache License 2.0 4 votes vote down vote up
public byte[] toByteArray() {
  return MessageNano.toByteArray(toMessageNano());
}
 
Example #30
Source File: JavaClient.java    From 365browser with Apache License 2.0 4 votes vote down vote up
public byte[] toByteArray() {
  return MessageNano.toByteArray(toMessageNano());
}