View Javadoc
1   /*
2    * Copyright 2002-2012 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.mock.web.test;
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  import javax.servlet.ServletInputStream;
22  
23  import org.springframework.util.Assert;
24  
25  /**
26   * Delegating implementation of {@link javax.servlet.ServletInputStream}.
27   *
28   * <p>Used by {@link MockHttpServletRequest}; typically not directly
29   * used for testing application controllers.
30   *
31   * @author Juergen Hoeller
32   * @since 1.0.2
33   * @see MockHttpServletRequest
34   */
35  public class DelegatingServletInputStream extends ServletInputStream {
36  
37  	private final InputStream sourceStream;
38  
39  
40  	/**
41  	 * Create a DelegatingServletInputStream for the given source stream.
42  	 * @param sourceStream the source stream (never {@code null})
43  	 */
44  	public DelegatingServletInputStream(InputStream sourceStream) {
45  		Assert.notNull(sourceStream, "Source InputStream must not be null");
46  		this.sourceStream = sourceStream;
47  	}
48  
49  	/**
50  	 * Return the underlying source stream (never {@code null}).
51  	 */
52  	public final InputStream getSourceStream() {
53  		return this.sourceStream;
54  	}
55  
56  
57  	@Override
58  	public int read() throws IOException {
59  		return this.sourceStream.read();
60  	}
61  
62  	@Override
63  	public void close() throws IOException {
64  		super.close();
65  		this.sourceStream.close();
66  	}
67  
68  }