Can someone explain this to me?

O

Ouabaine

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!
 
O

Owen Jacobson

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)>
 
R

Real Gagnon

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top