on 11-23-2005 5:33 AM
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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
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
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
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)
User | Count |
---|---|
88 | |
23 | |
11 | |
9 | |
8 | |
5 | |
5 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.