Results 1 to 3 of 3
  1. #1
    leejava is offline Member
    Join Date
    Nov 2009
    Posts
    2
    Rep Power
    0

    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

  2. #2
    DC200 is offline Member
    Join Date
    Dec 2008
    Posts
    49
    Rep Power
    0

    Default

    Try using getReader() to retrieve the request body instead.

    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

  3. #3
    leejava is offline Member
    Join Date
    Nov 2009
    Posts
    2
    Rep Power
    0

    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

Similar Threads

  1. Need Help: Double Buffer
    By rukawa527 in forum AWT / Swing
    Replies: 1
    Last Post: 02-09-2009, 12:39 AM
  2. Buffer
    By QPRocky in forum Networking
    Replies: 2
    Last Post: 02-02-2009, 03:54 PM
  3. Replies: 0
    Last Post: 03-11-2008, 12:25 PM
  4. Help with String Buffer
    By mathias in forum AWT / Swing
    Replies: 1
    Last Post: 08-07-2007, 06:52 AM
  5. how to set the value of BUFFER SIZE
    By oregon in forum Advanced Java
    Replies: 1
    Last Post: 08-06-2007, 03:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •