FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-javaobj/javaobj/v2/main.py at 0.4.4 · tcalmant/python-javaobj · GitHub
tcalmant
/
python-javaobj
Public
Notifications
You must be signed in to change notification settings
Fork
19
Star
84
Code
Issues
2
Pull requests
0
Actions
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
python-javaobj
/
javaobj
/
v2
/
main.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
86 lines (68 loc) · 2.59 KB
Breadcrumbs
python-javaobj
/
javaobj
/
v2
/
main.py
Copy path
File metadata and controls
86 lines (68 loc) · 2.59 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python3
"""
Mimics the core API with the new deserializer
"""
from
__future__
import
absolute_import
from
typing
import
IO
,
Any
# pylint:disable=W0611
try
:
# Python 2
from
StringIO
import
StringIO
as
BytesIO
except
ImportError
:
# Python 3+
from
io
import
BytesIO
from
..
utils
import
java_data_fd
from
.
api
import
ObjectTransformer
# pylint:disable=W0611
from
.
core
import
JavaStreamParser
from
.
transformers
import
DefaultObjectTransformer
,
NumpyArrayTransformer
# ------------------------------------------------------------------------------
# Module version
__version_info__
=
(
0
,
4
,
4
)
__version__
=
"."
.
join
(
str
(
x
)
for
x
in
__version_info__
)
# Documentation strings format
__docformat__
=
"restructuredtext en"
# ------------------------------------------------------------------------------
def
load
(
file_object
,
*
transformers
,
**
kwargs
):
# type: (IO[bytes], ObjectTransformer, Any) -> Any
"""
Deserializes Java primitive data and objects serialized using
ObjectOutputStream from a file-like object.
:param file_object: A file-like object
:param transformers: Custom transformers to use
:return: The deserialized object
"""
# Check file format (uncompress if necessary)
file_object
=
java_data_fd
(
file_object
)
# Ensure we have the default object transformer
all_transformers
=
list
(
transformers
)
for
t
in
all_transformers
:
if
isinstance
(
t
,
DefaultObjectTransformer
):
break
else
:
all_transformers
.
append
(
DefaultObjectTransformer
())
if
kwargs
.
get
(
"use_numpy_arrays"
,
False
):
# Use the numpy array transformer if requested
all_transformers
.
append
(
NumpyArrayTransformer
())
# Parse the object(s)
parser
=
JavaStreamParser
(
file_object
,
all_transformers
)
contents
=
parser
.
run
()
if
len
(
contents
)
==
0
:
# Nothing was parsed, but no error
return
None
elif
len
(
contents
)
==
1
:
# Return the only object as is
return
contents
[
0
]
else
:
# Returns all objects if they are more than one
return
contents
def
loads
(
data
,
*
transformers
,
**
kwargs
):
# type: (bytes, ObjectTransformer, Any) -> Any
"""
Deserializes Java objects and primitive data serialized using
ObjectOutputStream from bytes.
:param data: A Java data string
:param transformers: Custom transformers to use
:param ignore_remaining_data: If True, don't log an error when unused
trailing bytes are remaining
:return: The deserialized object
"""
return
load
(
BytesIO
(
data
),
*
transformers
,
**
kwargs
)
Back
|
FazBrowse Home
|
New Git URL