InputStreamDataSource.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.mail2.jakarta.activation;

  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.io.OutputStream;

  21. import jakarta.activation.DataSource;

  22. /**
  23.  * A JavaBeans Activation Framework {@link DataSource} specialized for {@link InputStream}.
  24.  * <p>
  25.  * Copied from <a href="https://cxf.apache.org/">Apache CXF</a> and modified.
  26.  * </p>
  27.  *
  28.  * @since 1.6.0
  29.  */
  30. public final class InputStreamDataSource implements DataSource {

  31.     /**
  32.      * Default content type documented in {@link DataSource#getContentType()}.
  33.      */
  34.     private static final String DEFAULT_CONTENT_TYPE = "application/octet-stream";

  35.     /**
  36.      * The MIME content type.
  37.      */
  38.     private final String contentType;

  39.     /**
  40.      * The source.
  41.      */
  42.     private final InputStream inputStream;

  43.     /**
  44.      * The optional name.
  45.      */
  46.     private final String name;

  47.     /**
  48.      * Constructs a new instance.
  49.      *
  50.      * @param inputStream An input stream.
  51.      * @param contentType A content type.
  52.      */
  53.     public InputStreamDataSource(final InputStream inputStream, final String contentType) {
  54.         this(inputStream, contentType, null);
  55.     }

  56.     /**
  57.      * Constructs a new instance.
  58.      *
  59.      * @param inputStream An input stream.
  60.      * @param contentType A content type.
  61.      * @param name        A name.
  62.      */
  63.     public InputStreamDataSource(final InputStream inputStream, final String contentType, final String name) {
  64.         this.inputStream = inputStream;
  65.         this.contentType = contentType != null ? contentType : DEFAULT_CONTENT_TYPE;
  66.         this.name = name;
  67.     }

  68.     @Override
  69.     public String getContentType() {
  70.         return contentType;
  71.     }

  72.     @Override
  73.     public InputStream getInputStream() throws IOException {
  74.         return inputStream;
  75.     }

  76.     @Override
  77.     public String getName() {
  78.         return name;
  79.     }

  80.     /**
  81.      * Always throws {@link UnsupportedOperationException}.
  82.      *
  83.      * @return Always throws {@link UnsupportedOperationException}.
  84.      * @throws UnsupportedOperationException Always throws {@link UnsupportedOperationException}.
  85.      */
  86.     @Override
  87.     public OutputStream getOutputStream() {
  88.         throw new UnsupportedOperationException();
  89.     }

  90. }