• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Http Connection stream only sends 2048 bytes  RSS feed

 
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
 
Sheriff
Posts: 21502
96
Chrome Eclipse IDE Java Spring Ubuntu VI Editor 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: 21502
96
Chrome Eclipse IDE Java Spring Ubuntu VI Editor 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!