• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ben-manes / caffeine / #5173

29 Dec 2025 05:27AM UTC coverage: 0.0% (-100.0%) from 100.0%
#5173

push

github

ben-manes
speed up development ci build

0 of 3838 branches covered (0.0%)

0 of 7869 relevant lines covered (0.0%)

0.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/jcache/src/main/java/com/github/benmanes/caffeine/jcache/copy/JavaSerializationCopier.java
1
/*
2
 * Copyright 2015 Ben Manes. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package com.github.benmanes.caffeine.jcache.copy;
17

18
import static java.util.Objects.requireNonNull;
19

20
import java.io.ByteArrayInputStream;
21
import java.io.ByteArrayOutputStream;
22
import java.io.IOException;
23
import java.io.InputStream;
24
import java.io.ObjectInputStream;
25
import java.io.ObjectOutputStream;
26
import java.io.ObjectStreamClass;
27
import java.io.UncheckedIOException;
28
import java.util.Map;
29
import java.util.Set;
30
import java.util.function.Function;
31

32
import javax.cache.CacheException;
33

34
import org.jspecify.annotations.NullMarked;
35

36
/**
37
 * A strategy that uses Java serialization if a fast path approach is not applicable.
38
 * <p>
39
 * Beware that native serialization is slow and is provided for completeness. In practice, it is
40
 * recommended that a higher performance alternative is used, which is provided by numerous external
41
 * libraries.
42
 *
43
 * @author ben.manes@gmail.com (Ben Manes)
44
 */
45
@NullMarked
46
public class JavaSerializationCopier extends AbstractCopier<byte[]> {
47

48
  public JavaSerializationCopier() {
49
    super();
×
50
  }
×
51

52
  public JavaSerializationCopier(Set<Class<?>> immutableClasses,
53
      Map<Class<?>, Function<Object, Object>> deepCopyStrategies) {
54
    super(immutableClasses, deepCopyStrategies);
×
55
  }
×
56

57
  @Override
58
  protected byte[] serialize(Object object) {
59
    var bytes = new ByteArrayOutputStream();
×
60
    try (var output = new ObjectOutputStream(bytes)) {
×
61
      output.writeObject(object);
×
62
    } catch (IOException e) {
×
63
      throw new UncheckedIOException("Failed to serialize " + object.getClass(), e);
×
64
    }
×
65
    return bytes.toByteArray();
×
66
  }
67

68
  @Override
69
  @SuppressWarnings("BanSerializableRead")
70
  protected Object deserialize(byte[] data, ClassLoader classLoader) {
71
    try (var bytes = new ByteArrayInputStream(data);
×
72
         var input = newInputStream(bytes, classLoader)) {
×
73
      return input.readObject();
×
74
    } catch (IOException e) {
×
75
      throw new CacheException("Failed to deserialize", e);
×
76
    } catch (ClassNotFoundException e) {
×
77
      throw new CacheException("Failed to resolve a deserialized class", e);
×
78
    }
79
  }
80

81
  // @VisibleForTesting
82
  ObjectInputStream newInputStream(
83
      InputStream in, ClassLoader classLoader) throws IOException {
84
    return new ClassLoaderAwareObjectInputStream(in, classLoader);
×
85
  }
86

87
  /** An {@linkplain ObjectInputStream} that instantiates using the supplied classloader. */
88
  protected static class ClassLoaderAwareObjectInputStream extends ObjectInputStream {
89
    private final ClassLoader classLoader;
90

91
    public ClassLoaderAwareObjectInputStream(InputStream in, ClassLoader classLoader)
92
        throws IOException {
93
      super(in);
×
94
      this.classLoader = requireNonNull(classLoader);
×
95
    }
×
96

97
    protected ClassLoader getClassLoader() {
98
      return classLoader;
×
99
    }
100

101
    @Override
102
    @SuppressWarnings("BanSerializableRead")
103
    protected Class<?> resolveClass(ObjectStreamClass desc)
104
        throws IOException, ClassNotFoundException {
105
      try {
106
        return Class.forName(desc.getName(), /* initialize= */ false, getClassLoader());
×
107
      } catch (ClassNotFoundException ignored) {
×
108
        return super.resolveClass(desc);
×
109
      }
110
    }
111
  }
112
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc