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.OutputStream;
21  import javax.servlet.ServletOutputStream;
22  
23  import org.springframework.util.Assert;
24  
25  /**
26   * Delegating implementation of {@link javax.servlet.ServletOutputStream}.
27   *
28   * <p>Used by {@link MockHttpServletResponse}; typically not directly
29   * used for testing application controllers.
30   *
31   * @author Juergen Hoeller
32   * @since 1.0.2
33   * @see MockHttpServletResponse
34   */
35  public class DelegatingServletOutputStream extends ServletOutputStream {
36  
37  	private final OutputStream targetStream;
38  
39  
40  	/**
41  	 * Create a DelegatingServletOutputStream for the given target stream.
42  	 * @param targetStream the target stream (never {@code null})
43  	 */
44  	public DelegatingServletOutputStream(OutputStream targetStream) {
45  		Assert.notNull(targetStream, "Target OutputStream must not be null");
46  		this.targetStream = targetStream;
47  	}
48  
49  	/**
50  	 * Return the underlying target stream (never {@code null}).
51  	 */
52  	public final OutputStream getTargetStream() {
53  		return this.targetStream;
54  	}
55  
56  
57  	@Override
58  	public void write(int b) throws IOException {
59  		this.targetStream.write(b);
60  	}
61  
62  	@Override
63  	public void flush() throws IOException {
64  		super.flush();
65  		this.targetStream.flush();
66  	}
67  
68  	@Override
69  	public void close() throws IOException {
70  		super.close();
71  		this.targetStream.close();
72  	}
73  
74  }