aspose file tools*
The moose likes I/O and Streams and the fly likes reading byte and char Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "reading byte and char" Watch "reading byte and char" New topic
Author

reading byte and char

k mayank
Greenhorn

Joined: Feb 09, 2009
Posts: 27
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


mayank
k mayank
Greenhorn

Joined: Feb 09, 2009
Posts: 27
Hi All,

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

Regards,
Mayank
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
k mayank
Greenhorn

Joined: Feb 09, 2009
Posts: 27
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

Joined: Feb 09, 2009
Posts: 27
Hello Friends,
Any suggestions on this......

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

Regards,
Mayank
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42648
    
  65
You can work with either one of the Stream classes, or with a Reader class, not with both at the same time.


Ping & DNS - my free Android networking tools app
Carey Evans
Ranch Hand

Joined: May 27, 2008
Posts: 225

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

Joined: Feb 09, 2009
Posts: 27
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

Joined: Oct 27, 2005
Posts: 19762
    
  20

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

Joined: Feb 09, 2009
Posts: 27
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

Joined: May 27, 2008
Posts: 225

Apache HttpComponents has a SocketInputBuffer that can read lines as characters as well as bytes. This might be what you want.
k mayank
Greenhorn

Joined: Feb 09, 2009
Posts: 27
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
 
 
subject: reading byte and char