File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Sockets and Internet Protocols and the fly likes Http Connection stream only sends 2048 bytes Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Reply Bookmark "Http Connection stream only sends 2048 bytes" Watch "Http Connection stream only sends 2048 bytes" New topic
Author

Http Connection stream only sends 2048 bytes

Alex Parvan
Ranch Hand

Joined: Dec 10, 2009
Posts: 115
Hi, i'm having some trouble sending a byte array via http connection stream to servlet, here is my client code:

and the servlet code:

The servlet reads 4 chunks (2048 bytes) and then stops, and ofcourse the image cannot be read, i get an EOFException. I tried forcing the servlet to read byte[167096] (167096 is bytes.length on the client) and the image gets created but only the first ~3% of the pixels are shown, the rest is just white.

Maybe someone can tell me what i'm doing wrong ? i would appreciate it.
Thanks,
Alex


"Quoting yourself is stupid" - Me
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

I suggest you take a look at ByteArrayOutputStream. It will make the problem as easy as simple InputStream-to-OutputStream copying after which you call toByteArray() on the ByteArrayOutputStream to get the byte array.

However, in your case I see that you are first gathering all data in a byte[], then save that byte[] to disk. Why not use a direct FileOutputStream to copy the contents of the InputStream to instead of saving the intermediate result?


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Alex Parvan
Ranch Hand

Joined: Dec 10, 2009
Posts: 115
Rob Prime wrote:I suggest you take a look at ByteArrayOutputStream. It will make the problem as easy as simple InputStream-to-OutputStream copying after which you call toByteArray() on the ByteArrayOutputStream to get the byte array.

I don't really understand what do you mean by this, isn't my code working the way it is ?
Rob Prime wrote:However, in your case I see that you are first gathering all data in a byte[], then save that byte[] to disk. Why not use a direct FileOutputStream to copy the contents of the InputStream to instead of saving the intermediate result?

I'm not really interested in saving a file to disk, i just did that so i can check if the image came out ok.
Alex Parvan
Ranch Hand

Joined: Dec 10, 2009
Posts: 115
I was never good at using streams, i always skipped them, the error occured because i didn't read the stream correctly.

It's working, and the complete code is:
J2ME client code:

Servlet code:
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

That should work if you change the "> 0" on line 6 of the servlet code to ">= 0". If read returns 0 that does not mean there is no more data, only that there is no data right now. A return value of -1 is used to indicate there is no data and there won't be any data anymore.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Http Connection stream only sends 2048 bytes
 
Similar Threads
Downloading an Image from servlet
bytearray image decoding
Problem regarding Image save/retrieve
send image in post method from J2ME to Servlet
Convert image to raw binary data