PathDataSource.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.javax.activation;

  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.io.OutputStream;
  21. import java.nio.file.Files;
  22. import java.nio.file.OpenOption;
  23. import java.nio.file.Path;
  24. import java.util.Objects;

  25. import javax.activation.DataSource;
  26. import javax.activation.FileTypeMap;
  27. import javax.activation.MimetypesFileTypeMap;

  28. /**
  29.  * A JavaBeans Activation Framework {@link DataSource} that encapsulates a {@link Path}. It provides data typing services via a {@link FileTypeMap} object.
  30.  *
  31.  * @see javax.activation.DataSource
  32.  * @see javax.activation.FileTypeMap
  33.  * @see javax.activation.MimetypesFileTypeMap
  34.  *
  35.  * @since 1.6.0
  36.  */
  37. public final class PathDataSource implements DataSource {

  38.     /**
  39.      * The source.
  40.      */
  41.     private final Path path;

  42.     /**
  43.      * Defaults to {@link FileTypeMap#getDefaultFileTypeMap()}.
  44.      */
  45.     private final FileTypeMap typeMap;

  46.     /**
  47.      * NIO options to open the source Path.
  48.      */
  49.     private final OpenOption[] options;

  50.     /**
  51.      * Creates a new instance from a Path.
  52.      * <p>
  53.      * The file will not actually be opened until a method is called that requires the path to be opened.
  54.      * </p>
  55.      * <p>
  56.      * The type map defaults to {@link FileTypeMap#getDefaultFileTypeMap()}.
  57.      *
  58.      * @param path the path
  59.      */
  60.     public PathDataSource(final Path path) {
  61.         this(path, FileTypeMap.getDefaultFileTypeMap());
  62.     }

  63.     /**
  64.      * Creates a new instance from a Path.
  65.      * <p>
  66.      * The file will not actually be opened until a method is called that requires the path to be opened.
  67.      * </p>
  68.      *
  69.      * @param path    the path, non-null.
  70.      * @param typeMap the type map, non-null.
  71.      * @param options options for opening file streams.
  72.      */
  73.     public PathDataSource(final Path path, final FileTypeMap typeMap, final OpenOption... options) {
  74.         this.path = Objects.requireNonNull(path, "path");
  75.         this.typeMap = Objects.requireNonNull(typeMap, "typeMap");
  76.         this.options = options;
  77.     }

  78.     /**
  79.      * Gets the MIME type of the data as a String. This method uses the currently installed FileTypeMap. If there is no FileTypeMap explicitly set, the
  80.      * FileDataSource will call the {@link FileTypeMap#getDefaultFileTypeMap} method to acquire a default FileTypeMap.
  81.      * <p>
  82.      * By default, the {@link FileTypeMap} used will be a {@link MimetypesFileTypeMap}.
  83.      * </p>
  84.      *
  85.      * @return the MIME Type
  86.      * @see FileTypeMap#getDefaultFileTypeMap
  87.      */
  88.     @Override
  89.     public String getContentType() {
  90.         return typeMap.getContentType(getName());
  91.     }

  92.     /**
  93.      * Gets an InputStream representing the the data and will throw an IOException if it can not do so. This method will return a new instance of InputStream
  94.      * with each invocation.
  95.      *
  96.      * @return an InputStream
  97.      */
  98.     @Override
  99.     public InputStream getInputStream() throws IOException {
  100.         return Files.newInputStream(path, options);
  101.     }

  102.     /**
  103.      * Gets the <em>name</em> of this object. The FileDataSource will return the file name of the object.
  104.      *
  105.      * @return the name of the object or null.
  106.      * @see javax.activation.DataSource
  107.      */
  108.     @Override
  109.     public String getName() {
  110.         return Objects.toString(path.getFileName(), null);
  111.     }

  112.     /**
  113.      * Gets an OutputStream representing the the data and will throw an IOException if it can not do so. This method will return a new instance of OutputStream
  114.      * with each invocation.
  115.      *
  116.      * @return an OutputStream
  117.      */
  118.     @Override
  119.     public OutputStream getOutputStream() throws IOException {
  120.         return Files.newOutputStream(path, options);
  121.     }

  122.     /**
  123.      * Gets the File object that corresponds to this PathDataSource.
  124.      *
  125.      * @return the File object for the file represented by this object.
  126.      */
  127.     public Path getPath() {
  128.         return path;
  129.     }

  130. }