com.commercehub.gradle.plugin.avro.FileUtils Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.commercehub.gradle.plugin.avro;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/**
* General file manipulation utilities.
*
* This copy from Apache Commons IO has been pruned down to just what is needed for gradle-avro-plugin.
*
*
* Facilities are provided in the following areas:
*
* - writing to a file
*
- reading from a file
*
- make a directory including parent directories
*
- copying files and directories
*
- deleting files and directories
*
- converting to and from a URL
*
- listing files and directories by filter and extension
*
- comparing file content
*
- file last changed date
*
- calculating a checksum
*
*
* Origin of code: Excalibur, Alexandria, Commons-Utils
*
* @author Kevin A. Burton
* @author Scott Sanders
* @author Daniel Rall
* @author Christoph.Reck
* @author Peter Donald
* @author Jeff Turner
* @author Matthew Hawthorne
* @author Jeremias Maerki
* @author Stephen Colebourne
* @author Ian Springer
* @author Chris Eldredge
* @author Jim Harrington
* @author Niall Pemberton
* @author Sandy McArthur
* @version $Id: FileUtils.java 507684 2007-02-14 20:38:25Z bayard $
*/
class FileUtils {
/**
* Opens a {@link FileOutputStream} for the specified file, checking and
* creating the parent directory if it does not exist.
*
* At the end of the method either the stream will be successfully opened,
* or an exception will have been thrown.
*
* The parent directory will be created if it does not exist.
* The file will be created if it does not exist.
* An exception is thrown if the file object exists but is a directory.
* An exception is thrown if the file exists but cannot be written to.
* An exception is thrown if the parent directory cannot be created.
*
* @param file the file to open for output, must not be null
* @return a new {@link FileOutputStream} for the specified file
* @throws IOException if the file object is a directory
* @throws IOException if the file cannot be written to
* @throws IOException if a parent directory needs creating but that fails
* @since Commons IO 1.3
*/
public static FileOutputStream openOutputStream(File file) throws IOException {
if (file.exists()) {
if (file.isDirectory()) {
throw new IOException("File '" + file + "' exists but is a directory");
}
if (!file.canWrite()) {
throw new IOException("File '" + file + "' cannot be written to");
}
} else {
File parent = file.getParentFile();
if (parent != null && !parent.exists()) {
if (!parent.mkdirs()) {
throw new IOException("File '" + file + "' could not be created");
}
}
}
return new FileOutputStream(file);
}
/**
* Writes a String to a file creating the file if it does not exist.
*
* NOTE: As from v1.3, the parent directories of the file will be created
* if they do not exist.
*
* @param file the file to write
* @param data the content to write to the file
* @param encoding the encoding to use, null
means platform default
* @throws IOException in case of an I/O error
* @throws java.io.UnsupportedEncodingException if the encoding is not supported by the VM
*/
public static void writeStringToFile(File file, String data, String encoding) throws IOException {
if (encoding == null) {
throw new IllegalArgumentException("Must specify encoding");
}
OutputStream out = null;
try {
out = openOutputStream(file);
if (data != null) {
out.write(data.getBytes(encoding));
}
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException ioe) {
// ignore
}
}
}
}