001package org.eclipse.aether.spi.io;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 * 
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 * 
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.File;
023import java.io.IOException;
024import java.io.InputStream;
025import java.nio.ByteBuffer;
026
027/**
028 * A utility component to perform file-based operations.
029 */
030public interface FileProcessor
031{
032
033    /**
034     * Creates the directory named by the given abstract pathname, including any necessary but nonexistent parent
035     * directories. Note that if this operation fails it may have succeeded in creating some of the necessary parent
036     * directories.
037     * 
038     * @param directory The directory to create, may be {@code null}.
039     * @return {@code true} if and only if the directory was created, along with all necessary parent directories;
040     *         {@code false} otherwise
041     */
042    boolean mkdirs( File directory );
043
044    /**
045     * Writes the given data to a file. UTF-8 is assumed as encoding for the data. Creates the necessary directories for
046     * the target file. In case of an error, the created directories will be left on the file system.
047     * 
048     * @param target The file to write to, must not be {@code null}. This file will be overwritten.
049     * @param data The data to write, may be {@code null}.
050     * @throws IOException If an I/O error occurs.
051     */
052    void write( File target, String data )
053        throws IOException;
054
055    /**
056     * Writes the given stream to a file. Creates the necessary directories for the target file. In case of an error,
057     * the created directories will be left on the file system.
058     * 
059     * @param target The file to write to, must not be {@code null}. This file will be overwritten.
060     * @param source The stream to write to the file, must not be {@code null}.
061     * @throws IOException If an I/O error occurs.
062     */
063    void write( File target, InputStream source )
064        throws IOException;
065
066    /**
067     * Moves the specified source file to the given target file. If the target file already exists, it is overwritten.
068     * Creates the necessary directories for the target file. In case of an error, the created directories will be left
069     * on the file system.
070     * 
071     * @param source The file to move from, must not be {@code null}.
072     * @param target The file to move to, must not be {@code null}.
073     * @throws IOException If an I/O error occurs.
074     */
075    void move( File source, File target )
076        throws IOException;
077
078    /**
079     * Copies the specified source file to the given target file. Creates the necessary directories for the target file.
080     * In case of an error, the created directories will be left on the file system.
081     * 
082     * @param source The file to copy from, must not be {@code null}.
083     * @param target The file to copy to, must not be {@code null}.
084     * @throws IOException If an I/O error occurs.
085     */
086    void copy( File source, File target )
087        throws IOException;
088
089    /**
090     * Copies the specified source file to the given target file. Creates the necessary directories for the target file.
091     * In case of an error, the created directories will be left on the file system.
092     * 
093     * @param source The file to copy from, must not be {@code null}.
094     * @param target The file to copy to, must not be {@code null}.
095     * @param listener The listener to notify about the copy progress, may be {@code null}.
096     * @return The number of copied bytes.
097     * @throws IOException If an I/O error occurs.
098     */
099    long copy( File source, File target, ProgressListener listener )
100        throws IOException;
101
102    /**
103     * A listener object that is notified for every progress made while copying files.
104     * 
105     * @see FileProcessor#copy(File, File, ProgressListener)
106     */
107    interface ProgressListener
108    {
109
110        void progressed( ByteBuffer buffer )
111            throws IOException;
112
113    }
114
115}