001/*
002 * Copyright 2006-2007 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.batch.item.util;
018
019import java.io.File;
020import java.io.IOException;
021
022import org.springframework.batch.item.ItemStreamException;
023import org.springframework.util.Assert;
024
025/**
026 * Utility methods for files used in batch processing.
027 * 
028 * @author Peter Zozom
029 */
030public final class FileUtils {
031
032        // forbids instantiation
033        private FileUtils() {
034        }
035
036        /**
037         * Set up output file for batch processing. This method implements common logic for handling output files when
038         * starting or restarting file I/O. When starting output file processing, creates/overwrites new file. When
039         * restarting output file processing, checks whether file is writable.
040         * 
041         * @param file file to be set up
042         * @param restarted true signals that we are restarting output file processing
043         * @param append true signals input file may already exist (but doesn't have to)
044         * @param overwriteOutputFile If set to true, output file will be overwritten (this flag is ignored when processing
045         * is restart)
046         */
047        public static void setUpOutputFile(File file, boolean restarted, boolean append, boolean overwriteOutputFile) {
048
049                Assert.notNull(file, "An output file is required");
050
051                try {
052                        if (!restarted) {
053                                if (!append) {
054                                        if (file.exists()) {
055                                                if (!overwriteOutputFile) {
056                                                        throw new ItemStreamException("File already exists: [" + file.getAbsolutePath() + "]");
057                                                }
058                                                if (!file.delete()) {
059                                                        throw new IOException("Could not delete file: " + file);
060                                                }
061                                        }
062
063                                        if (file.getParent() != null) {
064                                                new File(file.getParent()).mkdirs();
065                                        }
066                                        if (!createNewFile(file)) {
067                                                throw new ItemStreamException("Output file was not created: [" + file.getAbsolutePath() + "]");
068                                        }
069                                }
070                                else {
071                                        if (!file.exists()) {
072                                                if (file.getParent() != null) {
073                                                        new File(file.getParent()).mkdirs();
074                                                }
075                                                if (!createNewFile(file)) {
076                                                        throw new ItemStreamException("Output file was not created: [" + file.getAbsolutePath()
077                                                                        + "]");
078                                                }
079                                        }
080                                }
081                        }
082                }
083                catch (IOException ioe) {
084                        throw new ItemStreamException("Unable to create file: [" + file.getAbsolutePath() + "]", ioe);
085                }
086
087                if (!file.canWrite()) {
088                        throw new ItemStreamException("File is not writable: [" + file.getAbsolutePath() + "]");
089                }
090        }
091
092        /**
093         * Set up output file for batch processing. This method implements common logic for handling output files when
094         * starting or restarting file I/O. When starting output file processing, creates/overwrites new file. When
095         * restarting output file processing, checks whether file is writable.
096         *
097         * @param file file to be set up
098         * @param restarted true signals that we are restarting output file processing
099         * @param overwriteOutputFile If set to true, output file will be overwritten (this flag is ignored when processing
100         * is restart)
101         *
102         * @throws IllegalArgumentException when file is null
103         * @throws ItemStreamException when starting output file processing, file exists and flag "overwriteOutputFile" is
104         * set to false
105         * @throws ItemStreamException when unable to create file or file is not writable
106         *
107         * @deprecated use the version with explicit append parameter instead. Here append=false is assumed.
108         */
109        @Deprecated
110        public static void setUpOutputFile(File file, boolean restarted, boolean overwriteOutputFile) {
111                setUpOutputFile(file, restarted, false, overwriteOutputFile);
112        }
113
114        /**
115         * Create a new file if it doesn't already exist.
116         * 
117         * @param file the file to create on the filesystem
118         * @return true if file was created else false.
119         *
120         * @throws IOException is thrown if error occurs during creation and file
121         * does not exist.
122         */
123        public static boolean createNewFile(File file) throws IOException {
124
125                if (file.exists()) {
126                        return false;
127                }
128
129                try {
130                        return file.createNewFile() && file.exists();
131                }
132                catch (IOException e) {
133                        // On some file systems you can get an exception here even though the
134                        // files was successfully created
135                        if (file.exists()) {
136                                return true;
137                        }
138                        else {
139                                throw e;
140                        }
141                }
142
143        }
144
145}