• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

reading byte and char

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Friends,

Can you let me know if I can use character and byte based classes for the same inputstream.

My application is sitting in between a http request(through IE) and an HttpServlet and I want to read the headers using BufferedReader and body using InputStreamReader(using character encoding).
As this is quite urgent(u can see me working on Sunday ), if possible please provide a quick reply.

Thanks and Regards,
Mayank
 
k mayank
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Any suggestions. When I try it I get IO exception.
Not sure if this is allowed or not.

Regards,
Mayank
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I doubt it will work.
Technically you could try using an InputStream for the bytes, and an InputStreamReader wrapped around it for the chars. However, that reader will probably be a little ahead most of the time, therefore reading bytes it shouldn't.

Is this related to file uploads, where the file is stored in the request body? If so, you could check out Commons FileUpload.
 
k mayank
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply,

I did some code run. I found that after reding bytes directly through InputStream, I try stuffing this stream into BufferedReader and read chars, it works fine.

However after reading through BufferedReader, if I try reading bytes back from InoutStream, it hangs.
Donn know if there are synchronization issues here(though BufferedReader code looks very straight)

Thus for the below code:

inputStream = _socket.getInputStream();

byte []tempbyte = new byte[100];
int count1 = inputStream.read(tempbyte);
String s= new String(tempbyte);

inputReader = new BufferedReader(new InputStreamReader(_socket.getInputStream(), "UTF-8");
char [] tempChar = new char[100];
int count = inputReader.read(tempChar);


works till here while the below line when run after the above one hangs
count1 = inputStream.read(tempbyte);

I can confirm that reading through InputStream after having read through BufferedReader fails in any combination of above code.
Any suggestions.

Regards,
 
k mayank
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Friends,
Any suggestions on this......

How can I achive byte pass through and character passthrough on the same InputStream.

Regards,
Mayank
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can work with either one of the Stream classes, or with a Reader class, not with both at the same time.
 
Ranch Hand
Posts: 225
Eclipse IDE Debian Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this particular case, BufferedReader has read far more than 100 bytes or characters from the InputStream. Even the InputStreamReader may read ahead and consume more characters than you requested.

If you know how many bytes of UTF-8 data you want to read, you can just read a byte array and create a string with new String(byteArray, "UTF-8"). This way you won’t mix streams and readers.
 
k mayank
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually I wanted to read headers using BufferedReader and body using InputStream. This was because i can use readLine from BufferedReader and make sence out of headers(and decide weather to forward the request to Servlet) while I dont want to do anything with body.

Looks like this is becoming very difficult to achieve.

Regards,
Mayank
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I repeat:

Rob Prime wrote:Is this related to file uploads, where the file is stored in the request body? If so, you could check out Commons FileUpload.


If you're not using servlets (although you did mention them in your first post), you may not be able to use it directly, but you can at least download the source and see how it is implemented.
 
k mayank
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have checked the site. I'm currently using Orielly liabraries for multipart/form-data upload. Also I have checked the code in there.
The problem is by the time it comes to such liabraries, it already has HttpServletRequest object type and can just take out the body from them.
Currently I'm trying to figure out how tomcat might have implemented this.

Regards,
 
Carey Evans
Ranch Hand
Posts: 225
Eclipse IDE Debian Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apache HttpComponents has a SocketInputBuffer that can read lines as characters as well as bytes. This might be what you want.
 
k mayank
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Thanks for your wonderful replies.
Carey, the classes suggested by your were really helpful and for now I have build the application and delivered.

Will surely come back in case something bad happens over there.

Thanks again to all for your help.

Regards,
Mayank
 
Hey! You're stepping on my hand! Help me tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic