31 package com.google.protobuf;
33 import java.io.IOException;
34 import java.nio.ByteBuffer;
35 import java.util.Queue;
44 return toByteArray(msg, Protobuf.getInstance().schemaFor(msg));
51 public static <T>
byte[]
toByteArray(
T msg, Schema<T> schema)
throws IOException {
52 BinaryWriter writer = BinaryWriter.newHeapInstance(BufferAllocator.unpooled());
53 schema.writeTo(msg, writer);
55 byte[] out =
new byte[writer.getTotalBytesWritten()];
57 Queue<AllocatedBuffer>
buffers = writer.complete();
68 if (out.length != outPos) {
69 throw new IllegalArgumentException(
"Failed to serialize test message");
76 if (Android.isOnAndroidDevice()) {
90 Schema<T> schema = Protobuf.getInstance().schemaFor(messageType);
91 T msg = schema.newInstance();
93 msg, BinaryReader.newInstance(ByteBuffer.wrap(
data),
true), extensionRegistry);
94 schema.makeImmutable(msg);
96 }
catch (IOException e) {
97 throw new RuntimeException(e);
104 Schema<T> schema = Protobuf.getInstance().schemaFor(messageType);
105 T msg = schema.newInstance();
106 schema.mergeFrom(msg,
data, 0,
data.length,
new ArrayDecoders.Registers());
107 schema.makeImmutable(msg);
109 }
catch (IOException e) {
110 throw new RuntimeException(e);