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 95af340
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- Added CHANGELOG and verifier workflow ([65](https://github.com/opensearch-project/opensearch-hadoop/pull/65))
### Changed
- [Spark Distribution] Default Assemble artifact to Spark 3 ([107](https://github.com/opensearch-project/opensearch-hadoop/pull/107))
- Uses DataInputStream instead of ObjectInputStream to deserialize objects ([154](https://github.com/opensearch-project/opensearch-hadoop/pull/154))
### Deprecated
### Removed
### Fixed
Expand Down
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 95af340

Please sign in to comment.