Move ProtoCacheSerializers to a more common location

We intend to reuse the methods of ProtoCacheSerializers for other
protobuf conversions unrelated to CacheSerializers.

Change-Id: I59acfd037969b1a0ae8d607cf8846876ccafbd16
This commit is contained in:
Alice Kober-Sotzek
2018-12-07 17:15:58 +01:00
parent fb51e6aa9f
commit 1015e95497
16 changed files with 61 additions and 56 deletions

View File

@@ -4,6 +4,7 @@ java_library(
visibility = ["//visibility:public"],
deps = [
"//java/com/google/gerrit/common:annotations",
"//java/com/google/gerrit/proto",
"//lib:guava",
"//lib:gwtorm",
"//lib:protobuf",

View File

@@ -1,88 +0,0 @@
// Copyright (C) 2018 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server.cache.serialize;
import com.google.gwtorm.protobuf.ProtobufCodec;
import com.google.protobuf.ByteString;
import com.google.protobuf.CodedOutputStream;
import com.google.protobuf.MessageLite;
import com.google.protobuf.Parser;
import java.io.IOException;
/** Static utilities for writing protobuf-based {@link CacheSerializer} implementations. */
public class ProtoCacheSerializers {
/**
* Serializes a proto to a byte array.
*
* <p>Guarantees deterministic serialization and thus is suitable for use in persistent caches.
* Should be used in preference to {@link MessageLite#toByteArray()}, which is not guaranteed
* deterministic.
*
* @param message the proto message to serialize.
* @return a byte array with the message contents.
*/
public static byte[] toByteArray(MessageLite message) {
byte[] bytes = new byte[message.getSerializedSize()];
CodedOutputStream cout = CodedOutputStream.newInstance(bytes);
cout.useDeterministicSerialization();
try {
message.writeTo(cout);
cout.checkNoSpaceLeft();
return bytes;
} catch (IOException e) {
throw new IllegalStateException("exception writing to byte array", e);
}
}
/**
* Serializes an object to a {@link ByteString} using a protobuf codec.
*
* <p>Guarantees deterministic serialization and thus is suitable for use in persistent caches.
* Should be used in preference to {@link ProtobufCodec#encodeToByteString(Object)}, which is not
* guaranteed deterministic.
*
* @param object the object to serialize.
* @param codec codec for serializing.
* @return a {@code ByteString} with the message contents.
*/
public static <T> ByteString toByteString(T object, ProtobufCodec<T> codec) {
try (ByteString.Output bout = ByteString.newOutput()) {
CodedOutputStream cout = CodedOutputStream.newInstance(bout);
cout.useDeterministicSerialization();
codec.encode(object, cout);
cout.flush();
return bout.toByteString();
} catch (IOException e) {
throw new IllegalStateException("exception writing to ByteString", e);
}
}
/**
* Parses a byte array to a protobuf message.
*
* @param parser parser for the proto type.
* @param in byte array with the message contents.
* @return parsed proto.
*/
public static <M extends MessageLite> M parseUnchecked(Parser<M> parser, byte[] in) {
try {
return parser.parseFrom(in);
} catch (IOException e) {
throw new IllegalArgumentException("exception parsing byte array to proto", e);
}
}
private ProtoCacheSerializers() {}
}