Skip to content

Commit 5748151

Browse files
committed
Fixed loading of HashMap and TreeMap
See issue #23
1 parent aca8d82 commit 5748151

File tree

1 file changed

+22
-37
lines changed

1 file changed

+22
-37
lines changed

javaobj.py

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
1414
:authors: Volodymyr Buell, Thomas Calmant
1515
:license: Apache License 2.0
16-
:version: 0.2.4
16+
:version: 0.2.3
1717
:status: Alpha
1818
1919
..
@@ -47,16 +47,10 @@
4747
# Python 3+
4848
from io import BytesIO
4949

50-
try:
51-
import ftfy.bad_codecs
52-
javacodec = "utf-8-var"
53-
except ImportError:
54-
javacodec = "utf-8"
55-
5650
# ------------------------------------------------------------------------------
5751

5852
# Module version
59-
__version_info__ = (0, 2, 4)
53+
__version_info__ = (0, 2, 3)
6054
__version__ = ".".join(str(x) for x in __version_info__)
6155

6256
# Documentation strings format
@@ -89,7 +83,6 @@ def log_error(message, ident=0):
8983

9084
# ------------------------------------------------------------------------------
9185

92-
9386
if sys.version_info[0] >= 3:
9487
# Python 3 interpreter : bytes & str
9588
def to_bytes(data, encoding="UTF-8"):
@@ -126,9 +119,6 @@ def read_to_str(data):
126119
"""
127120
return ''.join(chr(char) for char in data)
128121

129-
unichr = chr
130-
unicode = str
131-
132122
else:
133123
# Python 2 interpreter : str & unicode
134124
def to_str(data, encoding="UTF-8"):
@@ -651,7 +641,7 @@ def _readString(self, length_fmt="H"):
651641
"""
652642
(length,) = self._readStruct(">{0}".format(length_fmt))
653643
ba = self.object_stream.read(length)
654-
return to_str(ba.decode(javacodec))
644+
return to_str(ba)
655645

656646
def do_classdesc(self, parent=None, ident=0):
657647
"""
@@ -754,11 +744,6 @@ def do_classdesc(self, parent=None, ident=0):
754744
log_debug("Super Class for {0}: {1}"
755745
.format(clazz.name, str(superclassdesc)), ident)
756746
clazz.superclass = superclassdesc
757-
# j8spencer (Google, LLC) 2018-01-16: OR in superclass flags to catch
758-
# any SC_WRITE_METHODs needed for objects.
759-
if superclassdesc and hasattr(superclassdesc, "flags"):
760-
clazz.flags |= superclassdesc.flags
761-
762747
return clazz
763748

764749
def do_blockdata(self, parent=None, ident=0):
@@ -989,11 +974,7 @@ def do_array(self, parent=None, ident=0):
989974
else:
990975
for _ in range(size):
991976
res = self._read_value(type_char, ident)
992-
_res = res
993-
# py2
994-
if str is not unicode and isinstance(res, unicode):
995-
_res = res.encode('ascii', 'replace')
996-
log_debug("Native value: {0}".format(_res), ident)
977+
log_debug("Native value: {0}".format(res), ident)
997978
array.append(res)
998979

999980
return array
@@ -1084,15 +1065,16 @@ def _read_value(self, field_type, ident, name=""):
10841065
# We don't need details for arrays and objects
10851066
field_type = field_type[0]
10861067

1087-
_res = None
10881068
if field_type == self.TYPE_BOOLEAN:
10891069
(val,) = self._readStruct(">B")
10901070
res = bool(val)
10911071
elif field_type == self.TYPE_BYTE:
10921072
(res,) = self._readStruct(">b")
10931073
elif field_type == self.TYPE_CHAR:
1094-
_res = self._readStruct(">H")[0]
1095-
res = unichr(_res)
1074+
# TYPE_CHAR is defined by the serialization specification
1075+
# but not used in the implementation, so this is
1076+
# a hypothetical code
1077+
res = bytes(self._readStruct(">bb")).decode("utf-16-be")
10961078
elif field_type == self.TYPE_SHORT:
10971079
(res,) = self._readStruct(">h")
10981080
elif field_type == self.TYPE_INTEGER:
@@ -1108,10 +1090,7 @@ def _read_value(self, field_type, ident, name=""):
11081090
else:
11091091
raise RuntimeError("Unknown typecode: {0}".format(field_type))
11101092

1111-
if _res is None:
1112-
_res = res
1113-
1114-
log_debug("* {0} {1}: {2}".format(field_type, name, _res), ident)
1093+
log_debug("* {0} {1}: {2}".format(field_type, name, res), ident)
11151094
return res
11161095

11171096
def _convert_char_to_type(self, type_char):
@@ -1527,10 +1506,7 @@ def write_array(self, obj):
15271506
else:
15281507
log_debug("Write array of type %s" % type_char)
15291508
for v in obj:
1530-
_v = v
1531-
if str is not unicode and isinstance(v, unicode):
1532-
_v = v.encode('ascii', 'replace')
1533-
log_debug("Writing: %s" % _v)
1509+
log_debug("Writing: %s" % v)
15341510
self._write_value(type_char, v)
15351511

15361512
def _write_value(self, field_type, value):
@@ -1548,8 +1524,6 @@ def _write_value(self, field_type, value):
15481524
self._writeStruct(">B", 1, (1 if value else 0,))
15491525
elif field_type == self.TYPE_BYTE:
15501526
self._writeStruct(">b", 1, (value,))
1551-
elif field_type == self.TYPE_CHAR:
1552-
self._writeStruct(">H", 1, (ord(value),))
15531527
elif field_type == self.TYPE_SHORT:
15541528
self._writeStruct(">h", 1, (value,))
15551529
elif field_type == self.TYPE_INTEGER:
@@ -1633,6 +1607,17 @@ def __init__(self, unmarshaller):
16331607
dict.__init__(self)
16341608
JavaObject.__init__(self)
16351609

1610+
def __extra_loading__(self, unmarshaller, ident=0):
1611+
# type: (JavaObjectUnmarshaller, int) -> None
1612+
"""
1613+
Loads the content of the map, written with a custom implementation
1614+
"""
1615+
# Group annotation elements 2 by 2
1616+
args = [iter(self.annotations[1:])] * 2
1617+
for key, value in zip(*args):
1618+
self[key] = value
1619+
1620+
class JavaLinkedHashMap(JavaMap):
16361621
def __extra_loading__(self, unmarshaller, ident=0):
16371622
# type: (JavaObjectUnmarshaller, int) -> None
16381623
"""
@@ -1668,7 +1653,7 @@ def __extra_loading__(self, unmarshaller, ident=0):
16681653
"java.util.ArrayList": JavaList,
16691654
"java.util.LinkedList": JavaList,
16701655
"java.util.HashMap": JavaMap,
1671-
"java.util.LinkedHashMap": JavaMap,
1656+
"java.util.LinkedHashMap": JavaLinkedHashMap,
16721657
"java.util.TreeMap": JavaMap,
16731658
}
16741659

0 commit comments

Comments
 (0)