Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ByteArray values over 127 (0x7f) causes OverFlow error #93

Closed
trinidadestrada opened this issue Jan 2, 2014 · 7 comments · Fixed by #142
Closed

ByteArray values over 127 (0x7f) causes OverFlow error #93

trinidadestrada opened this issue Jan 2, 2014 · 7 comments · Fixed by #142
Labels

Comments

@trinidadestrada
Copy link

When using python to pass a list as a byte array, an OverFlow error will occur if values greater than 127 (0x7f) are passed.

I came across this using my android device.

Below is a simple (not practical) example which can be used to cause this error:

from jnius import autoclass

OutputStream = autoclass('java.io.OutputStream')
FileOutputStream = autoclass('java.io.FileOutputStream')
BufferedOutputStream = autoclass('java.io.BufferedOutputStream')
outstream = BufferedOutputStream(FileOutputStream('/path/to/test.txt'))
outstream.write([ 0x6f, 0x7a, 0x7f])
outstream.write([ 0x80 ])
outstream.flush()

The app crash should occur at the line containing outstream.write([ 0x80 ]) as demonstrated by my trace log:

I/python  ( 1631):  Traceback (most recent call last):
I/python  ( 1631):    File "main.py", line 61, in <module>
I/python  ( 1631):      main()
I/python  ( 1631):    File "main.py", line 58, in main
I/python  ( 1631):      run_app()
I/python  ( 1631):    File "main.py", line 53, in run_app
I/python  ( 1631):      shell.run()
I/python  ( 1631):    File "/home/joe/projects/shell/build/python-for-android/build/python-install/lib/python2.7/site-packages/kivy/app.py", line 600, in run
I/python  ( 1631):    File "/home/joe/projects/shell/build/python-for-android/build/python-install/lib/python2.7/site-packages/kivy/base.py", line 454, in runTouchApp
I/python  ( 1631):    File "/home/joe/projects/shell/build/python-for-android/build/python-install/lib/python2.7/site-packages/kivy/core/window/window_pygame.py", line 325, in mainloop
I/python  ( 1631):    File "/home/joe/projects/shell/build/python-for-android/build/python-install/lib/python2.7/site-packages/kivy/core/window/window_pygame.py", line 231, in _mainloop
I/python  ( 1631):    File "/home/joe/projects/shell/build/python-for-android/build/python-install/lib/python2.7/site-packages/kivy/base.py", line 294, in idle
I/python  ( 1631):    File "/home/joe/projects/shell/build/python-for-android/build/python-install/lib/python2.7/site-packages/kivy/clock.py", line 370, in tick
I/python  ( 1631):    File "/home/joe/projects/shell/build/python-for-android/build/python-install/lib/python2.7/site-packages/kivy/clock.py", line 481, in _process_events
I/python  ( 1631):    File "/home/joe/projects/shell/build/python-for-android/build/python-install/lib/python2.7/site-packages/kivy/clock.py", line 280, in tick
I/python  ( 1631):    File "/mnt/sdcard/com.test.shell.app4/view/screens.py", line 86, in start_cb
I/python  ( 1631):      control.splash.load(manager)
I/python  ( 1631):    File "/mnt/sdcard/com.test.shell.app4/control/splash.py", line 3, in load
I/python  ( 1631):      screen_manager.switch_screen("home")
I/python  ( 1631):    File "/mnt/sdcard/com.test.shell.app4/view/screens.py", line 177, in switch_screen
I/python  ( 1631):      self.push(screen_type, *args, **kwargs)
I/python  ( 1631):    File "/mnt/sdcard/com.test.shell.app4/view/screens.py", line 129, in push
I/python  ( 1631):      s = screen_class(*args, **kwargs)
I/python  ( 1631):    File "/mnt/sdcard/com.test.shell.app4/view/screens.py", line 39, in __init__
I/python  ( 1631):      layout = home.build_home_screen()
I/python  ( 1631):    File "/mnt/sdcard/com.test.shell.app4/view/home.py", line 98, in build_home_screen
I/python  ( 1631):      test_out_stream()
I/python  ( 1631):    File "/mnt/sdcard/com.test.shell.app4/view/home.py", line 65, in test_out_stream
I/python  ( 1631):      outstream.write([ 0x80 ])
I/python  ( 1631):    File "jnius_export_class.pxi", line 830, in jnius.jnius.JavaMultipleMethod.__call__ (jnius/jnius.c:20536)
I/python  ( 1631):    File "jnius_export_class.pxi", line 556, in jnius.jnius.JavaMethod.__call__ (jnius/jnius.c:17394)
I/python  ( 1631):    File "jnius_conversion.pxi", line 96, in jnius.jnius.populate_args (jnius/jnius.c:5899)
I/python  ( 1631):    File "jnius_conversion.pxi", line 448, in jnius.jnius.convert_pyarray_to_java (jnius/jnius.c:8966)
I/python  ( 1631):  OverflowError: value too large to convert to signed char
I/python  ( 1631): Python for android ended.
@jiangjianxiao
Copy link

I have the same problem, can fix it?

from jnius import autoclass
ByteArrayInputStream = autoclass('java.io.ByteArrayInputStream')
v = open('xxx.xls', 'rb').read()

ms = ByteArrayInputStream(v)

raise OverflowError: value too large to convert to jbyte

@ccdavid
Copy link

ccdavid commented Mar 31, 2014

Same problem here. Any news about this?

@vilasmaciel
Copy link

Same here, any workaround?

@ccdavid
Copy link

ccdavid commented Apr 22, 2014

I was able to work around it by converting the characters to integers between -127 and 126. Like this:

[ord(b) if ord(b) <= 127 else ord(b)-256 for b in bstr]

The problem is caused because java uses sign bytes as chars.

@ccdavid
Copy link

ccdavid commented Apr 22, 2014

I meant from -128 to 127.

@limodou
Copy link
Contributor

limodou commented Dec 10, 2015

This problem seems still existed.

@limodou
Copy link
Contributor

limodou commented Dec 11, 2015

I made mistake, the bug has been fiexed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants