Can someone explain this to me?

Discussion in 'Java' started by Ouabaine, Oct 23, 2007.

  1. Ouabaine

    Ouabaine Guest

    Hello,

    I have a very strange problem here. I have found the solution, but I would
    like to know what is happening.

    I load a file from a JAR file, using the following code :

    InputStream stream=this.getClass().getResourceAsStream(name);
    if (stream!=null)
    {
    int size=stream.available();
    byte data[]=new byte[size];
    stream.read(data);
    }
    stream.available() returns the correct size, the size of the file in the jar
    file (0x1610E).
    But the above code does not work! Only the first 0x3111 bytes of the stream
    are read, the rest of the array is still 0!

    But it works if I do the following code !

    int c;
    for (c=0; c<size; c++)
    {
    data[c]=(byte)stream.read();
    }

    Could it be that the stream cannot handle such a massive data output at
    once?
    Have you got any idea about this?

    Thanks!
     
    Ouabaine, Oct 23, 2007
    #1
    1. Advertising

  2. On Oct 23, 9:46 am, "Ouabaine" <> wrote:
    > Hello,
    >
    > I have a very strange problem here. I have found the solution, but I would
    > like to know what is happening.
    >
    > I load a file from a JAR file, using the following code :
    >
    > InputStream stream=this.getClass().getResourceAsStream(name);
    > if (stream!=null)
    > {
    > int size=stream.available();
    > byte data[]=new byte[size];
    > stream.read(data);
    > }
    > stream.available() returns the correct size, the size of the file in the jar
    > file (0x1610E).
    > But the above code does not work! Only the first 0x3111 bytes of the stream
    > are read, the rest of the array is still 0!
    >
    > But it works if I do the following code !
    >
    > int c;
    > for (c=0; c<size; c++)
    > {
    > data[c]=(byte)stream.read();
    > }
    >
    > Could it be that the stream cannot handle such a massive data output at
    > once?
    > Have you got any idea about this?
    >
    > Thanks!


    InputStreams are never, ever guaranteed to read the entire size of the
    buffer, regardless of where you're reading from. You *must* use the
    return value to find out how many bytes were actually read and whether
    or not you've reached the end of the stream yet.

    This is very clearly laid out in the javadoc for InputStream:
    <http://java.sun.com/javase/6/docs/api/java/io/
    InputStream.html#read(byte[])>

    You should embed the read in a loop that reads the next chunk until
    you reach end of stream. You might find this variant of read easier
    to work with for this:
    <http://java.sun.com/javase/6/docs/api/java/io/
    InputStream.html#read(byte[],%20int,%20int)>
     
    Owen Jacobson, Oct 23, 2007
    #2
    1. Advertising

  3. Ouabaine

    Real Gagnon Guest

    > InputStream stream=this.getClass().getResourceAsStream(name);
    > if (stream!=null)
    > {
    > int size=stream.available();
    > byte data[]=new byte[size];
    > stream.read(data);
    > }


    stream.read(data) returns the actual number of bytes read. You have to
    loop until the read(data) operation returns -1 (no more data).

    Try with something like :

    int bytesRead = stream.read(data,0,data.length);
    int current = bytesRead;

    do {
    bytesRead =
    stream.read(data, current, (data.length-current));
    if(bytesRead >= 0) current += bytesRead;
    } while(bytesRead > -1);

    Bye.
    --
    Real Gagnon from Quebec, Canada
    * Java, Javascript, VBScript and PowerBuilder code snippets
    * http://www.rgagnon.com/howto.html
    * http://www.rgagnon.com/bigindex.html
     
    Real Gagnon, Oct 23, 2007
    #3
  4. Ouabaine

    Roedy Green Guest

    On Tue, 23 Oct 2007 18:46:26 +0200, "Ouabaine" <>
    wrote, quoted or indirectly quoted someone who said :

    > int size=stream.available();


    That's not how you read a stream. available does not mean how many
    bytes are in the file.

    see http://mindprod.com/applet/fileio.html
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Oct 23, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ian
    Replies:
    0
    Views:
    1,865
  2. suzy
    Replies:
    3
    Views:
    460
  3. BluDog

    Can Someone Please Explain...

    BluDog, Oct 4, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    486
    BluDog
    Oct 5, 2004
  4. Karl Seguin

    Re: Can someone explain this?

    Karl Seguin, Dec 13, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    298
    John Saunders
    Dec 13, 2004
  5. Shapper
    Replies:
    3
    Views:
    421
    willow
    Jun 10, 2005
Loading...

Share This Page