From: draygen@... Date: 2009-01-30T10:23:16+09:00 Subject: Re: Reading a file in chunks, to a byte array ------=_Part_134498_678861998.1233278749285 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Robert,=20 =C2=A0That did it! Thank you so much, very simple solution - I just need to= understand iteration/blocks a bit better for me to figure that out on my o= wn.=20 =C2=A0thanks,=20 Brian=20 ----- Original Message -----=20 From: "Robert Klemme" =20 To: "ruby-talk ML" =20 Sent: Thursday, January 29, 2009 4:57:37 PM GMT -05:00 US/Canada Eastern=20 Subject: Re: Reading a file in chunks, to a byte array=20 On 29.01.2009 22:23, draygen@comcast.net wrote:=20 > [Note: =C2=A0parts of this message were removed to make it a legal post.]= =20 >=20 > Hi everyone,=20 >=20 > I'm pretty new to Ruby, I'm taking courses and reading as much info as I = can, but I'm stuck on this one problem that may/may not have a simple answe= r.=20 >=20 > A quick summary of what I'm trying to accomplish:=20 >=20 > I've written a simple SOAP client that interfaces with a web app used for= file transfer... It works great, however, if a file is over a certain size= it just hangs - and I know the reason why...=20 > The webapp receives the file as a chunk, in the form of a byte array, the= great thing about Ruby is that strings are basically an arrangement of arb= itrary bytes, so I can just pass a string with the file object into the web= service and upload small files.=20 >=20 > But I'd like to split a file into "chunks" .. I know that I can do this:= =20 >=20 > ---=20 >=20 > def get_chunk(bytearray)=20 > bytearray.read(61440) #read a 60kb chunk of the byte array=20 > end=20 >=20 > localFile =3D File.open('test.dat')=20 >=20 > get_chunk(localFile)=20 >=20 > ---=20 >=20 > But I need to keep track of more information .. The web app is looking fo= r an offset integer , which basically works like this:=20 >=20 > (with a 60k chunksize)=20 >=20 > the first request will be:=20 > offset =3D 0=20 >=20 > second request:=20 > offset =3D 61440=20 >=20 > third request:=20 > offset =3D 122880=20 >=20 > Does anyone know how I could iterate over a bytearray, keeping track of t= he offset, and read 60k chunks all at the same time?=20 > in pseudo code:=20 >=20 > get_chunk(bytearray)=20 >=20 > bytearray.read(61440)=20 > offset =3D 0=20 > puts bytearray.pos=20 > =3D> 61440=20 >=20 > bytearray.read(61440)=20 > offset +=3D 61440=20 > puts offset=20 > =3D> 61440=20 > puts bytearray.pos=20 > =3D> 122880=20 > ...=20 >=20 > After I have the proper offset, and chunks of a bytearray - I need to kee= p calling the web service method with that data until the file =3D eof.=20 >=20 > I'm horrible at code blocks and iteration, if anyone has any suggestions,= it would be greatly appreciated.=20 It's not fully clear to me which one is the driving application here.=20 If it is your client application, it's easy=20 File.open "foo", "rb" do |io|=20 =C2=A0=C2=A0 pos =3D io.tell=20 =C2=A0=C2=A0 while buffer =3D io.read(CHUNK_SIZE)=20 =C2=A0=C2=A0 =C2=A0 send_to_web_app(pos, buffer)=20 =C2=A0=C2=A0 =C2=A0 pos =3D io.tell=20 =C2=A0=C2=A0 end=20 end=20 Kind regards=20 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0robert=20 --=20 remember.guy do |as, often| as.you_can - without end=20 ------=_Part_134498_678861998.1233278749285--