This is an automated email from the ASF dual-hosted git repository. rec pushed a commit to branch UIMA-6469-Cleaning-up-file-handling-code in repository https://gitbox.apache.org/repos/asf/uima-uimaj.git
commit 1088f615154b385dd163965b8d2ef78e87111ab1 Author: Richard Eckart de Castilho <[email protected]> AuthorDate: Fri Jun 3 08:35:36 2022 +0200 [UIMA-6469] Cleaning up file-handling code - Introduce various constants - Use try-with-resources to more effect --- .../java/org/apache/uima/pear/util/FileUtil.java | 345 ++++++++++----------- 1 file changed, 169 insertions(+), 176 deletions(-) diff --git a/uimaj-core/src/main/java/org/apache/uima/pear/util/FileUtil.java b/uimaj-core/src/main/java/org/apache/uima/pear/util/FileUtil.java index f5f430938..3874c3129 100644 --- a/uimaj-core/src/main/java/org/apache/uima/pear/util/FileUtil.java +++ b/uimaj-core/src/main/java/org/apache/uima/pear/util/FileUtil.java @@ -63,8 +63,14 @@ import org.apache.uima.util.impl.Constants; /** * The <code>FileUtil</code> class provides utility methods for working with general files. */ - public class FileUtil { + private static final String UTF8_ENCODING = "UTF-8"; + private static final String ASCII_ENCODING = "ASCII"; + private static final String XML_EXTENSION = ".xml"; + private static final String BACKUP_EXTENSION = ".bak"; + private static final String ZIP_EXTENSION = ".zip"; + private static final char UNIX_SEPARATOR = '/'; + private static final char WINDOWS_SEPARATOR = '\\'; /** * The <code>FileTimeComparator</code> class allows comparing 'last modified' time in 2 given @@ -116,11 +122,12 @@ public class FileUtil { * The given file extension. */ public DirFileFilter(String dirPath, String fileExt) { - _dirPath = (dirPath != null) ? dirPath.replace('\\', '/') : null; - if (fileExt != null) + _dirPath = (dirPath != null) ? dirPath.replace(WINDOWS_SEPARATOR, UNIX_SEPARATOR) : null; + if (fileExt != null) { _fileExt = fileExt.startsWith(".") ? fileExt.toLowerCase() : "." + fileExt.toLowerCase(); - else + } else { _fileExt = null; + } } /** @@ -134,7 +141,8 @@ public class FileUtil { boolean extAccepted = true; if (_dirPath != null) { String parentDir = file.getParent(); - dirAccepted = parentDir != null && parentDir.replace('\\', '/').startsWith(_dirPath); + dirAccepted = parentDir != null + && parentDir.replace(WINDOWS_SEPARATOR, UNIX_SEPARATOR).startsWith(_dirPath); } if (_fileExt != null) { extAccepted = file.getPath().toLowerCase().endsWith(_fileExt); @@ -145,7 +153,6 @@ public class FileUtil { /** * The <code>NameFileFilter</code> class allows to filter files based on specified file name. - * */ public static class NameFileFilter implements FileFilter { // attributes @@ -158,7 +165,7 @@ public class FileUtil { * The given file name for filtering. */ public NameFileFilter(String fileName) { - _fileName = fileName.replace('\\', '/'); + _fileName = fileName.replace(WINDOWS_SEPARATOR, UNIX_SEPARATOR); } /** @@ -168,14 +175,16 @@ public class FileUtil { */ @Override public boolean accept(File file) { - String filePath = file.getAbsolutePath().replace('\\', '/'); + String filePath = file.getAbsolutePath().replace(WINDOWS_SEPARATOR, UNIX_SEPARATOR); if (filePath.endsWith(_fileName)) { if (filePath.length() > _fileName.length()) { char prevChar = filePath.charAt(filePath.length() - _fileName.length() - 1); - if (prevChar == ':' || prevChar == '/') + if (prevChar == ':' || prevChar == UNIX_SEPARATOR) { return true; - } else + } + } else { return true; + } } return false; } @@ -218,8 +227,9 @@ public class FileUtil { public ExtFilenameFilter(String fileExt, boolean ignoreCase) { _fileExt = fileExt.startsWith(".") ? fileExt : "." + fileExt; _ignoreCase = ignoreCase; - if (ignoreCase) + if (ignoreCase) { _fileExt = _fileExt.toLowerCase(); + } } /** @@ -258,15 +268,17 @@ public class FileUtil { File aFile = allDirFiles[i]; if (aFile.isDirectory()) { counter += cleanUpDirectoryContent(aFile); - if (aFile.delete()) + if (aFile.delete()) { counter++; - else + } else { aFile.deleteOnExit(); + } } else if (aFile.isFile()) { - if (aFile.delete()) + if (aFile.delete()) { counter++; - else + } else { aFile.deleteOnExit(); + } } } } @@ -290,10 +302,11 @@ public class FileUtil { for (int i = 0; i < allDirFiles.length; i++) { File aFile = allDirFiles[i]; if (aFile.isFile()) { - if (aFile.delete()) + if (aFile.delete()) { counter++; - else + } else { aFile.deleteOnExit(); + } } } } @@ -324,10 +337,11 @@ public class FileUtil { File file = list.next(); no++; if (no > maxLimit) { - if (file.delete()) + if (file.delete()) { counter++; - else + } else { file.deleteOnExit(); + } } } } @@ -350,25 +364,28 @@ public class FileUtil { */ public static String computeRelativePath(File referenceDir, File file) throws IOException { // get canonical path expressions - String refPath = referenceDir.getCanonicalPath().replace('\\', '/'); - String filePath = file.getCanonicalPath().replace('\\', '/'); + String refPath = referenceDir.getCanonicalPath().replace(WINDOWS_SEPARATOR, UNIX_SEPARATOR); + String filePath = file.getCanonicalPath().replace(WINDOWS_SEPARATOR, UNIX_SEPARATOR); // compute relative path from reference dir to file dir-tree StringBuffer relBuffer = new StringBuffer(); while (refPath != null && !filePath.startsWith(refPath)) { relBuffer.append("../"); refPath = (new File(refPath)).getParent(); - if (refPath != null) - refPath = refPath.replace('\\', '/'); + if (refPath != null) { + refPath = refPath.replace(WINDOWS_SEPARATOR, UNIX_SEPARATOR); + } } if (refPath != null) { // construct relative path String subPath = filePath.substring(refPath.length()); - if (relBuffer.length() == 0) + if (relBuffer.length() == 0) { relBuffer.append("./"); - if (subPath.startsWith("/")) + } + if (subPath.startsWith("/")) { relBuffer.append(subPath.substring(1)); - else + } else { relBuffer.append(subPath); + } return relBuffer.toString(); } // relative path does not exist @@ -466,14 +483,16 @@ public class FileUtil { throws IOException { ArrayList<File> listOfDirs = new ArrayList<>(); File[] allDirFiles = rootDir.listFiles(); - if (allDirFiles == null) + if (allDirFiles == null) { throw new FileNotFoundException("invalid directory specified"); + } for (int i = 0; i < allDirFiles.length; i++) { File aFile = allDirFiles[i]; if (aFile.isDirectory()) { listOfDirs.add(aFile); - if (includeSubdirs) + if (includeSubdirs) { listOfDirs.addAll(createDirList(aFile, includeSubdirs)); + } } } return listOfDirs; @@ -503,14 +522,15 @@ public class FileUtil { while (entries.hasMoreElements()) { JarEntry entry = entries.nextElement(); File file = new File(rootDir, entry.getName()); - if (entry.isDirectory()) + if (entry.isDirectory()) { listOfDirs.add(file); - else { + } else { // make sure the parent dir is added File parentDir = file.getParentFile(); while (!parentDir.equals(rootDir)) { - if (!listOfDirs.contains(parentDir)) + if (!listOfDirs.contains(parentDir)) { listOfDirs.add(parentDir); + } parentDir = parentDir.getParentFile(); } } @@ -553,14 +573,16 @@ public class FileUtil { throws IOException { ArrayList<File> listOfFiles = new ArrayList<>(); File[] allDirFiles = filesDir.listFiles(); - if (allDirFiles == null) + if (allDirFiles == null) { throw new FileNotFoundException("invalid directory specified"); + } for (int i = 0; i < allDirFiles.length; i++) { File aFile = allDirFiles[i]; - if (aFile.isDirectory() && includeSubdirs) + if (aFile.isDirectory() && includeSubdirs) { listOfFiles.addAll(createFileList(aFile, includeSubdirs)); - else if (!aFile.isDirectory()) + } else if (!aFile.isDirectory()) { listOfFiles.add(aFile); + } } return listOfFiles; } @@ -588,8 +610,9 @@ public class FileUtil { while (entries.hasMoreElements()) { JarEntry entry = entries.nextElement(); File file = new File(rootDir, entry.getName()); - if (!entry.isDirectory()) + if (!entry.isDirectory()) { listOfFiles.add(file); + } } return listOfFiles; } @@ -614,13 +637,16 @@ public class FileUtil { @Deprecated public static File createTempFile(String prefix, String suffix) throws IOException { String tempDirPath = System.getProperty("java.io.tmpdir"); - if (tempDirPath == null) + if (tempDirPath == null) { tempDirPath = System.getProperty("user.home"); - if (tempDirPath == null) + } + if (tempDirPath == null) { throw new IOException("could not find temporary directory"); + } File tempDir = new File(tempDirPath); - if (!tempDir.isDirectory()) + if (!tempDir.isDirectory()) { throw new IOException("temporary directory not available"); + } return File.createTempFile(prefix, suffix, tempDir); } @@ -643,9 +669,9 @@ public class FileUtil { // first, delete plain files and sub-directories (recursive) for (int i = 0; i < fileList.length; i++) { File entry = fileList[i]; - if (entry.isDirectory()) + if (entry.isDirectory()) { done = deleteDirectory(entry); - else if (!entry.delete()) { + } else if (!entry.delete()) { entry.deleteOnExit(); done = false; } @@ -735,14 +761,16 @@ public class FileUtil { JarEntry jarEntry = jarList.nextElement(); if (!jarEntry.isDirectory()) { // check that file is accepted - if (filter != null && !filter.accept(new File(jarEntry.getName()))) + if (filter != null && !filter.accept(new File(jarEntry.getName()))) { continue; + } // extract file File file = new File(targetDir, jarEntry.getName()); // make sure the file directory exists File dir = file.getParentFile(); - if (!dir.exists() && !dir.mkdirs()) + if (!dir.exists() && !dir.mkdirs()) { throw new IOException("could not create directory " + dir.getAbsolutePath()); + } try (BufferedInputStream iStream = new BufferedInputStream( jarFile.getInputStream(jarEntry)); BufferedOutputStream oStream = new BufferedOutputStream( @@ -787,10 +815,11 @@ public class FileUtil { buffer.append('.'); for (int i = begIndex + 1; i < fileName.length(); i++) { char ch = fileName.charAt(i); - if (Character.isLetterOrDigit(ch)) + if (Character.isLetterOrDigit(ch)) { buffer.append(ch); - else + } else { break; + } } } return buffer.toString(); @@ -809,9 +838,9 @@ public class FileUtil { long fileSize = 0; // choose file size method: local FS or HTTP File file = new File(fileLocation); - if (file.isFile()) + if (file.isFile()) { fileSize = file.length(); - else { + } else { try { URL fileUrl = new URL(fileLocation); URLConnection urlConn = fileUrl.openConnection(); @@ -836,12 +865,14 @@ public class FileUtil { * @return The relative path of the given object, located in the given root directory. */ public static String getRelativePath(File rootDir, String absolutePath) { - String rootDirPath = rootDir.getAbsolutePath().replace('\\', '/'); - String objectPath = absolutePath.replace('\\', '/'); - if (objectPath.startsWith(rootDirPath)) + String rootDirPath = rootDir.getAbsolutePath().replace(WINDOWS_SEPARATOR, UNIX_SEPARATOR); + String objectPath = absolutePath.replace(WINDOWS_SEPARATOR, UNIX_SEPARATOR); + if (objectPath.startsWith(rootDirPath)) { objectPath = objectPath.substring(rootDirPath.length()); - if (objectPath.startsWith("/")) + } + if (objectPath.startsWith("/")) { objectPath = objectPath.substring(1); + } return objectPath; } @@ -861,20 +892,23 @@ public class FileUtil { String utfSignature = null; if (length == 3) { // check for UTF-8 signature - if (prefix[0] == 0xEF && prefix[1] == 0xBB && prefix[2] == 0xBF) - utfSignature = "UTF-8"; + if (prefix[0] == 0xEF && prefix[1] == 0xBB && prefix[2] == 0xBF) { + utfSignature = UTF8_ENCODING; + } } else if (length == 2) { // check for UTF-16 signature - if (prefix[0] == 0xFE && prefix[1] == 0xFF) + if (prefix[0] == 0xFE && prefix[1] == 0xFF) { utfSignature = "UTF-16BE"; - else if (prefix[0] == 0xFF && prefix[1] == 0xFE) + } else if (prefix[0] == 0xFF && prefix[1] == 0xFE) { utfSignature = "UTF-16LE"; + } } else if (length == 4) { // check for UTF-32 signature - if (prefix[0] == 0x00 && prefix[1] == 0x00 && prefix[2] == 0xFE && prefix[3] == 0xFF) + if (prefix[0] == 0x00 && prefix[1] == 0x00 && prefix[2] == 0xFE && prefix[3] == 0xFF) { utfSignature = "UTF-32BE"; - else if (prefix[0] == 0xFF && prefix[1] == 0xFE && prefix[2] == 0x00 && prefix[3] == 0x00) + } else if (prefix[0] == 0xFF && prefix[1] == 0xFE && prefix[2] == 0x00 && prefix[3] == 0x00) { utfSignature = "UTF-32LE"; + } } return utfSignature; } @@ -891,23 +925,9 @@ public class FileUtil { * If an I/O exception occurred. */ public static boolean isAsciiFile(File textFile) throws IOException { - boolean isAscii = true; - FileInputStream iStream = null; - try { - iStream = new FileInputStream(textFile); - isAscii = isAsciiStream(iStream); - iStream.close(); - } catch (IOException exc) { - isAscii = false; - throw exc; - } finally { - if (iStream != null) - try { - iStream.close(); - } catch (Exception e) { - } + try (FileInputStream iStream = new FileInputStream(textFile)) { + return isAsciiStream(iStream); } - return isAscii; } /** @@ -923,17 +943,12 @@ public class FileUtil { */ public static boolean isAsciiStream(InputStream iStream) throws IOException { boolean isAscii = true; - try { - int nextByte = 0; - while ((nextByte = iStream.read()) >= 0) { - if (nextByte > 127) { - isAscii = false; - break; - } + int nextByte = 0; + while ((nextByte = iStream.read()) >= 0) { + if (nextByte > 127) { + isAscii = false; + break; } - } catch (IOException exc) { - isAscii = false; - throw exc; } return isAscii; } @@ -953,8 +968,9 @@ public class FileUtil { String line = null; while ((line = iStream.readLine()) != null) { String string = line.trim(); - if (string.length() > 0) + if (string.length() > 0) { outputList.add(string); + } } if (outputList.size() > 0) { outputArray = new String[outputList.size()]; @@ -1021,7 +1037,7 @@ public class FileUtil { public static Properties loadPropertiesFromJar(String propFilePath, JarFile jarFile) throws IOException { Properties properties = null; - String name = propFilePath.replace('\\', '/'); + String name = propFilePath.replace(WINDOWS_SEPARATOR, UNIX_SEPARATOR); JarEntry jarEntry = jarFile.getJarEntry(name); if (jarEntry != null) { try (InputStream iStream = jarFile.getInputStream(jarEntry)) { @@ -1042,26 +1058,13 @@ public class FileUtil { * If any I/O exception occurs. */ public static String loadTextFile(BufferedReader iStream) throws IOException { - StringWriter buffer = null; - PrintWriter writer = null; - try { - buffer = new StringWriter(); - writer = new PrintWriter(buffer); + try (StringWriter buffer = new StringWriter(); PrintWriter writer = new PrintWriter(buffer);) { String line = null; - while ((line = iStream.readLine()) != null) + while ((line = iStream.readLine()) != null) { writer.println(line); - writer.flush(); - } catch (IOException exc) { - throw exc; - } finally { - if (writer != null) { - try { - writer.close(); - } catch (Exception e) { - } } + return buffer.toString(); } - return buffer.toString(); } /** @@ -1078,11 +1081,9 @@ public class FileUtil { */ @Deprecated public static String loadTextFile(File textFile) throws IOException { - String content; try (BufferedReader iStream = new BufferedReader(new FileReader(textFile))) { - content = loadTextFile(iStream); + return loadTextFile(iStream); } - return content; } /** @@ -1155,7 +1156,7 @@ public class FileUtil { */ public static String loadTextFileFromJar(String filePath, JarFile jarFile) throws IOException { String content = null; - String name = filePath.replace('\\', '/'); + String name = filePath.replace(WINDOWS_SEPARATOR, UNIX_SEPARATOR); JarEntry jarEntry = jarFile.getJarEntry(name); if (jarEntry != null) { try (BufferedReader iStream = new BufferedReader( @@ -1176,7 +1177,7 @@ public class FileUtil { public static String localPathToFileUrl(String path) { // get absolute path File file = new File(path); - String absPath = file.getAbsolutePath().replace('\\', '/'); + String absPath = file.getAbsolutePath().replace(WINDOWS_SEPARATOR, UNIX_SEPARATOR); // construct file URL StringBuffer urlBuffer = new StringBuffer("file:///"); urlBuffer.append(absPath.replace(':', '|')); @@ -1207,8 +1208,9 @@ public class FileUtil { public static boolean moveFile(File source, File destinationDir) throws IOException { boolean completed = false; File destination = new File(destinationDir, source.getName()); - if (destination.exists()) + if (destination.exists()) { destination.delete(); + } if (copyFile(source, destination)) { completed = source.delete(); } @@ -1234,23 +1236,22 @@ public class FileUtil { throws IOException { int counter = 0; // for general text file - supporting ASCII encoding only - String encoding = "ASCII"; + String encoding = ASCII_ENCODING; // check file extension int extIndex = textFile.getName().lastIndexOf('.'); String fileExt = (extIndex > 0) ? textFile.getName().substring(extIndex) : null; - if (".xml".equalsIgnoreCase(fileExt)) { + if (XML_EXTENSION.equalsIgnoreCase(fileExt)) { // for XML file - supporting UTF-8 (ASCII) and UTF-16 encodings String xmlEncoding = XMLUtil.detectXmlFileEncoding(textFile); if (xmlEncoding != null) { encoding = xmlEncoding; } else { - encoding = "UTF-8"; + encoding = UTF8_ENCODING; } } // load text file, using supported encoding String fileContent = loadTextFile(textFile, encoding); - BufferedReader sReader = null; - PrintStream fStream = null; + boolean done = false; File backupFile = null; // get pattern for given regex @@ -1259,51 +1260,48 @@ public class FileUtil { String replaceWith = StringUtil.toRegExpReplacement(replacement); try { // save backup copy of input file - backupFile = new File(textFile.getAbsolutePath() + ".bak"); - if (backupFile.exists()) + backupFile = new File(textFile.getAbsolutePath() + BACKUP_EXTENSION); + if (backupFile.exists()) { backupFile.delete(); - if (!textFile.renameTo(backupFile)) + } + if (!textFile.renameTo(backupFile)) { throw new IOException("can't save backup copy of " + textFile.getAbsolutePath()); - sReader = new BufferedReader(new StringReader(fileContent)); - fStream = new PrintStream(new FileOutputStream(textFile), true, encoding); - String srcLine = null; - while ((srcLine = sReader.readLine()) != null) { - // count pattern matches in the source string - Matcher matcher = pattern.matcher(srcLine); - while (matcher.find()) - counter++; - // replace all pattern matches in the source string - String resLine = srcLine.replaceAll(subStringRegex, replaceWith); - fStream.println(resLine); } - fStream.close(); + try (BufferedReader sReader = new BufferedReader(new StringReader(fileContent)); + PrintStream fStream = new PrintStream(new FileOutputStream(textFile), true, + encoding);) { + String srcLine = null; + while ((srcLine = sReader.readLine()) != null) { + // count pattern matches in the source string + Matcher matcher = pattern.matcher(srcLine); + while (matcher.find()) { + counter++; + } + // replace all pattern matches in the source string + String resLine = srcLine.replaceAll(subStringRegex, replaceWith); + fStream.println(resLine); + } + } done = true; } catch (IOException exc) { throw exc; } catch (Throwable err) { - if (err instanceof IOException) + if (err instanceof IOException) { throw new IOException(err.toString() + " in " + textFile.getAbsolutePath()); + } throw new RuntimeException(err.toString() + " in " + textFile.getAbsolutePath()); } finally { - if (sReader != null) { - try { - sReader.close(); - } catch (Exception e) { - } - } - if (fStream != null) { - try { - fStream.close(); - } catch (Exception e) { - } - } if (done) { // remove backup file - backupFile.delete(); + if (backupFile != null) { + backupFile.delete(); + } } else { // restore input file textFile.delete(); - backupFile.renameTo(textFile); + if (backupFile != null) { + backupFile.renameTo(textFile); + } } } return counter; @@ -1334,7 +1332,7 @@ public class FileUtil { */ public static File zipDirectory(File dir2zip) throws IOException { // construct zipped file path - String zipFileName = dir2zip.getName() + ".zip"; + String zipFileName = dir2zip.getName() + ZIP_EXTENSION; File zipFile = new File(dir2zip, zipFileName); return zipDirectory(dir2zip, zipFile); } @@ -1383,27 +1381,27 @@ public class FileUtil { File referenceDir, File[] excludeFiles) throws IOException { byte[] block = new byte[4096]; int inBytes = 0; - FileInputStream iStream = null; - try { - // get list of all files/dirs in the given directory - File[] dirFileList = dir2zip.listFiles(); - // compress all files and sub-dirs - for (int i = 0; i < dirFileList.length; i++) { - File entry = dirFileList[i]; - // check if this entry is not in the list of exclusions - boolean isExcluded = false; - for (int n = 0; n < excludeFiles.length; n++) { - if (entry.equals(excludeFiles[n])) { - isExcluded = true; - break; - } + + // get list of all files/dirs in the given directory + File[] dirFileList = dir2zip.listFiles(); + // compress all files and sub-dirs + for (int i = 0; i < dirFileList.length; i++) { + File entry = dirFileList[i]; + // check if this entry is not in the list of exclusions + boolean isExcluded = false; + for (int n = 0; n < excludeFiles.length; n++) { + if (entry.equals(excludeFiles[n])) { + isExcluded = true; + break; } - if (isExcluded) - continue; - // for each file - add ZipEntry and compress the file - if (entry.isFile()) { - // open input stream - iStream = new FileInputStream(entry); + } + if (isExcluded) { + continue; + } + // for each file - add ZipEntry and compress the file + if (entry.isFile()) { + // open input stream + try (FileInputStream iStream = new FileInputStream(entry)) { // put ZipEntry for the file String zipEntryName = (referenceDir != null) ? getRelativePath(referenceDir, entry.getAbsolutePath()) @@ -1411,21 +1409,15 @@ public class FileUtil { ZipEntry zipEntry = new ZipEntry(zipEntryName); zoStream.putNextEntry(zipEntry); // read input stream and write to output stream - while ((inBytes = iStream.read(block)) > 0) + while ((inBytes = iStream.read(block)) > 0) { zoStream.write(block, 0, inBytes); - // close input stream - iStream.close(); - } else if (entry.isDirectory()) // zip sub-dir recursively - zipDirectory(entry, zoStream, referenceDir, excludeFiles); - } - } finally { - if (iStream != null) { - try { - iStream.close(); - } catch (Exception e) { + } } + } else if (entry.isDirectory()) { // zip sub-dir recursively + zipDirectory(entry, zoStream, referenceDir, excludeFiles); } } + return zoStream; } @@ -1443,8 +1435,8 @@ public class FileUtil { // construct zipped file path String zipFileName = file2zip.getName(); int extIndex = zipFileName.lastIndexOf('.'); - zipFileName = (extIndex >= 0) ? zipFileName.substring(0, extIndex) + ".zip" - : zipFileName + ".zip"; + zipFileName = (extIndex >= 0) ? zipFileName.substring(0, extIndex) + ZIP_EXTENSION + : zipFileName + ZIP_EXTENSION; File zipFile = new File(file2zip.getParentFile(), zipFileName); return zipFile(file2zip, zipFile); } @@ -1475,8 +1467,9 @@ public class FileUtil { // add new ZipEntry oStream.putNextEntry(zipEntry); // read input stream and write to output stream - while ((inBytes = iStream.read(block)) > 0) + while ((inBytes = iStream.read(block)) > 0) { oStream.write(block, 0, inBytes); + } } finally { if (iStream != null) { try {
