001/*
002 * Copyright 2002-2017 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.core.io;
018
019import java.io.IOException;
020import java.io.InputStream;
021
022/**
023 * Simple interface for objects that are sources for an {@link InputStream}.
024 *
025 * <p>This is the base interface for Spring's more extensive {@link Resource} interface.
026 *
027 * <p>For single-use streams, {@link InputStreamResource} can be used for any
028 * given {@code InputStream}. Spring's {@link ByteArrayResource} or any
029 * file-based {@code Resource} implementation can be used as a concrete
030 * instance, allowing one to read the underlying content stream multiple times.
031 * This makes this interface useful as an abstract content source for mail
032 * attachments, for example.
033 *
034 * @author Juergen Hoeller
035 * @since 20.01.2004
036 * @see java.io.InputStream
037 * @see Resource
038 * @see InputStreamResource
039 * @see ByteArrayResource
040 */
041public interface InputStreamSource {
042
043        /**
044         * Return an {@link InputStream} for the content of an underlying resource.
045         * <p>It is expected that each call creates a <i>fresh</i> stream.
046         * <p>This requirement is particularly important when you consider an API such
047         * as JavaMail, which needs to be able to read the stream multiple times when
048         * creating mail attachments. For such a use case, it is <i>required</i>
049         * that each {@code getInputStream()} call returns a fresh stream.
050         * @return the input stream for the underlying resource (must not be {@code null})
051         * @throws java.io.FileNotFoundException if the underlying resource doesn't exist
052         * @throws IOException if the content stream could not be opened
053         */
054        InputStream getInputStream() throws IOException;
055
056}