001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.eclipse.aether.spi.io;
020
021import java.io.IOException;
022import java.io.InputStream;
023import java.io.UncheckedIOException;
024import java.nio.ByteBuffer;
025import java.nio.file.Files;
026import java.nio.file.NoSuchFileException;
027import java.nio.file.Path;
028
029/**
030 * A utility component to perform file-based operations.
031 *
032 * @since 2.0.0
033 */
034public interface PathProcessor {
035    /**
036     * Returns last modified of path in milliseconds, if exists.
037     *
038     * @param path The path, may be {@code null}.
039     * @throws UncheckedIOException If an I/O error occurs.
040     */
041    default long lastModified(Path path, long defValue) {
042        try {
043            return Files.getLastModifiedTime(path).toMillis();
044        } catch (NoSuchFileException e) {
045            return defValue;
046        } catch (IOException e) {
047            throw new UncheckedIOException(e);
048        }
049    }
050
051    /**
052     * Sets last modified of path in milliseconds, if exists.
053     *
054     * @param path The path, may be {@code null}.
055     * @throws IOException If an I/O error occurs. Some exceptions/reasons of failure to set mtime may be swallowed,
056     * and can be multiple, ranging from "file not found" to cases when FS does not support the setting the mtime.
057     * @since 2.0.0
058     */
059    void setLastModified(Path path, long value) throws IOException;
060
061    /**
062     * Returns size of file, if exists.
063     *
064     * @param path The path, may be {@code null}.
065     * @throws UncheckedIOException If an I/O error occurs.
066     */
067    default long size(Path path, long defValue) {
068        try {
069            return Files.size(path);
070        } catch (NoSuchFileException e) {
071            return defValue;
072        } catch (IOException e) {
073            throw new UncheckedIOException(e);
074        }
075    }
076
077    /**
078     * Writes the given data to a file. UTF-8 is assumed as encoding for the data. Creates the necessary directories for
079     * the target file. In case of an error, the created directories will be left on the file system.
080     *
081     * @param target The file to write to, must not be {@code null}. This file will be overwritten.
082     * @param data The data to write, may be {@code null}.
083     * @throws IOException If an I/O error occurs.
084     */
085    void write(Path target, String data) throws IOException;
086
087    /**
088     * Writes the given stream to a file. Creates the necessary directories for the target file. In case of an error,
089     * the created directories will be left on the file system.
090     *
091     * @param target The file to write to, must not be {@code null}. This file will be overwritten.
092     * @param source The stream to write to the file, must not be {@code null}.
093     * @throws IOException If an I/O error occurs.
094     */
095    void write(Path target, InputStream source) throws IOException;
096
097    /**
098     * Moves the specified source file to the given target file. If the target file already exists, it is overwritten.
099     * Creates the necessary directories for the target file. In case of an error, the created directories will be left
100     * on the file system.
101     *
102     * @param source The file to move from, must not be {@code null}.
103     * @param target The file to move to, must not be {@code null}.
104     * @throws IOException If an I/O error occurs.
105     */
106    void move(Path source, Path target) throws IOException;
107
108    /**
109     * Copies the specified source file to the given target file. Creates the necessary directories for the target file.
110     * In case of an error, the created directories will be left on the file system.
111     *
112     * @param source The file to copy from, must not be {@code null}.
113     * @param target The file to copy to, must not be {@code null}.
114     * @throws IOException If an I/O error occurs.
115     */
116    default void copy(Path source, Path target) throws IOException {
117        copy(source, target, null);
118    }
119
120    /**
121     * Same as {@link #copy(Path, Path)} but sets source file last modified timestamp on target as well.
122     *
123     * @param source The file to copy from, must not be {@code null}.
124     * @param target The file to copy to, must not be {@code null}.
125     * @throws IOException If an I/O error occurs.
126     * @see #setLastModified(Path, long)
127     * @since 2.0.0
128     */
129    default void copyWithTimestamp(Path source, Path target) throws IOException {
130        copy(source, target, null);
131        setLastModified(target, Files.getLastModifiedTime(source).toMillis());
132    }
133
134    /**
135     * Copies the specified source file to the given target file. Creates the necessary directories for the target file.
136     * In case of an error, the created directories will be left on the file system.
137     *
138     * @param source The file to copy from, must not be {@code null}.
139     * @param target The file to copy to, must not be {@code null}.
140     * @param listener The listener to notify about the copy progress, may be {@code null}.
141     * @return The number of copied bytes.
142     * @throws IOException If an I/O error occurs.
143     */
144    long copy(Path source, Path target, ProgressListener listener) throws IOException;
145
146    /**
147     * A listener object that is notified for every progress made while copying files.
148     *
149     * @see PathProcessor#copy(Path, Path, ProgressListener)
150     */
151    interface ProgressListener {
152
153        void progressed(ByteBuffer buffer) throws IOException;
154    }
155}