commons-email2-jakarta/src/main/java/org/apache/commons/mail2/jakarta/activation/PathDataSource.java [40:141]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
public final class PathDataSource implements DataSource {

    /**
     * The source.
     */
    private final Path path;

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

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

    /**
     * Creates a new instance from a Path.
     * <p>
     * The file will not actually be opened until a method is called that requires the path to be opened.
     * </p>
     * <p>
     * The type map defaults to {@link FileTypeMap#getDefaultFileTypeMap()}.
     *
     * @param path the path
     */
    public PathDataSource(final Path path) {
        this(path, FileTypeMap.getDefaultFileTypeMap());
    }

    /**
     * Creates a new instance from a Path.
     * <p>
     * The file will not actually be opened until a method is called that requires the path to be opened.
     * </p>
     *
     * @param path    the path, non-null.
     * @param typeMap the type map, non-null.
     * @param options options for opening file streams.
     */
    public PathDataSource(final Path path, final FileTypeMap typeMap, final OpenOption... options) {
        this.path = Objects.requireNonNull(path, "path");
        this.typeMap = Objects.requireNonNull(typeMap, "typeMap");
        this.options = options;
    }

    /**
     * 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
     * FileDataSource will call the {@link FileTypeMap#getDefaultFileTypeMap} method to acquire a default FileTypeMap.
     * <p>
     * By default, the {@link FileTypeMap} used will be a {@link MimetypesFileTypeMap}.
     * </p>
     *
     * @return the MIME Type
     * @see FileTypeMap#getDefaultFileTypeMap
     */
    @Override
    public String getContentType() {
        return typeMap.getContentType(getName());
    }

    /**
     * Gets an InputStream representing the the data and will throw an IOException if it cannot do so. This method will return a new instance of InputStream
     * with each invocation.
     *
     * @return an InputStream
     */
    @Override
    public InputStream getInputStream() throws IOException {
        return Files.newInputStream(path, options);
    }

    /**
     * Gets the <em>name</em> of this object. The FileDataSource will return the file name of the object.
     *
     * @return the name of the object or null.
     * @see jakarta.activation.DataSource
     */
    @Override
    public String getName() {
        return Objects.toString(path.getFileName(), null);
    }

    /**
     * Gets an OutputStream representing the the data and will throw an IOException if it cannot do so. This method will return a new instance of OutputStream
     * with each invocation.
     *
     * @return an OutputStream
     */
    @Override
    public OutputStream getOutputStream() throws IOException {
        return Files.newOutputStream(path, options);
    }

    /**
     * Gets the File object that corresponds to this PathDataSource.
     *
     * @return the File object for the file represented by this object.
     */
    public Path getPath() {
        return path;
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



commons-email2-javax/src/main/java/org/apache/commons/mail2/javax/activation/PathDataSource.java [40:141]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
public final class PathDataSource implements DataSource {

    /**
     * The source.
     */
    private final Path path;

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

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

    /**
     * Creates a new instance from a Path.
     * <p>
     * The file will not actually be opened until a method is called that requires the path to be opened.
     * </p>
     * <p>
     * The type map defaults to {@link FileTypeMap#getDefaultFileTypeMap()}.
     *
     * @param path the path
     */
    public PathDataSource(final Path path) {
        this(path, FileTypeMap.getDefaultFileTypeMap());
    }

    /**
     * Creates a new instance from a Path.
     * <p>
     * The file will not actually be opened until a method is called that requires the path to be opened.
     * </p>
     *
     * @param path    the path, non-null.
     * @param typeMap the type map, non-null.
     * @param options options for opening file streams.
     */
    public PathDataSource(final Path path, final FileTypeMap typeMap, final OpenOption... options) {
        this.path = Objects.requireNonNull(path, "path");
        this.typeMap = Objects.requireNonNull(typeMap, "typeMap");
        this.options = options;
    }

    /**
     * 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
     * FileDataSource will call the {@link FileTypeMap#getDefaultFileTypeMap} method to acquire a default FileTypeMap.
     * <p>
     * By default, the {@link FileTypeMap} used will be a {@link MimetypesFileTypeMap}.
     * </p>
     *
     * @return the MIME Type
     * @see FileTypeMap#getDefaultFileTypeMap
     */
    @Override
    public String getContentType() {
        return typeMap.getContentType(getName());
    }

    /**
     * Gets an InputStream representing the the data and will throw an IOException if it cannot do so. This method will return a new instance of InputStream
     * with each invocation.
     *
     * @return an InputStream
     */
    @Override
    public InputStream getInputStream() throws IOException {
        return Files.newInputStream(path, options);
    }

    /**
     * Gets the <em>name</em> of this object. The FileDataSource will return the file name of the object.
     *
     * @return the name of the object or null.
     * @see javax.activation.DataSource
     */
    @Override
    public String getName() {
        return Objects.toString(path.getFileName(), null);
    }

    /**
     * Gets an OutputStream representing the the data and will throw an IOException if it cannot do so. This method will return a new instance of OutputStream
     * with each invocation.
     *
     * @return an OutputStream
     */
    @Override
    public OutputStream getOutputStream() throws IOException {
        return Files.newOutputStream(path, options);
    }

    /**
     * Gets the File object that corresponds to this PathDataSource.
     *
     * @return the File object for the file represented by this object.
     */
    public Path getPath() {
        return path;
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



