LCOV - code coverage report
Current view: top level - server/cache/serialize - IntegerCacheSerializer.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 17 21 81.0 %
Date: 2022-11-19 15:00:39 Functions: 3 3 100.0 %

          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.server.cache.serialize;
      16             : 
      17             : import static java.util.Objects.requireNonNull;
      18             : 
      19             : import com.google.protobuf.CodedInputStream;
      20             : import com.google.protobuf.CodedOutputStream;
      21             : import com.google.protobuf.TextFormat;
      22             : import java.io.IOException;
      23             : import java.util.Arrays;
      24             : 
      25           2 : public enum IntegerCacheSerializer implements CacheSerializer<Integer> {
      26           2 :   INSTANCE;
      27             : 
      28             :   // Same as com.google.protobuf.WireFormat#MAX_VARINT_SIZE. Note that negative values take up more
      29             :   // than MAX_VARINT32_SIZE space.
      30             :   private static final int MAX_VARINT_SIZE = 10;
      31             : 
      32             :   @Override
      33             :   public byte[] serialize(Integer object) {
      34           2 :     byte[] buf = new byte[MAX_VARINT_SIZE];
      35           2 :     CodedOutputStream cout = CodedOutputStream.newInstance(buf);
      36             :     try {
      37           2 :       cout.writeInt32NoTag(requireNonNull(object));
      38           2 :       cout.flush();
      39           0 :     } catch (IOException e) {
      40           0 :       throw new IllegalStateException("Failed to serialize int", e);
      41           2 :     }
      42           2 :     int n = cout.getTotalBytesWritten();
      43           2 :     return n == buf.length ? buf : Arrays.copyOfRange(buf, 0, n);
      44             :   }
      45             : 
      46             :   @Override
      47             :   public Integer deserialize(byte[] in) {
      48           2 :     CodedInputStream cin = CodedInputStream.newInstance(requireNonNull(in));
      49             :     int ret;
      50             :     try {
      51           2 :       ret = cin.readRawVarint32();
      52           0 :     } catch (IOException e) {
      53           0 :       throw new IllegalArgumentException("Failed to deserialize int", e);
      54           2 :     }
      55           2 :     int n = cin.getTotalBytesRead();
      56           2 :     if (n != in.length) {
      57           1 :       throw new IllegalArgumentException(
      58             :           "Extra bytes in int representation: "
      59           1 :               + TextFormat.escapeBytes(Arrays.copyOfRange(in, n, in.length)));
      60             :     }
      61           2 :     return ret;
      62             :   }
      63             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750