• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Http Connection stream only sends 2048 bytes

 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
The fastest and most reliable components of any system are those that are not there. Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic