cancel
Showing results for 
Search instead for 
Did you mean: 

Problem reading data from url

Former Member
0 Kudos

Hi,

I am facing a problem, trying to read the data from a url. I open a connection and get an input stream of the url. When I read the input stream into a byte[], only part of the data is read, other characters become junk.

This is the code I use

URL url = new URL(uri);

HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.connect();

byte[] file = new byte[conn.getContentLength()];

int size = in.read( file, 0, conn.getContentLength() );

The size (no of bytes read) is way less than the length of the stream. Please help...

Regards,

Guru

Accepted Solutions (0)

Answers (2)

Answers (2)

detlev_beutner
Active Contributor
0 Kudos

Hi,

have the answers been of any value for you? If yes, please consider rewarding points for helpful answers! Thanks in avance!

Best regards

Detlev

detlev_beutner
Active Contributor
0 Kudos

Hi Guru,

first, mark code fragments as code, using the code button of the editor for making it more readable.

Your code is strange; what's "<i>in</i>"?

Best regards

Detlev

Former Member
0 Kudos

Hi Detlev,

Sorry for that. The "in" was the input stream.

Here is the code again

URL url = new URL(uri);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.connect();
InputStream in = conn.getInputStream();
byte[] file = new byte[conn.getContentLength()];
int size = in.read( file, 0, conn.getContentLength() );

Regards,

Guru

detlev_beutner
Active Contributor
0 Kudos

Hi Guru,

try this as a workaround:

URL url = new URL(uri);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.connect();
BufferedInputStream in = new BufferedInputStream(conn.getInputStream());
int chunkSize = 16384;
byte[] chunk = new byte[chunkSize];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while ((int size == in.read(chunk, 0, chunkSize) == chunkSize) {
  baos.write(chunk);
}
if (size != -1) {
  baos.write(chunk, 0, size);
}

From the baos you can convert into a pure byte array using toByteArray().

Hope it helps

Detlev

Former Member
0 Kudos

Detlev,

Very tricky combo in while / if, this's simplier :


URL url = new URL(uri);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.connect();
BufferedInputStream in = new BufferedInputStream(conn.getInputStream());
int chunkSize = 16384;
byte[] chunk = new byte[chunkSize];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int size = -1;
while ( (size = in.read(chunk, 0, chunkSize)) > 0 ) 
  baos.write(chunk, 0, size);

VS

Message was edited by: Valery Silaev

detlev_beutner
Active Contributor
0 Kudos

Hi Valery,

you are definitely right and I knew it could have been more nice, but have not the two seconds at the moment to think it over for providing "nice" solutions

Best regards

Detlev

PS: But the missing "{" "}" around the "while-content" seem to indicate that you also try to save time (violating Sun's Coding Conventions, which at this point I definitely appreciate quite much)

Former Member
0 Kudos

Guru,

Also the correct solution is posted below, here is a source of problem:

Many servers does not return content length (this is allowed by related HTTP RFCs), so conn.getContentLength() is equals to -1 (afterwards array with negative size creation causes an error).

VS