View Javadoc
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  
18  package org.apache.commons.mail2.javax.activation;
19  
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.io.OutputStream;
23  
24  import javax.activation.DataSource;
25  
26  /**
27   * A JavaBeans Activation Framework {@link DataSource} specialized for {@link InputStream}.
28   * <p>
29   * Copied from <a href="https://cxf.apache.org/">Apache CXF</a> and modified.
30   * </p>
31   *
32   * @since 1.6.0
33   */
34  public final class InputStreamDataSource implements DataSource {
35  
36      /**
37       * Default content type documented in {@link DataSource#getContentType()}.
38       */
39      private static final String DEFAULT_CONTENT_TYPE = "application/octet-stream";
40  
41      /**
42       * The MIME content type.
43       */
44      private final String contentType;
45  
46      /**
47       * The source.
48       */
49      private final InputStream inputStream;
50  
51      /**
52       * The optional name.
53       */
54      private final String name;
55  
56      /**
57       * Constructs a new instance.
58       *
59       * @param inputStream An input stream.
60       * @param contentType A content type.
61       */
62      public InputStreamDataSource(final InputStream inputStream, final String contentType) {
63          this(inputStream, contentType, null);
64      }
65  
66      /**
67       * Constructs a new instance.
68       *
69       * @param inputStream An input stream.
70       * @param contentType A content type.
71       * @param name        A name.
72       */
73      public InputStreamDataSource(final InputStream inputStream, final String contentType, final String name) {
74          this.inputStream = inputStream;
75          this.contentType = contentType != null ? contentType : DEFAULT_CONTENT_TYPE;
76          this.name = name;
77      }
78  
79      @Override
80      public String getContentType() {
81          return contentType;
82      }
83  
84      @Override
85      public InputStream getInputStream() throws IOException {
86          return inputStream;
87      }
88  
89      @Override
90      public String getName() {
91          return name;
92      }
93  
94      /**
95       * Always throws {@link UnsupportedOperationException}.
96       *
97       * @return Always throws {@link UnsupportedOperationException}.
98       * @throws UnsupportedOperationException Always throws {@link UnsupportedOperationException}.
99       */
100     @Override
101     public OutputStream getOutputStream() {
102         throw new UnsupportedOperationException();
103     }
104 
105 }