    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *     http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017
    018package org.apache.commons.mail.activation;
    019
    020import java.io.IOException;
    021import java.io.InputStream;
    022import java.io.OutputStream;
    023
    024import javax.activation.DataSource;
    025
    026/**
    027 * A JavaBeans Activation Framework {@link DataSource} specialized for {@link InputStream}.
    028 * <p>
    029 * Copied from <a href="https://cxf.apache.org/">Apache CXF</a> and modified.
    030 * </p>
    031 *
    032 * @since 1.6.0
    033 */
    034public final class InputStreamDataSource implements DataSource {
    035
    036    /**
    037     * Default content type documented in {@link DataSource#getContentType()}.
    038     */
    039    private static final String DEFAULT_CONTENT_TYPE = "application/octet-stream";
    040    private final String contentType;
    041    private final InputStream inputStream;
    042    private final String name;
    043
    044    /**
    045     * Constructs a new instance.
    046     *
    047     * @param inputStream An input stream.
    048     * @param contentType A content type.
    049     */
    050    public InputStreamDataSource(final InputStream inputStream, final String contentType) {
    051        this(inputStream, contentType, null);
    052    }
    053
    054    /**
    055     * Constructs a new instance.
    056     *
    057     * @param inputStream An input stream.
    058     * @param contentType A content type.
    059     * @param name        A name.
    060     */
    061    public InputStreamDataSource(final InputStream inputStream, final String contentType, final String name) {
    062        this.inputStream = inputStream;
    063        this.contentType = contentType != null ? contentType : DEFAULT_CONTENT_TYPE;
    064        this.name = name;
    065    }
    066
    067    @Override
    068    public String getContentType() {
    069        return contentType;
    070    }
    071
    072    @Override
    073    public InputStream getInputStream() throws IOException {
    074        return inputStream;
    075    }
    076
    077    @Override
    078    public String getName() {
    079        return name;
    080    }
    081
    082    /**
    083     * Always throws {@link UnsupportedOperationException}.
    084     *
    085     * @return Always throws {@link UnsupportedOperationException}.
    086     * @throws UnsupportedOperationException Always throws {@link UnsupportedOperationException}.
    087     */
    088    @Override
    089    public OutputStream getOutputStream() {
    090        throw new UnsupportedOperationException();
    091    }
    092
    093}