<NME> JavaSerializer.java
<BEF> package com.orangefunction.tomcat.redissessions;

import org.apache.catalina.util.CustomObjectInputStream;

import javax.servlet.http.HttpSession;

import java.util.Enumeration;
import java.util.HashMap;
import java.io.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;

public class JavaSerializer implements Serializer {
  private ClassLoader loader;

  private final Log log = LogFactory.getLog(JavaSerializer.class);

  @Override
  public void setClassLoader(ClassLoader loader) {
    this.loader = loader;
  }

  public byte[] attributesHashFrom(RedisSession session) throws IOException {
    HashMap<String,Object> attributes = new HashMap<String,Object>();
    for (Enumeration<String> enumerator = session.getAttributeNames(); enumerator.hasMoreElements();) {
      String key = enumerator.nextElement();
      attributes.put(key, session.getAttribute(key));
    }

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try (ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(bos))) {
      oos.writeUnshared(attributes);
    }

    MessageDigest digester = null;
    try {
      digester = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
      log.error("Unable to get MessageDigest instance for MD5");
    }
    return digester.digest(bos.toByteArray());
  }

  @Override
  public byte[] serializeFrom(RedisSession session, SessionSerializationMetadata metadata) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try (ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(bos))) {
      oos.writeObject(metadata);
      session.writeObjectData(oos);
    }

    return bos.toByteArray();
  }

  @Override
  public void deserializeInto(byte[] data, RedisSession session, SessionSerializationMetadata metadata) throws IOException, ClassNotFoundException {
    try(
        BufferedInputStream bis = new BufferedInputStream(new ByteArrayInputStream(data));
        ObjectInputStream ois = new CustomObjectInputStream(bis, loader);
    ) {
      SessionSerializationMetadata serializedMetadata = (SessionSerializationMetadata)ois.readObject();
      metadata.copyFieldsFrom(serializedMetadata);
      session.readObjectData(ois);
    }
  }
}

<MSG> Fix possible resource leak in serialization.

<DFF> @@ -30,9 +30,15 @@ public class JavaSerializer implements Serializer {
       attributes.put(key, session.getAttribute(key));
     }
 
-    ByteArrayOutputStream bos = new ByteArrayOutputStream();
-    try (ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(bos))) {
+    byte[] serialized = null;
+
+    try (
+         ByteArrayOutputStream bos = new ByteArrayOutputStream();
+         ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(bos));
+    ) {
       oos.writeUnshared(attributes);
+
+      serialized = bos.toByteArray();
     }
 
     MessageDigest digester = null;
@@ -41,18 +47,24 @@ public class JavaSerializer implements Serializer {
     } catch (NoSuchAlgorithmException e) {
       log.error("Unable to get MessageDigest instance for MD5");
     }
-    return digester.digest(bos.toByteArray());
+    return digester.digest(serialized);
   }
 
   @Override
   public byte[] serializeFrom(RedisSession session, SessionSerializationMetadata metadata) throws IOException {
-    ByteArrayOutputStream bos = new ByteArrayOutputStream();
-    try (ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(bos))) {
+    byte[] serialized = null;
+
+    try (
+         ByteArrayOutputStream bos = new ByteArrayOutputStream();
+         ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(bos));
+    ) {
       oos.writeObject(metadata);
       session.writeObjectData(oos);
+
+      serialized = bos.toByteArray();
     }
 
-    return bos.toByteArray();
+    return serialized;
   }
 
   @Override

Fix possible resource leak in serialization.