Re: [Py4j-users] Question about Python bytearray and Java byte[]
Status: Beta
Brought to you by:
barthe
From: Alex G. <ale...@ne...> - 2012-10-14 20:46:39
|
14.10.2012 22:25, kur...@co... kirjoitti: > Hello all, > > First off I want to say thanks to the developers of this project. We > are a python shop using py4j to work with a vendor that provides us > .class and .so library interfaces. For our Mac and Windows developers > we use the py4j package to talk to the .class driver otherwise we use > Python's ctypes to interact with their native unix .so library. > > So my question relates to bytearrays in Python. I have a > straightforward Gateway java class attached below that interacts with > our vendor's code. In red I've highlighted where my issues are. On > the python side I'm doing this: > > code = gateway.ttsRequestBuffer(self.server_ip, self.server_port, > text, self.voice, fmt, bFirst, bAll) > > buf = self.gw.ttsGetBufferData() if (code == > driver_h.TTS_RESULT_SUCCESS) else None > > > buf is a Python bytearray. The Java Gateway is returning byte[] which > in this case is the binary of a short .wav clip. Attached are two > files, each one is the hexdump of the short .wav file. The unix one > is generated by using Python's ctype library and the Java uses py4j to > talk to our vendor's library. > > > Java using py4j: > > 00000000 52 49 46 46 00 1c 02 00 57 41 56 45 66 6d 74 20 > |RIFF....WAVEfmt | > > 00000010 12 00 00 00 01 00 01 00 40 1f 00 00 80 3e 00 00 > |........@....>..| > > > Unix using ctypes: > > 00000000 52 49 46 46 da 1c 02 00 57 41 56 45 66 6d 74 20 > |RIFF....WAVEfmt | > > 00000010 12 00 00 00 01 00 01 00 40 1f 00 00 80 3e 00 00 > |........@....>..| > > > These are the first 32 bytes of the files. This occurs throughout and > doesn't seem to be random, take a look at the file differences between > the two attachments, it's very strange. And I did test > writeToTempfile() to verify it wasn't on the Java side. The problem > definitely is between returning the byte[] buffer on the Java side and > the reading of that bytearray on Python side. > > We would love to get this working so our Mac and Windows developers > can work with our vendor's library. > > Any ideas would be helpful, > Thanks! > Kurt > This problem was caused by the unicode-based serialization of bytes in the latest release and has been fixed in development. See here: https://github.com/bartdag/py4j/issues/91 > > ******************************* > > > import java.lang.String; > > import java.io.IOException; > > > import py4j.GatewayServer; > > import voiceware.libttsapi; // This is our vendors library > > > class Gateway > > { > > private libttsapi m_ttsapi; > > > /* Default c'tor create libttsapi instance used by our gateway to > > * communicate with our python app */ > > public Gateway() > > { > > m_ttsapi = new libttsapi(); > > } > > /* python client will call this and get socket error if gateway not up */ > > public boolean get_gw_status() > > { > > return true; > > } > > > /* server status port is default to 7777 */ > > public int ttsRequestStatus(String ipaddr, int status_port) > > { > > int rc; > > try > > { > > rc = m_ttsapi.ttsRequestStatus(ipaddr, status_port); > > } > > catch (IOException e) > > { > > rc = -9; > > } > > return rc; > > } > > > /* request to generate buffer from tts server */ > > public int ttsRequestBuffer(String ipaddr, int port, String text, int > speakerId, > > int bufformat, int reqfirst, int oneframe) > > { > > int rc; > > try > > { > > rc = m_ttsapi.ttsRequestBuffer(ipaddr, port, text, speakerId, > > bufformat, reqfirst, oneframe); > > } > > catch (IOException e) > > { > > rc = -9; > > } > > return rc; > > } > > > /* Typically after if ttsRequestBuffer() successfull to get buffer > contents */ > > public byte[] ttsGetBufferData() > > { > > return m_ttsapi.szVoiceData; > > } > > > /* main entry point for Gateway */ > > public static void main(String[] args) > > { > > GatewayServer gatewayServer = new GatewayServer(new Gateway()); > > gatewayServer.start(); > > } > > } > > > > > ------------------------------------------------------------------------------ > Don't let slow site performance ruin your business. Deploy New Relic APM > Deploy New Relic app performance management and know exactly > what is happening inside your Ruby, Python, PHP, Java, and .NET app > Try New Relic at no cost today and get our sweet Data Nerd shirt too! > http://p.sf.net/sfu/newrelic-dev2dev > > > _______________________________________________ > Py4j-users mailing list > Py4...@li... > https://lists.sourceforge.net/lists/listinfo/py4j-users |