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.File;
022import java.io.IOException;
023import java.io.InputStream;
024import java.nio.ByteBuffer;
025
026/**
027 * A utility component to perform file-based operations.
028 */
029public interface FileProcessor {
030
031    /**
032     * Creates the directory named by the given abstract pathname, including any necessary but nonexistent parent
033     * directories. Note that if this operation fails it may have succeeded in creating some of the necessary parent
034     * directories.
035     *
036     * @param directory The directory to create, may be {@code null}.
037     * @return {@code true} if and only if the directory was created, along with all necessary parent directories;
038     *         {@code false} otherwise
039     */
040    boolean mkdirs(File directory);
041
042    /**
043     * Writes the given data to a file. UTF-8 is assumed as encoding for the data. Creates the necessary directories for
044     * the target file. In case of an error, the created directories will be left on the file system.
045     *
046     * @param target The file to write to, must not be {@code null}. This file will be overwritten.
047     * @param data The data to write, may be {@code null}.
048     * @throws IOException If an I/O error occurs.
049     */
050    void write(File target, String data) throws IOException;
051
052    /**
053     * Writes the given stream to a file. Creates the necessary directories for the target file. In case of an error,
054     * the created directories will be left on the file system.
055     *
056     * @param target The file to write to, must not be {@code null}. This file will be overwritten.
057     * @param source The stream to write to the file, must not be {@code null}.
058     * @throws IOException If an I/O error occurs.
059     */
060    void write(File target, InputStream source) throws IOException;
061
062    /**
063     * Moves the specified source file to the given target file. If the target file already exists, it is overwritten.
064     * Creates the necessary directories for the target file. In case of an error, the created directories will be left
065     * on the file system.
066     *
067     * @param source The file to move from, must not be {@code null}.
068     * @param target The file to move to, must not be {@code null}.
069     * @throws IOException If an I/O error occurs.
070     */
071    void move(File source, File target) throws IOException;
072
073    /**
074     * Copies the specified source file to the given target file. Creates the necessary directories for the target file.
075     * In case of an error, the created directories will be left on the file system.
076     *
077     * @param source The file to copy from, must not be {@code null}.
078     * @param target The file to copy to, must not be {@code null}.
079     * @throws IOException If an I/O error occurs.
080     */
081    void copy(File source, File target) throws IOException;
082
083    /**
084     * Copies the specified source file to the given target file. Creates the necessary directories for the target file.
085     * In case of an error, the created directories will be left on the file system.
086     *
087     * @param source The file to copy from, must not be {@code null}.
088     * @param target The file to copy to, must not be {@code null}.
089     * @param listener The listener to notify about the copy progress, may be {@code null}.
090     * @return The number of copied bytes.
091     * @throws IOException If an I/O error occurs.
092     */
093    long copy(File source, File target, ProgressListener listener) throws IOException;
094
095    /**
096     * A listener object that is notified for every progress made while copying files.
097     *
098     * @see FileProcessor#copy(File, File, ProgressListener)
099     */
100    interface ProgressListener {
101
102        void progressed(ByteBuffer buffer) throws IOException;
103    }
104
105    /**
106     * Reads checksum from specified file.
107     *
108     * @throws IOException in case of any IO error.
109     * @since 1.8.0
110     */
111    String readChecksum(File checksumFile) throws IOException;
112
113    /**
114     * Writes checksum to specified file.
115     *
116     * @throws IOException in case of any IO error.
117     * @since 1.8.0
118     */
119    void writeChecksum(File checksumFile, String checksum) throws IOException;
120}