001/*
002 * Copyright 2002-2012 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.mock.web;
018
019import java.io.IOException;
020import java.io.OutputStream;
021import javax.servlet.ServletOutputStream;
022
023import org.springframework.util.Assert;
024
025/**
026 * Delegating implementation of {@link javax.servlet.ServletOutputStream}.
027 *
028 * <p>Used by {@link MockHttpServletResponse}; typically not directly
029 * used for testing application controllers.
030 *
031 * @author Juergen Hoeller
032 * @since 1.0.2
033 * @see MockHttpServletResponse
034 */
035public class DelegatingServletOutputStream extends ServletOutputStream {
036
037        private final OutputStream targetStream;
038
039
040        /**
041         * Create a DelegatingServletOutputStream for the given target stream.
042         * @param targetStream the target stream (never {@code null})
043         */
044        public DelegatingServletOutputStream(OutputStream targetStream) {
045                Assert.notNull(targetStream, "Target OutputStream must not be null");
046                this.targetStream = targetStream;
047        }
048
049        /**
050         * Return the underlying target stream (never {@code null}).
051         */
052        public final OutputStream getTargetStream() {
053                return this.targetStream;
054        }
055
056
057        @Override
058        public void write(int b) throws IOException {
059                this.targetStream.write(b);
060        }
061
062        @Override
063        public void flush() throws IOException {
064                super.flush();
065                this.targetStream.flush();
066        }
067
068        @Override
069        public void close() throws IOException {
070                super.close();
071                this.targetStream.close();
072        }
073
074}