removed dependency on java.rmi for Android compatibility
*/
package org.apache.logging.log4j.util;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InvalidObjectException;
-import java.rmi.MarshalledObject;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
import java.util.Arrays;
import java.util.ConcurrentModificationException;
import java.util.HashMap;
@@ -485,7 +488,7 @@ public class SortedArrayStringMap implements IndexedStringMap {
for (int i = 0; i < size; i++) {
s.writeObject(keys[i]);
try {
- s.writeObject(new MarshalledObject<>(values[i]));
+ s.writeObject(marshall(values[i]));
} catch (final Exception e) {
handleSerializationException(e, i, keys[i]);
s.writeObject(null);
@@ -494,6 +497,22 @@ public class SortedArrayStringMap implements IndexedStringMap {
}
}
+ private static byte[] marshall(Object obj) throws IOException {
+ ByteArrayOutputStream bout = new ByteArrayOutputStream();
+ try (ObjectOutputStream oos = new ObjectOutputStream(bout)) {
+ oos.writeObject(obj);
+ oos.flush();
+ return bout.toByteArray();
+ }
+ }
+
+ private static Object unmarshall(byte[] data) throws IOException, ClassNotFoundException {
+ ByteArrayInputStream bin = new ByteArrayInputStream(data);
+ try (ObjectInputStream ois = new ObjectInputStream(bin)) {
+ return ois.readObject();
+ }
+ }
+
/**
* Calculate the next power of 2, greater than or equal to x.
* <p>
@@ -542,8 +561,8 @@ public class SortedArrayStringMap implements IndexedStringMap {
for (int i = 0; i < mappings; i++) {
keys[i] = (String) s.readObject();
try {
- final MarshalledObject<Object> marshalledObject = (MarshalledObject<Object>) s.readObject();
- values[i] = marshalledObject == null ? null : marshalledObject.get();
+ final byte[] marshalledObject = (byte[]) s.readObject();
+ values[i] = marshalledObject == null ? null : unmarshall(marshalledObject);
} catch (final Exception | LinkageError error) {
handleSerializationException(error, i, keys[i]);
values[i] = null;
