Skip to content

Commit

Permalink
Uses DataInputStream instead of ObjectInputStream to deserialize objects
Browse files Browse the repository at this point in the history
Signed-off-by: Harsha Vamsi Kalluri <harshavamsi096@gmail.com>
  • Loading branch information
harshavamsi committed Mar 20, 2023
1 parent b9b10bf commit a37f401
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions mr/src/main/java/org/opensearch/hadoop/util/IOUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import java.io.ByteArrayInputStream;
import java.io.Closeable;
import java.io.DataInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -43,6 +44,7 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Base64;
import java.util.Properties;

import javax.xml.bind.DatatypeConverter;
Expand Down Expand Up @@ -81,23 +83,22 @@ public static String serializeToBase64(Serializable object) {
}

@SuppressWarnings("unchecked")
public static <T extends Serializable> T deserializeFromBase64(String data) {
public static <T extends Serializable> T deserializeFromBase64(String data) throws ClassNotFoundException {
if (!StringUtils.hasLength(data)) {
return null;
}

byte[] rawData = DatatypeConverter.parseBase64Binary(data);
ObjectInputStream ois = null;
final byte[] dataBytes = Base64.getDecoder().decode(data);
final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(dataBytes);

try {
ois = new ObjectInputStream(new FastByteArrayInputStream(rawData));
Object o = ois.readObject();
return (T) o;
} catch (ClassNotFoundException ex) {
throw new OpenSearchHadoopIllegalStateException("cannot deserialize object", ex);
final DataInputStream dataInputStream = new DataInputStream(byteArrayInputStream);
Object obj = (Object) dataInputStream.readUTF();
return (T) obj;
} catch (IOException ex) {
throw new OpenSearchHadoopSerializationException("cannot deserialize object", ex);
} finally {
close(ois);
close(byteArrayInputStream);
}
}

Expand Down

0 comments on commit a37f401

Please sign in to comment.