View Javadoc
1   /*
2    *    Copyright 2010-2026 the original author or authors.
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    *       https://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 org.mybatis.caches.ehcache;
17  
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  import java.nio.ByteBuffer;
24  
25  import org.ehcache.spi.serialization.Serializer;
26  import org.ehcache.spi.serialization.SerializerException;
27  
28  /**
29   * Ehcache 3 {@link Serializer} that uses standard Java serialization. This serializer is required when off-heap or
30   * disk-based storage tiers are used, since those tiers cannot store object references directly.
31   * <p>
32   * Note: heap-only caches do not need serialization; this class is provided for configurations that add off-heap or disk
33   * tiers.
34   * </p>
35   */
36  public class ObjectSerializer implements Serializer<Object> {
37  
38    /**
39     * Constructor required by Ehcache 3's serializer contract.
40     *
41     * @param loader
42     *          the class loader to use when deserialising objects
43     */
44    public ObjectSerializer(ClassLoader loader) {
45      // class loader is not used; standard ObjectInputStream resolves classes through the context class loader
46    }
47  
48    @Override
49    public ByteBuffer serialize(Object object) throws SerializerException {
50      try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
51          ObjectOutputStream oos = new ObjectOutputStream(baos)) {
52        oos.writeObject(object);
53        oos.flush();
54        return ByteBuffer.wrap(baos.toByteArray());
55      } catch (IOException e) {
56        throw new SerializerException("Failed to serialize object", e);
57      }
58    }
59  
60    @Override
61    public Object read(ByteBuffer binary) throws ClassNotFoundException, SerializerException {
62      byte[] bytes = new byte[binary.remaining()];
63      binary.get(bytes);
64      try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes))) {
65        return ois.readObject();
66      } catch (IOException e) {
67        throw new SerializerException("Failed to deserialize object", e);
68      }
69    }
70  
71    @Override
72    public boolean equals(Object object, ByteBuffer binary) throws ClassNotFoundException, SerializerException {
73      return object.equals(read(binary));
74    }
75  
76  }