The reason that a stream in HttpServletRequest can only be read once

Source: Internet
Author: User
Tags http post glassfish

First, let's review the basics of the InputStream read method,

Within the Java InputStream Read method, there is a postion, which flags the current stream read to the location, each time it is read, the position is moved once, if read to the last, The Inputstream.read method returns 1, the flag has been read, and if you want to read again, you can call the Inputstream.reset method, and position will move to the location where Mark was last called, and Mark defaults to 0, so you can read it from the beginning.

Of course, the ability to reset is conditional, depending on whether the marksupported,marksupported () method returns can be Mark/reset

We'll look back at Request.getinputstream.

The value returned by Request.getinputstream is servletinputstream, viewing servletinputstream source discovery, without rewriting the Reset method, So view InputStream source found marksupported returns false, and the Reset method throws an exception directly.

Inputstream.java

 Public Boolean marksupported () {   returnfalse;}    Public synchronized void throws IOException {   thrownew ioexception ("Mark/reset not supported");}

In summary , after the Request.getinputstream read once position to the end of the file, the second time will not read the data, due to reset (), so, Request.getinputstream can only be read once.

Summarize:

The most fundamental problem is the deep understanding of the Read and reset methods of Java IO, especially the internal implementation principle of the Read method.

Attached Servletinputstream.java source code

/** The contents of this file is subject to the terms * of the Common development and Distribution License * (the "Lic  Ense "). except * In compliance with the License. * * Can obtain a copy of the license at * glassfish/bootstrap/legal/cddlv1.0.txt or *https://glassfish.dev.java.net/public/CDDLv1.0.html.* See the License for the specific language governing * permissions and limitations under the License. * When Distr Ibuting Covered Code, include this CDDL * HEADER with each file and include the License file at * glassfish/bootstrap/legal/  Cddlv1.0.txt. If Applicable, * Add the following below this CDDL HEADER, with the ' fields ' enclosed by brackets ' [] ' replaced with your * Own identifying information:portions copyright [YYYY] * [Name of copyright owner] * * Copyright 2005 Sun Microsystems, Inc. All rights reserved. * * Portions Copyright Apache software Foundation. */  PackageJavax.servlet;ImportJava.io.InputStream;Importjava.io.IOException;/*** * provides an input stream for reading binary data from a client * request, including an efficient <code>rea Dline</code> method * For reading data one line at a time. With some protocols, such * as HTTP POST and PUT, a <code>ServletInputStream</code> * object can is used to re Ad data sent from the client. * * <p>a <code>ServletInputStream</code> object is normally retrieved via * The {@linkServletrequest#getinputstream} method. * * * <p>this is an abstract class A servlet container implements. * Subclasses of this class * must implement the <code>java.io.inputstream.read () </code> method. * * * @authorVarious * *@seeServletRequest **/ Public Abstract classServletInputStreamextendsInputStream {/*** Does Nothing, because the is an abstract class. *     */    protectedServletInputStream () {}/*** * Reads the input stream, one line at a time. Starting at A * offset, reads bytes to an array, until it reads a certain number * of bytes or reaches a Newlin     e character, which it reads into the * array as well. * * <p>this Method returns-1 If it reaches the end of the input * stream before reading the maximum number     of bytes. *     *     *     * @paramb An array of bytes to which data is read * *@paramoff an integer specifying the character @ which * This method begins reading * * @paramlen An integer specifying the maximum number of * bytes to read * *@returnAn integer specifying the actual number of bytes * Read, or-1 If the end of the Strea M is reached * *@exceptionIOException If an input or output exception have occurred **/          Public intReadLine (byte[] B,intOffintLenthrowsIOException {if(Len <= 0) {        return0; }    intCount = 0, C;  while((c = Read ())! =-1) {B[off++] = (byte) C; Count++; if(c = = ' \ n ' | | count = =Len) {         Break; }    }    returnCount > 0? Count: 1; }}

The reason that a stream in HttpServletRequest can only be read once

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.