LCOV - code coverage report
Current view: top level - server/cache/serialize - JavaCacheSerializer.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 11 15 73.3 %
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 com.google.gerrit.common.Nullable;
      18             : import java.io.ByteArrayInputStream;
      19             : import java.io.ByteArrayOutputStream;
      20             : import java.io.IOException;
      21             : import java.io.ObjectInputStream;
      22             : import java.io.ObjectOutputStream;
      23             : 
      24             : /**
      25             :  * Serializer that uses default Java serialization.
      26             :  *
      27             :  * <p>Unlike most {@link CacheSerializer} implementations, serializing null is supported.
      28             :  *
      29             :  * @param <T> type to serialize. Must implement {@code Serializable}, but due to implementation
      30             :  *     details this is only checked at runtime.
      31             :  */
      32         153 : public class JavaCacheSerializer<T> implements CacheSerializer<T> {
      33             :   @Override
      34             :   public byte[] serialize(@Nullable T object) {
      35           7 :     try (ByteArrayOutputStream bout = new ByteArrayOutputStream();
      36           7 :         ObjectOutputStream oout = new ObjectOutputStream(bout)) {
      37           7 :       oout.writeObject(object);
      38           7 :       oout.flush();
      39           7 :       return bout.toByteArray();
      40           0 :     } catch (IOException e) {
      41           0 :       throw new IllegalArgumentException("Failed to serialize object", e);
      42             :     }
      43             :   }
      44             : 
      45             :   @SuppressWarnings({"unchecked", "BanSerializableRead"})
      46             :   @Override
      47             :   public T deserialize(byte[] in) {
      48             :     Object object;
      49           2 :     try (ByteArrayInputStream bin = new ByteArrayInputStream(in);
      50           2 :         ObjectInputStream oin = new ObjectInputStream(bin)) {
      51           2 :       object = oin.readObject();
      52           0 :     } catch (ClassNotFoundException | IOException e) {
      53           0 :       throw new IllegalArgumentException("Failed to deserialize object", e);
      54           2 :     }
      55           2 :     return (T) object;
      56             :   }
      57             : }

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