Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-17-2009, 12:27 AM
Member
 
Join Date: Nov 2009
Posts: 2
Rep Power: 0
leejava is on a distinguished road
Default how to read the same req buffer twice
I have this problem in the code that I am looking at (done by someone who had been long gone).

The code reads the buffer that has the HttpServletRequest data. After the code reads the buffer here, I want to "reset" it so some other code in another component can read the same buffer again. Right now, when the buffer is read the second time by the other component, that code throws an IOException because there is no data returned (???) for the readLine (only my guess, don't have the other component's source code).

Here is the code I am looking at:
req is input defined as "HttpServletRequest req"
int theLen2 = 0;
String tempLine2;
theLen2 = req.getContentLength();

BufferedReader reqBody = new BufferedReader
(new InputStreamReader(req.getInputStream()));
String tempLine2;
tempLine2 = reqBody.readLine();

I put another BufferedReader and readLine in the code again just to see what happens when the buffer is read a second time, the reg.getContentLength() returns the correct length, but the reqBody.readLine returns nothing so tempLine2 is a null string.

I read from the Java book that a "mark" and "reset" can make the file available for a second read again. But I don't know how to mark it at the beginning of the "req".
And I could not find any other way to make the "req" available again.

Any ideas and/or suggestion will be greatly appreciated.

Lee
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-17-2009, 01:53 AM
Member
 
Join Date: Dec 2008
Posts: 26
Rep Power: 0
DC200 is on a distinguished road
Default
Try using getReader() to retrieve the request body instead.

Quote:
getReader() retrieves the body of the request as character data using a BufferedReader. The reader translates the character data according to the character encoding used on the body. Either this method or getInputStream() may be called to read the body, not both.
Java 2 Platform EE v1.3: Interface ServletRequest
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-17-2009, 08:29 PM
Member
 
Join Date: Nov 2009
Posts: 2
Rep Power: 0
leejava is on a distinguished road
Default RE: How to read a request buffer multiple times?
Thanks for the reply.
I changed the "getInputStream()" to a "getReader" and I got the request data the first time. But then I got an exception (Reader already defined) when the other component tried to ge the data from the buffer (my guess only, since I don't have the source code).

How do I make the buffer data available again and how do I make sure a second bufferedReader/readLine will succeed?

Thank much,
Lee
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need Help: Double Buffer rukawa527 AWT / Swing 1 02-09-2009 01:39 AM
Buffer QPRocky Networking 2 02-02-2009 04:54 PM
how to redirect the output buffer of pl/sql onto a jsp sriavr Database 0 03-11-2008 01:25 PM
Help with String Buffer mathias AWT / Swing 1 08-07-2007 07:52 AM
how to set the value of BUFFER SIZE oregon Advanced Java 1 08-06-2007 04:16 AM


All times are GMT +2. The time now is 02:59 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org