Article View: borland.public.jbuilder.java.api
Article #989Re: OutOfMemoryError
From: Owen Green
Date: Tue, 02 Jul 2002 17:18
Date: Tue, 02 Jul 2002 17:18
49 lines
3205 bytes
3205 bytes
"Alan Shiers" <ashiers@hfx.eastlink.ca> wrote in message news:3D21EC78.228A5CD3@hfx.eastlink.ca... > > > > byte[] buffer = new byte[128]; > > int n; > > while ((n = in.read(buffer)) >= 0) > > out.write(buffer, n); > > > This while loop is confusing to me. You dimension an array at 128 > elements and pass it to the read(byte[]) method of DataInputStream. Which populates the array with /up to/ 128 bytes of data on each read. > Where does the size of the file figure into all this? In the boolean condition (n = in.read(buffer)) >= 0, which does two things: n = in.read(buffer) assigns the int n to the number of bytes that have just been read (..) >= 0 tests that this is greater than 0; if EOF is detected then the stream will return -1, for example, so this condition gurantees that the loop will continue until all the data in the file has been read. At no point do we actually need to know the size of the file; it simply isn't relevant. > The thing that is throwing me is that the file maybe much bigger than > 128bytes. Hence the loop. >Is the read() method going to automatically increase the size > of the array? No, this is impossible with Java arrays. What happens is that for each iteration of the loop up to 128 bytes of data are read from the input stream. The actual number of bytes read is stored in a variable, n, so that we can ensure that the correct number are written to the output stream. The loop has an exit condition based on the fact that the general contract of input stream is for EOF to be signalled with a negative number (and the assumption that a read of 0 bytes would also indicate the absence of any more useful data). This technique offers an improvement over trying to read an entire file in at one time, as the resource overhead remains pretty much constant, i.e. the size of your buffer, and as such far more able to deal with files of any size. It also provides an improvment to reading a byte at a time by requiring far less method calls (roughly 128th of the number in this case). hth Owen
Technical Details
Message-ID:
<3d21e07b_2@dnews>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!supernews.com!195.40.4.120.MISMATCH!easynet-quince!easynet.net!oleane.net!oleane!news.maxwell.syr.edu!iad-peer.news.verio.net!news.verio.net!newsfeed.wirehub.nl!PeerFeed.KWeb.on.ca!dnewsReferences:
<3D20CC5D.B70473C9@hfx.eastlink.ca> <MPG.178acb912dba8f7998b79d@newsgroups.borland.com> <3D21C1CA.F2D953E2@hfx.eastlink.ca> <3d21bb44_1@dnews> <3D21EC78.228A5CD3@hfx.eastlink.ca>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 1089d0,28eab50e784856ac X-Google-Attributes: gid1089d0,public X-Google-ArrivalTime: 2002-07-02 10:27:46 PST From: "Owen Green" <owen@begonefoulmachine.kennulz.demon.co.uk> Newsgroups: borland.public.jbuilder.java.api References: <3D20CC5D.B70473C9@hfx.eastlink.ca> <MPG.178acb912dba8f7998b79d@newsgroups.borland.com> <3D21C1CA.F2D953E2@hfx.eastlink.ca> <3d21bb44_1@dnews> <3D21EC78.228A5CD3@hfx.eastlink.ca> Subject: Re: OutOfMemoryError Date: Tue, 2 Jul 2002 18:18:45 +0100 Lines: 53 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4522.1200 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4522.1200 NNTP-Posting-Host: 193.237.54.9 Message-ID: <3d21e07b_2@dnews> X-Trace: dnews 1025630331 193.237.54.9 (2 Jul 2002 10:18:51 -0700) Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!supernews.com!195.40.4.120.MISMATCH!easynet-quince!easynet.net!oleane.net!oleane!news.maxwell.syr.edu!iad-peer.news.verio.net!news.verio.net!newsfeed.wirehub.nl!PeerFeed.KWeb.on.ca!dnews