Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Http Connection stream only sends 2048 bytes  RSS feed

 
Alex Parvan
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Alex Parvan
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!