File.length() a InputStream.available()

Lukáš Zapletal lukas na zapletalovi.com
Středa Září 17 14:47:01 CEST 2008


To je prispevku :-)

Samozrejme ze jsem si to precetl, nez jsem psal na konferenci.
Semantiku obou metod znam, ale pochopil jsem ji tak, ze v pripade
FileInputStream neni duvod k zadnemu blokovani, takze by mela
available vracet velikost souboru. Ona to tak v praxi i dela, a proto
jsem se ptal, jestli s tim nema nekdo praktickou zkusenost. Co JRE od
Sunu vrati pri volani available nad FileInputStreamem u vetsiho
souboru nez je INT_MAX. Mohla by napriklad vratit INT_MAX a po nacteni
takoveho mnozstvi znovu - az do konce souboru... to uz spekuluji.

LZ

2008/9/16 Rado Benc <radobenc na gmail.com>:
> Nazdar,
>
> v JDK 1.6 je k available() nasledujuci Javadoc:
>
> <cite>
> Returns an estimate of the number of bytes that can be read (or
> skipped over) from this input stream without blocking by the next
> invocation of a method for this input stream. The next invocation
> might be the same thread or another thread. A single read or skip of
> this many bytes will not block, but may read or skip fewer bytes.
>
> Note that while some implementations of InputStream will return the
> total number of bytes in the stream, many will not. It is never
> correct to use the return value of this method to allocate a buffer
> intended to hold all data in this stream.
>
> A subclass' implementation of this method may choose to throw an
> IOException if this input stream has been closed by invoking the
> close() method.
>
> The available method for class InputStream always returns 0.
>
> This method should be overridden by subclasses.
>
> Returns: an estimate of the number of bytes that can be read (or
> skipped over) from this input stream without blocking or 0 when it
> reaches the end of the input stream. Throws: IOException if an I/O
> error occurs.
> </cite>
>
> Co vracia available() zavisi od implementacie streamu a nativnej
> kniznice, pokial vracia 0, v ziadnom pripade to neznamena, ze uz zo
> streamu nemozno citat (dalsie zavolanie read() moze kludne vratit
> data).
>
> Podla mna jedinym spolahlivym sposobom, ako riadit citanie zo streamu,
> je kontrolovat navratovu hodnotu read(). Kontrakt read() metody je
> daleko jednoznacnejsi, nez available():
>
> <cite>
> If no byte is available because the end of the stream has been
> reached, the value -1 is returned.
> </cite>
>
> Takze odporucam zabudnut na available() a pouzivat navratovu hodnotu z read().
>
> Maj sa,
>
> Rado
>
> 2008/9/16 Vilhelm, Robert <Robert.Vilhelm na hypotheken-management.com>
>>
>> Dobry den
>>
>>
>>
>> , asi jste prehlidl nasledujici:
>>
>>
>>
>> available():
>>
>> Returns the number of bytes that can be read (or skipped over) from this input stream WITHOUT BLOCKING by the next caller of a method for this input stream. The next caller might be the same thread or or another thread.
>>
>> hezky den
>>
>> Robert
>>
>>
>>
>>
>>
>> ________________________________
>>
>> From: konference-bounces na java.cz [mailto:konference-bounces na java.cz] On Behalf Of Lukas Barton
>> Sent: Dienstag, 16. September 2008 10:28
>> To: Java
>> Subject: Re: File.length() a InputStream.available()
>>
>>
>>
>> available() vraci neco jineho nez length(), viz javadoc:
>>
>>
>> available():
>>
>> Returns the number of bytes that can be read (or skipped over) from this input stream without blocking by the next caller of a method for this input stream. The next caller might be the same thread or or another thread.
>>
>> length():
>>
>> Returns the length of the file denoted by this abstract pathname. The return value is unspecified if this pathname denotes a directory.
>>
>>
>>   Lukas
>>
>> On Tue, Sep 16, 2008 at 10:21 AM, Luká Zapletal <lukas na zapletalovi.com> wrote:
>>
>> Zdravim konferenci,
>>
>> metoda length() tridy File vraci long, ale metoda available() tridy
>> InputStream int. Je zde jisty typovy rozpor. Chtel jsem kouknout, jak
>> je implementovana metoda available() tridy FileInputStream, ale je to
>> nativni metoda.
>>
>> Nevi nekdo, jak se Java od Sunu chova pri cteni velikeho souboru,
>> ktery presahne rozsah typu int? Co vraci metoda available tridy
>> FileInputStream?
>>
>> LZ
>>
>> --
>> Lukas Zapletal
>> http://lukas.zapletalovi.com
>>
>>
>>
>> __________________________________________________________________
>> Kreditwerk Hypotheken-Management GmbH
>> Janderstraße 7-8
>> D-68199 Mannheim
>> Tel. + 49 (621) 87557-0
>> Fax: +49 (621) 87557-111
>>
>> Geschäftsführer:
>>   Beate Stollenwerk
>>   Bernd Meier
>>   Manfred Lund
>>
>> Sitz der Gesellschaft:             Handelsregister:
>>   Mannheim                           Amtsgericht Mannheim HRB 8345
>>
>> http://www.hypotheken-management.com
>>
>> Als Inkassobüro zugelassen
>> __________________________________________________________________
>



-- 
Lukas Zapletal
http://lukas.zapletalovi.com



Další informace o konferenci Konference