Skip to content

Commit d079983

Browse files
committed
Added a "ignore_remaining_data" flag to avoid logging errors when remaining bytes are found
See #2
1 parent 38d6ceb commit d079983

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

javaobj.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -146,31 +146,35 @@ def read_to_str(data):
146146
# ------------------------------------------------------------------------------
147147

148148

149-
def load(file_object):
149+
def load(file_object, ignore_remaining_data=False):
150150
"""
151151
Deserializes Java primitive data and objects serialized using
152152
ObjectOutputStream from a file-like object.
153153
154154
:param file_object: A file-like object
155+
:param ignore_remaining_data: If True, don't log an error when unused
156+
trailing bytes are remaining
155157
:return: The deserialized object
156158
"""
157159
marshaller = JavaObjectUnmarshaller(file_object)
158160
marshaller.add_transformer(DefaultObjectTransformer())
159-
return marshaller.readObject()
161+
return marshaller.readObject(ignore_remaining_data=ignore_remaining_data)
160162

161163

162-
def loads(string):
164+
def loads(string, ignore_remaining_data=False):
163165
"""
164166
Deserializes Java objects and primitive data serialized using
165167
ObjectOutputStream from a string.
166168
167169
:param string: A Java data string
170+
:param ignore_remaining_data: If True, don't log an error when unused
171+
trailing bytes are remaining
168172
:return: The deserialized object
169173
"""
170174
file_like = BytesIO(string)
171175
marshaller = JavaObjectUnmarshaller(file_like)
172176
marshaller.add_transformer(DefaultObjectTransformer())
173-
return marshaller.readObject()
177+
return marshaller.readObject(ignore_remaining_data=ignore_remaining_data)
174178

175179

176180
def dumps(obj):
@@ -393,10 +397,12 @@ def __init__(self, stream):
393397
# Read the stream header (magic & version)
394398
self._readStreamHeader()
395399

396-
def readObject(self):
400+
def readObject(self, ignore_remaining_data=False):
397401
"""
398402
Reads an object from the input stream
399403
404+
:param ignore_remaining_data: If True, don't log an error when
405+
unused trailing bytes are remaining
400406
:return: The unmarshalled object
401407
:raise Exception: Any exception that occurred during unmarshalling
402408
"""
@@ -406,7 +412,7 @@ def readObject(self):
406412

407413
position_bak = self.object_stream.tell()
408414
the_rest = self.object_stream.read()
409-
if len(the_rest):
415+
if not ignore_remaining_data and len(the_rest):
410416
log_error("Warning!!!!: Stream still has {0} bytes left. "
411417
"Enable debug mode of logging to see the hexdump."
412418
.format(len(the_rest)))
@@ -417,7 +423,7 @@ def readObject(self):
417423
self.object_stream.seek(position_bak)
418424
return res
419425
except Exception:
420-
self._oops_dump_state()
426+
self._oops_dump_state(ignore_remaining_data)
421427
raise
422428

423429
def add_transformer(self, transformer):
@@ -898,9 +904,12 @@ def _add_reference(self, obj):
898904
"""
899905
self.references.append(obj)
900906

901-
def _oops_dump_state(self):
907+
def _oops_dump_state(self, ignore_remaining_data=False):
902908
"""
903909
Log a deserialization error
910+
911+
:param ignore_remaining_data: If True, don't log an error when
912+
unused trailing bytes are remaining
904913
"""
905914
log_error("==Oops state dump" + "=" * (30 - 17))
906915
log_error("References: {0}".format(self.references))
@@ -911,7 +920,7 @@ def _oops_dump_state(self):
911920
self.object_stream.seek(-16, os.SEEK_CUR)
912921
the_rest = self.object_stream.read()
913922

914-
if len(the_rest):
923+
if not ignore_remaining_data and len(the_rest):
915924
log_error("Warning!!!!: Stream still has {0} bytes left."
916925
.format(len(the_rest)))
917926
log_error(self._create_hexdump(the_rest))

0 commit comments

Comments
 (0)