From: <bc...@wo...> - 2001-03-09 08:17:19
|
[D-Man] >On Thu, Mar 08, 2001 at 05:09:42PM -0800, John Mudd wrote: >| This should be easy. Reading a binary file in Jython causes changes to >| the data? But it works in Python. > >You forgot to mention that you are using a kernel/OS that likes to >mangle your data. ;-) I strongly suspect that John is using a *nix for the Jython test. >Use _binary_ mode for writing _binary_ data. Correct. >| $ jython >| Jython 2.0 on javaVM-1.3.0.01 (JIT: null) >| Type "copyright", "credits" or "license" for more information. >| >>> tiffObj = open('john.tif', 'r').read() >| >>> open('junk.tif', 'w').write(tiffObj) > ^ >This is "text" mode and Windoze likes to replace every \n with \r\n in >such streams. This is what causes your data corruption. Fortunately >on *nix systems the kernel doesn't mangle your streams under the hood. Incorrect. It is Jython that does this mangling. The issue basicly is: should a read from a file into a unicode String read the data as binary (and always set the top 8 bit to zero) or as text by passing the data through the default encoding. Without the 'b' flag, Jython is reading and writing the data as if using a Reader/Writer class. When you use the 'b' flag it uses a InputStream/OutputStream. This overloading of the 'b' flag (the flag also controls the platform dependent newline translation) is not a good thing, but it was the best I could come up with. Normally is works as expected and I think that also goes for John's case. regards, finn |