001/* 002 * Copyright 2002-2016 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; 021 022import javax.servlet.ServletOutputStream; 023import javax.servlet.WriteListener; 024 025import org.springframework.util.Assert; 026 027/** 028 * Delegating implementation of {@link javax.servlet.ServletOutputStream}. 029 * 030 * <p>Used by {@link MockHttpServletResponse}; typically not directly 031 * used for testing application controllers. 032 * 033 * @author Juergen Hoeller 034 * @since 1.0.2 035 * @see MockHttpServletResponse 036 */ 037public class DelegatingServletOutputStream extends ServletOutputStream { 038 039 private final OutputStream targetStream; 040 041 042 /** 043 * Create a DelegatingServletOutputStream for the given target stream. 044 * @param targetStream the target stream (never {@code null}) 045 */ 046 public DelegatingServletOutputStream(OutputStream targetStream) { 047 Assert.notNull(targetStream, "Target OutputStream must not be null"); 048 this.targetStream = targetStream; 049 } 050 051 /** 052 * Return the underlying target stream (never {@code null}). 053 */ 054 public final OutputStream getTargetStream() { 055 return this.targetStream; 056 } 057 058 059 @Override 060 public void write(int b) throws IOException { 061 this.targetStream.write(b); 062 } 063 064 @Override 065 public void flush() throws IOException { 066 super.flush(); 067 this.targetStream.flush(); 068 } 069 070 @Override 071 public void close() throws IOException { 072 super.close(); 073 this.targetStream.close(); 074 } 075 076 @Override 077 public boolean isReady() { 078 return true; 079 } 080 081 @Override 082 public void setWriteListener(WriteListener writeListener) { 083 throw new UnsupportedOperationException(); 084 } 085 086}