FileUtils.java revision 8f8adeb1540d89588dba4c26d00a6ed6887cf6de
/**
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2006 Sun Microsystems Inc. All Rights Reserved
*
* The contents of this file are subject to the terms
* of the Common Development and Distribution License
* (the License). You may not use this file except in
* compliance with the License.
*
* You can obtain a copy of the License at
* See the License for the specific language governing
* permission and limitations under the License.
*
* When distributing Covered Code, include this CDDL
* Header Notice in each file and include the License file
* at opensso/legal/CDDLv1.0.txt.
* If applicable, add the following below the CDDL Header,
* with the fields enclosed by brackets [] replaced by
* your own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
*
* $Id: FileUtils.java,v 1.3 2010/02/09 21:34:01 hari44 Exp $
*
*/
/*
* Portions Copyrighted 2010-2014 ForgeRock AS.
* Portions Copyrighted 2014 Nomura Research Institute, Ltd.
*/
public class FileUtils {
/**
* Copies a file
*
* @param source
* file
* @param destination
* file
* @return true if copy operation succeed
*/
throws Exception {
boolean result = false;
try {
byte[] buf = new byte[4096];
int bytesRead;
}
result = true;
} catch (Exception e) { // Log & throw exception
throw e;
} finally {
try {
} catch (IOException e) {
+ "closing input stream for: " + source);
}
}
try {
} catch (IOException e) {
+ "closing output stream for: " + destination);
}
}
}
return result;
}
/**
* Method copyJarFile to destination directory
*
* @param src
* @param dest
*
*/
try {
// Allocate a buffer for reading entry data.
byte[] buffer = new byte[4096];
// Write the entry to the output JAR
int bytesRead;
}
}
} catch (Exception e) { // Log & throw exception
throw e;
} finally {
// Flush and close all the streams
try {
} catch (IOException e) {
+ "closing input stream for: " + src);
}
}
try {
} catch (IOException e) {
+ "closing output stream for: " + dest);
}
}
}
}
/**
* Method copyDirContents
*
* @param srcDir
* @param destDir
*
*/
throws Exception {
for (int i = 0; i < count; i++) {
if (entry.isDirectory()) {
+ entryName;
}
}
}
}
/**
* Method removeDir. All the files in the directory will be removed
*
* @param directory
* to be removed. @ return true if the directory is deleted. False other
* wise
*/
// Delete the contents of the directory!
for (int i = 0; i < count; i++) {
if (file.isDirectory()) {
+ file.getAbsolutePath());
}
}
}
// Now delete the directory!
if (!status) {
+ dir.getAbsolutePath());
}
return status;
}
/**
* Method removeJarFiles. jar files will be removed from source directory
*
* @param srcDir - Source directory
* @param filename - file to be deleted
*
*/
}
else
}
/**
* Method removeFiles. All the files existing in source directory
* will be removed from Destination Directory
*
* @param srcDir - Source directory to compare files
* @param desDir - Destination directory to remove files
*
*/
for (int i = 0; i < count; i++) {
}else {
}
}
}
}
}
}
}
}
throws Exception {
try {
} catch (Exception e) {
throw e;
} finally {
try {
} catch (IOException io) {
// Ignore
}
}
}
}
throws Exception {
// Obtain the current time
// Create a backup file
}
/*
* Is file valid @param filename
*
* @return boolean
*/
boolean result = false;
result = true;
}
}
return result;
}
/**
* Method replaceBackWithForward
*
*
* @param str
*
* @return
*
*/
char backSlash = '\\';
char forwardSlash = '/';
}
return returnStr;
}
/*
* Is directory valid @param directory
*
* @return boolean
*/
boolean result = false;
result = true;
}
}
return result;
}
/**
* Method getLineByNumber
*
*
* @param filePath
* @param lineNum
*
* @return
*
*/
int curLine = -1;
try {
}
}
}
return retval;
}
/**
* Method matchPattern
*
*
* @param line
* @param pattern
* @param matchBegin
* @param matchEnd
* @param ignoreCase
*
* @return
*
*/
boolean matchB = false;
boolean matchE = false;
boolean matchP = false;
boolean match = false;
if (matchBegin) {
} else if (matchEnd) {
patLen);
} else {
if (ignoreCase) {
matchP = true;
}
} else {
matchP = true;
}
}
}
}
match = true;
}
return match;
}
/**
* Method getLineWithPattern
*
*
* @param filePath
* @param pattern
* @param matchBegin
* @param matchEnd
* @param ignoreCase
* @param beginAtLine
*
* @return
*
*/
int beginAtLine) {
boolean match = false;
try {
if (beginAtLine > 0) {
int lineCount = 0;
lineCount++;
}
}
}
if (match) {
}
}
}
}
return retLine;
}
/**
* Method getFirstOccurence
*
*
* @param filePath
* @param pattern
* @param matchBegin
* @param matchEnd
* @param ignoreCase
* @param beginAtLine
*
* @return
*
*/
int beginAtLine) {
int retVal = -1;
boolean match = false;
try {
if (beginAtLine > 0) {
int lineCount = 0;
lineCount++;
}
}
}
if (match) {
} else {
retVal = -1;
}
}
}
retVal = -1;
}
return retVal;
}
/**
* Method getFirstOccurence
*
*
* @param filePath
* @param pattern
* @param matchBegin
* @param matchEnd
* @param ignoreCase
*
* @return
*
*/
ignoreCase, 0);
}
/**
* Method getLastOccurence
*
*
* @param filePath
* @param pattern
* @param matchBegin
* @param matchEnd
* @param ignoreCase
*
* @return
*
*/
int retVal = -1;
boolean match = false;
boolean notFound = true;
try {
if (match) {
notFound = false;
}
}
}
if (notFound) {
retVal = -1;
}
retVal = -1;
}
return retVal;
}
/**
* Method addMapProperty
*
* Adds a map property like <b>key[name]=value</b> in PRODUCT
*
* @param key
* key name of the property
* @param name
* name of the property
* @param value
* value of the property
*
*/
boolean result = true;
boolean isExisting = false;
int index = 0;
try {
// find the index of property insertion
if (index > 0) {
// remove the empty map and add the entry
}
} else {
}
if (result) {
while (propNames.hasMoreElements()) {
{
isExisting = true;
break;
}
}
result = isExisting;
} else {
result = false;
}
}
result = false;
}
return result;
}
/**
* Adds an entry to a list property in Product Config properties
*
*
* @param fileName
* name of the config file
* @param list
* property name of the property for the list
* @param value
* name of the list property to be added
*
* Ex: Adds a property like <b>property[index] = value</b> in Product
* Config properties
*
* @return boolean true or false
*
*/
boolean status = false;
try {
// Find location to add the list property
if (locationToAdd <= 0) {
}
// Create the new map if the value is non empty string
// Add the list property
int index = 0;
index++;
}
}
}
}
}
return status;
}
/**
* Method declaration
*
*
* @param fileName
* @param property
* @param value
*
* @return
*
* @see
*/
try {
new FileReader(file));
while (presentLine != null) {
if (presentLineNo > 0) {
}
}
}
}
}
}
// Now remove these lines and store them
int count = 0;
if (lineNum > 0) {
// shorter
}
lineNum, 1);
count++;
// empty vals
}
}
}
}
}
}
// Finally add the new list element value
return returnList;
}
/**
* Method declaration
*
*
* @param line
*
* @return
*
* @see
*/
try {
}
}
return retVal;
}
/**
* Method declaration
*
*
* @param fileName
*
* @return
*
* @see
*/
int lineNum = 0;
try {
new FileReader(file));
while (presentLine != null) {
}
}
}
}
return lineNum;
}
/**
* Method insertLineByNumber
*
*
* @param filePath
* @param lineNum
* @param line
*
* @return
*
*/
boolean success = false;
try {
if (lineNum > 1) {
lineNum);
}
if (!success) {
throw new Exception("ERROR: Failed to copy lines");
}
if (!success) {
throw new Exception("ERROR: Writing to File");
}
}
success = true;
}
}
}
return success;
}
/**
* Method removeLinesByNum
*
*
* @param filePath
* @param lineNum
* begining line number
* @param numLines
* total number of lines to remove
*
* @return true if removal is successful
*
*/
int numLines) {
boolean result = true;
try {
} catch (Exception e) {
result = false;
}
return result;
}
/**
* Method removeLinesByNumber
*
*
* @param filePath
* @param lineNum
* @param numLines
*
* @return
*
*/
int numLines) {
boolean success = false;
try {
if (lineNum > 1) {
lineNum);
}
if (numLines <= 0) {
numLines = 1;
}
for (int i = 0; i < numLines; i++) {
}
if (!success) {
throw new Exception("ERROR: Failed to copy lines");
}
if (!success) {
throw new Exception("ERROR: Writing to File");
}
}
}
ex);
}
return line;
}
/**
* Appends the given set of lines to the specified file.
*
* @param filePath
* @param linesToAppend
* @return true for success, false otherwise
*/
String[] linesToAppend) {
}
/**
* Appends the given set of lines to the specified file.
*
* @param filePath
* @param linesToAppend
* @param create should the file be created if it does not exist
* @return true for success, false otherwise
*/
boolean result = false;
try {
}
}
if (result) {
if (result) {
}
}
}
}
result = false;
}
return result;
}
/**
* Method getProperties
*
* Loads a properties file into memory and creates a
* <code>java.util.Properties</code> object
*
* @param configPath
* path to the properties file
* @return prop a <code>java.util.Properties</code> object from the
* specified file or <code>null</code> in case of an error in
* processing
*/
synchronized (prop) {
try {
new FileInputStream(configPath));
} catch (Exception e) {
e.printStackTrace();
}
}// end sync
return prop;
}
/**
* Method writeToFile
*
*
* @param filePath
* @param writer
*
* @return
*
*/
boolean success = false;
try {
// System.out.println("WARNING :File " + destFile +
// "Overwritten");
}
success = true;
}
}
return success;
}
/**
* Method writeLine
*
*
* @param writer
* @param line
*
*/
try {
}
}
}
/**
* Method copyTillLine
*
*
* @param reader
* @param writer
* @param lineNum
*
* @return
*
*/
int lineNum) {
int curLine = -1;
try {
do {
}
}
}
return reader;
}
/**
* Method copyTillEnd
*
*
* @param reader
* @param writer
*
* @return
*
*/
boolean success = false;
try {
}
}
success = true;
}
return success;
}
/**
* Method declaration
*
*
* @param fileName
* @param property
*
* @return
*
* @see
*/
int matchIndex = -1;
try {
new FileReader(file));
int prevLineNum = -1;
int presentLineNo = 0;
while (presentLine != null) {
if ((presentLine != null)
break;
}
}
}
}
}
return matchIndex;
}
/**
* Method getLineNumReader
*
*
* @param filePath
*
* @return
*
*/
return getLineNumReader(filePath, false);
}
/**
* Method getLineNumReader
*
*
* @param filePath the path to the file
* @param create should we create the file if it does not exist
*
* @return
*
*/
try {
}
}
return reader;
}
/**
* Returns the parent folder upper level count to the current path
*
* @param level how much should we go upper in the path
* @return path to the parent upper the given level, for example:
* (/a/b/c/d, 3) will result in /a
*/
for (int i = 0 ; i < level; i++) {
}
}
return ret;
}
/**
*
* @return path to the parent, it may be null, if there is no parent.
*/
}
/** Field SQRBRACKET_OPEN * */
/** Field SQRBRACKET_CLOSE * */
/** Field EXAMPLE_FIELD * */
/** Field HASH * */
/** Field HASH * */
/** Field HASH * */
}