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.InputStream; 021import javax.servlet.ServletInputStream; 022 023import org.springframework.util.Assert; 024 025/** 026 * Delegating implementation of {@link javax.servlet.ServletInputStream}. 027 * 028 * <p>Used by {@link MockHttpServletRequest}; typically not directly 029 * used for testing application controllers. 030 * 031 * @author Juergen Hoeller 032 * @since 1.0.2 033 * @see MockHttpServletRequest 034 */ 035public class DelegatingServletInputStream extends ServletInputStream { 036 037 private final InputStream sourceStream; 038 039 040 /** 041 * Create a DelegatingServletInputStream for the given source stream. 042 * @param sourceStream the source stream (never {@code null}) 043 */ 044 public DelegatingServletInputStream(InputStream sourceStream) { 045 Assert.notNull(sourceStream, "Source InputStream must not be null"); 046 this.sourceStream = sourceStream; 047 } 048 049 /** 050 * Return the underlying source stream (never {@code null}). 051 */ 052 public final InputStream getSourceStream() { 053 return this.sourceStream; 054 } 055 056 057 @Override 058 public int read() throws IOException { 059 return this.sourceStream.read(); 060 } 061 062 @Override 063 public void close() throws IOException { 064 super.close(); 065 this.sourceStream.close(); 066 } 067 068}