From: Updike, C. <Cla...@jh...> - 2005-02-17 16:36:27
|
I think you needed to use zeros, not array: Jython 2.1 on java1.3.1_01 (JIT: null) Type "copyright", "credits" or "license" for more information. >>> import jarray >>> ba =3D jarray.array(64*1024,'b') Traceback (innermost last): File "<console>", line 1, in ? AttributeError: __len__ >>> ba =3D jarray.zeros(64*1024,'b') >>> -Clark > -----Original Message----- > From: jyt...@li...=20 > [mailto:jyt...@li...] On Behalf=20 > Of Jeff Emanuel > Sent: Thursday, February 17, 2005 11:27 AM > To: Lauri Lehtinen > Cc: Jython Users > Subject: Re: [Jython-users] File write and read >=20 >=20 > Sorry, I answered before reading your entire post. You=20 > already have a buffered stream in the example below. Perhaps=20 > you can read a longer array of bytes per iteration of your=20 > loop instead of a single byte. >=20 > ba =3D jarray.array(64*1024,'b') > bi.read(ba) >=20 >=20 > Jeff Emanuel wrote: > > Because it does a physical I/O operation for every byte. > > You need to wrap the file streams with buffered streams, > > for example: > >=20 > > bi =3D BufferedInputStream(FileInputStream(File(filename))) > >=20 > >=20 > > Lauri Lehtinen wrote: > >=20 > >> Setting to use the binary mode worked! > >> > >> Does any one know why java variant of this in jython performs very > >> poorly? > >> > >> Here the basic java variant: > >> --- > >> from java.net import * > >> from java.io import * > >> def copy(filename): > >> bi =3D FileInputStream(File(filename)) > >> bo =3D FileOutputStream(File(filename+".copy")) > >> b =3D [1000000] > >> while 1: > >> l =3D bi.read(b) > >> if l =3D=3D -1: > >> break > >> bo.write(b) bo.close() > >> bi.close() > >> return 1 > >> if __name__ =3D=3D "__main__": i =3D=20 > raw_input("Give file:") > >> copy(i) > >> --- > >> That berforms only about 10kB/s, here one that uses buffers and > >> performs better: > >> --- > >> from java.net import * > >> from java.io import * > >> def copy(filename): > >> bi =3D BufferedInputStream(FileInputStream(File(filename))) > >> bo =3D=20 > BufferedOutputStream(FileOutputStream(File(filename+".copy"))) > >> while 1: > >> l =3D bi.read() > >> if l =3D=3D -1: > >> break > >> bo.write(l) bo.close() > >> bi.close() > >> return 1 > >> if __name__ =3D=3D "__main__": i =3D=20 > raw_input("Give file:") > >> copy(i) > >> --- > >> This performs about 600kB/s what isn't good at all, that should be=20 > >> much better. I tried also by using static buffer size but that=20 > >> performed even poorly for some reason. Why the performance=20 > is so bad? > >> > >> Sincerely, > >> Lauri Lehtinen > >> > >> Kent Johnson wrote: > >> > >>> Lauri Lehtinen wrote: > >>> > >>>> Hi, > >>>> > >>>> I just noticed this, when I have this kind of code: > >>>> --- > >>>> def copyFile(filename): > >>>> bi =3D open(filename) > >>>> bo =3D open(filename+'.copy', 'w') > >>>> b =3D 1000000 > >>>> while 1: > >>>> l =3D bi.read(b) > >>>> if len(l) =3D=3D 0: > >>>> break > >>>> bo.write(l) > >>>> bo.close() > >>>> bi.close() > >>>> return 1 > >>>> i =3D raw_input("Give file:") > >>>> copyFile(i) > >>>> --- > >>>> and e.g. copy mp3 file with it the .copy mp3 file is=20 > somehow mixed > >>>> and not working as it should, but when I run this with=20 > plain python=20 > >>>> it works just as it should. How to fix or workaround=20 > this? Is thus=20 > >>>> known bug? > >>> > >>> > >>> > >>> > >>> Maybe you need to use binary mode? > >>> bi =3D open(filename, 'b') > >>> bo =3D open(filename+'.copy', 'wb') > >>> > >>> How do the two files differ? > >>> > >>> Kent > >>> > >>>> > >>>> I have tried to make the same using java's FileInputStream and > >>>> FileOutputStream but then the performance is then very=20 > bad (about=20 > >>>> 500kB/s). Is that also known issue? > >>>> > >>>> Sincerely, > >>>> Lauri Lehtinen > >>>> > >>>> > >>>> > >>>> ------------------------------------------------------- > >>>> SF email is sponsored by - The IT Product Guide > >>>> Read honest & candid reviews on hundreds of IT Products from real > >>>> users. > >>>> Discover which products truly live up to the hype. Start=20 > reading now. > >>>> http://ads.osdn.com/?ad_id=3D6595&alloc_id=3D14396&op=3Dclick > >>>> _______________________________________________ > >>>> Jython-users mailing list > >>>> Jyt...@li... > >>>> https://lists.sourceforge.net/lists/listinfo/jython-users > >>>> > >>> > >>> > >>> > >>> > >>> ------------------------------------------------------- > >>> SF email is sponsored by - The IT Product Guide > >>> Read honest & candid reviews on hundreds of IT Products from real=20 > >>> users. Discover which products truly live up to the hype. Start=20 > >>> reading now.=20 > http://ads.osdn.com/?ad_id=3D6595&alloc_id=3D14396&op=3Dclick > >>> _______________________________________________ > >>> Jython-users mailing list Jyt...@li... > >>> https://lists.sourceforge.net/lists/listinfo/jython-users > >>> > >>> > >>> > >> > >> > >> > >> ------------------------------------------------------- > >> SF email is sponsored by - The IT Product Guide > >> Read honest & candid reviews on hundreds of IT Products from real=20 > >> users. Discover which products truly live up to the hype. Start=20 > >> reading now.=20 > http://ads.osdn.com/?ad_id=3D6595&alloc_id=3D14396> &op=3Dclick > >>=20 > _______________________________________________ >=20 > >> Jython-users mailing list > >> Jyt...@li... > >> https://lists.sourceforge.net/lists/listinfo/jython-users > >=20 > >=20 > >=20 > > ------------------------------------------------------- > > SF email is sponsored by - The IT Product Guide > > Read honest & candid reviews on hundreds of IT Products from real=20 > > users. Discover which products truly live up to the hype. Start=20 > > reading now. = http://ads.osdn.com/?ad_id=3D6595&alloc_id=3D14396&op=3Dclick > > _______________________________________________ > > Jython-users mailing list > > Jyt...@li... > > https://lists.sourceforge.net/lists/listinfo/jython-users >=20 >=20 > ------------------------------------------------------- > SF email is sponsored by - The IT Product Guide > Read honest & candid reviews on hundreds of IT Products from=20 > real users. Discover which products truly live up to the=20 > hype. Start reading now.=20 > http://ads.osdn.com/?ad_id=3D6595&alloc_id=3D14396> &op=3Dclick >=20 > _______________________________________________ >=20 > Jython-users mailing list > Jyt...@li... > https://lists.sourceforge.net/lists/listinfo/jython-users >=20 |