🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.api
Article #990

Re: OutOfMemoryError

#990
From: Alan Shiers
Date: Tue, 02 Jul 2002 19:01
56 lines
3558 bytes
Thanks Owen for the explanation.  I understand better now what is going
on.

I'll work with this.

Alan

Owen Green wrote:
> 
> "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: <3D21F894.36597F8E@hfx.eastlink.ca>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-03!sn-xit-01!supernews.com!newsswitch.lcs.mit.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed.icl.net!newsfeed.fjserv.net!opentransit.net!fr.clara.net!heighliner.fr.clara.net!213.73.101.75.MISMATCH!eusc.inter.net!newsfeed01.sul.t-online.de!t-online.de!dnews
References: <3D20CC5D.B70473C9@hfx.eastlink.ca> <MPG.178acb912dba8f7998b79d@newsgroups.borland.com> <3D21C1CA.F2D953E2@hfx.eastlink.ca> <3d21bb44_1@dnews> <3D21EC78.228A5CD3@hfx.eastlink.ca> <3d21e07b_2@dnews>

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 11:16:02 PST
Message-ID: <3D21F894.36597F8E@hfx.eastlink.ca>
Date: Tue, 02 Jul 2002 15:01:40 -0400
From: Alan Shiers <ashiers@hfx.eastlink.ca>
Organization: Mustard Seed Software
X-Mailer: Mozilla 4.78 [en] (Win98; U)
X-Accept-Language: en
MIME-Version: 1.0
Newsgroups: borland.public.jbuilder.java.api
Subject: Re: OutOfMemoryError
References: <3D20CC5D.B70473C9@hfx.eastlink.ca> <MPG.178acb912dba8f7998b79d@newsgroups.borland.com> <3D21C1CA.F2D953E2@hfx.eastlink.ca> <3d21bb44_1@dnews> <3D21EC78.228A5CD3@hfx.eastlink.ca> <3d21e07b_2@dnews>
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 24.222.101.2
X-Trace: dnews 1025632737 24.222.101.2 (2 Jul 2002 10:58:57 -0700)
Lines: 56
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-03!sn-xit-01!supernews.com!newsswitch.lcs.mit.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed.icl.net!newsfeed.fjserv.net!opentransit.net!fr.clara.net!heighliner.fr.clara.net!213.73.101.75.MISMATCH!eusc.inter.net!newsfeed01.sul.t-online.de!t-online.de!dnews