Line data Source code
1 : // Copyright (C) 2018 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.proto; 16 : 17 : import com.google.protobuf.ByteString; 18 : import com.google.protobuf.CodedOutputStream; 19 : import com.google.protobuf.MessageLite; 20 : import com.google.protobuf.Parser; 21 : import java.io.IOException; 22 : 23 : /** Static utilities for dealing with protobuf-based objects. */ 24 : public class Protos { 25 : /** 26 : * Serializes a proto to a byte array. 27 : * 28 : * <p>Guarantees deterministic serialization. No matter whether the use case cares about 29 : * determinism or not, always use this method in preference to {@link MessageLite#toByteArray()}, 30 : * which is not guaranteed deterministic. 31 : * 32 : * @param message the proto message to serialize. 33 : * @return a byte array with the message contents. 34 : */ 35 : public static byte[] toByteArray(MessageLite message) { 36 115 : byte[] bytes = new byte[message.getSerializedSize()]; 37 115 : CodedOutputStream cout = CodedOutputStream.newInstance(bytes); 38 115 : cout.useDeterministicSerialization(); 39 : try { 40 115 : message.writeTo(cout); 41 115 : cout.checkNoSpaceLeft(); 42 115 : return bytes; 43 0 : } catch (IOException e) { 44 0 : throw new IllegalStateException("exception writing to byte array", e); 45 : } 46 : } 47 : 48 : /** 49 : * Serializes a proto to a {@code ByteString}. 50 : * 51 : * <p>Guarantees deterministic serialization. No matter whether the use case cares about 52 : * determinism or not, always use this method in preference to {@link MessageLite#toByteString()}, 53 : * which is not guaranteed deterministic. 54 : * 55 : * @param message the proto message to serialize 56 : * @return a {@code ByteString} with the message contents 57 : */ 58 : public static ByteString toByteString(MessageLite message) { 59 2 : try (ByteString.Output bout = ByteString.newOutput(message.getSerializedSize())) { 60 2 : CodedOutputStream outputStream = CodedOutputStream.newInstance(bout); 61 2 : outputStream.useDeterministicSerialization(); 62 2 : message.writeTo(outputStream); 63 2 : outputStream.flush(); 64 2 : return bout.toByteString(); 65 0 : } catch (IOException e) { 66 0 : throw new IllegalStateException("exception writing to ByteString", e); 67 : } 68 : } 69 : 70 : /** 71 : * Parses a byte array to a protobuf message. 72 : * 73 : * @param parser parser for the proto type. 74 : * @param in byte array with the message contents. 75 : * @return parsed proto. 76 : */ 77 : public static <M extends MessageLite> M parseUnchecked(Parser<M> parser, byte[] in) { 78 : try { 79 22 : return parser.parseFrom(in); 80 1 : } catch (IOException e) { 81 1 : throw new IllegalArgumentException("exception parsing byte array to proto", e); 82 : } 83 : } 84 : 85 : /** 86 : * Parses a specific segment of a byte array to a protobuf message. 87 : * 88 : * @param parser parser for the proto type 89 : * @param in byte array with the message contents 90 : * @param offset offset in the byte array to start reading from 91 : * @param length amount of read bytes 92 : * @return parsed proto 93 : */ 94 : public static <M extends MessageLite> M parseUnchecked( 95 : Parser<M> parser, byte[] in, int offset, int length) { 96 : try { 97 101 : return parser.parseFrom(in, offset, length); 98 1 : } catch (IOException e) { 99 1 : throw new IllegalArgumentException("exception parsing byte array to proto", e); 100 : } 101 : } 102 : 103 : /** 104 : * Parses a {@code ByteString} to a protobuf message. 105 : * 106 : * @param parser parser for the proto type 107 : * @param byteString {@code ByteString} with the message contents 108 : * @return parsed proto 109 : */ 110 : public static <M extends MessageLite> M parseUnchecked(Parser<M> parser, ByteString byteString) { 111 : try { 112 1 : return parser.parseFrom(byteString); 113 1 : } catch (IOException e) { 114 1 : throw new IllegalArgumentException("exception parsing ByteString to proto", e); 115 : } 116 : } 117 : 118 : private Protos() {} 119 : }