Author: schor Date: Tue Dec 4 16:56:46 2018 New Revision: 1848166 URL: http://svn.apache.org/viewvc?rev=1848166&view=rev Log: [UIMA-5923] deprecate duplicated methods in FileUtil(s)
Modified: uima/uv3/uimaj-v3/trunk/uimaj-core/src/main/java/org/apache/uima/pear/tools/InstallationController.java uima/uv3/uimaj-v3/trunk/uimaj-core/src/main/java/org/apache/uima/pear/tools/LocalInstallationAgent.java uima/uv3/uimaj-v3/trunk/uimaj-core/src/main/java/org/apache/uima/pear/util/FileUtil.java uima/uv3/uimaj-v3/trunk/uimaj-core/src/main/java/org/apache/uima/util/FileUtils.java Modified: uima/uv3/uimaj-v3/trunk/uimaj-core/src/main/java/org/apache/uima/pear/tools/InstallationController.java URL: http://svn.apache.org/viewvc/uima/uv3/uimaj-v3/trunk/uimaj-core/src/main/java/org/apache/uima/pear/tools/InstallationController.java?rev=1848166&r1=1848165&r2=1848166&view=diff ============================================================================== --- uima/uv3/uimaj-v3/trunk/uimaj-core/src/main/java/org/apache/uima/pear/tools/InstallationController.java (original) +++ uima/uv3/uimaj-v3/trunk/uimaj-core/src/main/java/org/apache/uima/pear/tools/InstallationController.java Tue Dec 4 16:56:46 2018 @@ -32,6 +32,9 @@ import java.io.StringWriter; import java.net.InetAddress; import java.net.MalformedURLException; import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; import java.util.ArrayList; import java.util.Collection; import java.util.Enumeration; @@ -718,7 +721,7 @@ public class InstallationController { File targetDir, InstallationController controller, boolean cleanTarget) throws IOException { // get PEAR file size - long fileSize = FileUtil.getFileSize(pearFileLocation); + long fileSize = Files.size(Paths.get(pearFileLocation)); // create root directory if (!targetDir.isDirectory() && !targetDir.mkdirs()) { //create localized error message @@ -767,9 +770,15 @@ public class InstallationController { + pearFileUrl.toExternalForm() + " to " + targetDir.getAbsolutePath()); String pearFileName = (new File(pearFileUrl.getFile())).getName(); pearFile = new File(targetDir, pearFileName); - if (!FileUtil.copyFile(pearFileUrl, pearFile)) + + try (InputStream pearIn = pearFileUrl.openStream()) { + Files.copy(pearIn, pearFile.toPath(), StandardCopyOption.REPLACE_EXISTING); + } + catch (IOException e) { throw new IOException("cannot copy " + pearFileUrl + " to file " - + pearFile.getAbsolutePath()); + + pearFile.getAbsolutePath(), e); + } + removeLocalCopy = true; } if (controller != null) // write message to OUT msg queue Modified: uima/uv3/uimaj-v3/trunk/uimaj-core/src/main/java/org/apache/uima/pear/tools/LocalInstallationAgent.java URL: http://svn.apache.org/viewvc/uima/uv3/uimaj-v3/trunk/uimaj-core/src/main/java/org/apache/uima/pear/tools/LocalInstallationAgent.java?rev=1848166&r1=1848165&r2=1848166&view=diff ============================================================================== --- uima/uv3/uimaj-v3/trunk/uimaj-core/src/main/java/org/apache/uima/pear/tools/LocalInstallationAgent.java (original) +++ uima/uv3/uimaj-v3/trunk/uimaj-core/src/main/java/org/apache/uima/pear/tools/LocalInstallationAgent.java Tue Dec 4 16:56:46 2018 @@ -23,6 +23,8 @@ import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.StandardCopyOption; import java.util.Collection; import java.util.Hashtable; import java.util.Iterator; @@ -336,12 +338,11 @@ public class LocalInstallationAgent { File orgFile = dirList.next(); String bakFileName = orgFile.getName().concat(BACKUP_FILE_SUFFIX); File bakFile = new File(orgFile.getParent(), bakFileName); - if (FileUtil.copyFile(orgFile, bakFile)) { - // localize original file - localizeComponentFile(orgFile, _insdObject, _packageConfig); - // add to localized file list - fileList[fileCounter++] = orgFile; - } + Files.copy(orgFile.toPath(), bakFile.toPath(), StandardCopyOption.REPLACE_EXISTING); + // localize original file + localizeComponentFile(orgFile, _insdObject, _packageConfig); + // add to localized file list + fileList[fileCounter++] = orgFile; } // backup and localize files in desc dir dirList = descDirFiles.iterator(); @@ -349,12 +350,11 @@ public class LocalInstallationAgent { File orgFile = dirList.next(); String bakFileName = orgFile.getName().concat(BACKUP_FILE_SUFFIX); File bakFile = new File(orgFile.getParent(), bakFileName); - if (FileUtil.copyFile(orgFile, bakFile)) { - // localize original file - localizeComponentFile(orgFile, _insdObject, _packageConfig); - // add to localized file list - fileList[fileCounter++] = orgFile; - } + Files.copy(orgFile.toPath(), bakFile.toPath(), StandardCopyOption.REPLACE_EXISTING); + // localize original file + localizeComponentFile(orgFile, _insdObject, _packageConfig); + // add to localized file list + fileList[fileCounter++] = orgFile; } return fileList; } @@ -369,19 +369,15 @@ public class LocalInstallationAgent { * if any I/O exception occurred. */ public synchronized boolean undoComponentLocalization() throws IOException { - boolean completed = false; + boolean completed; int counter = 0; for (int i = 0; i < _localizedFiles.length; i++) { File orgFile = _localizedFiles[i]; String bakFileName = orgFile.getName().concat(BACKUP_FILE_SUFFIX); File bakFile = new File(orgFile.getParent(), bakFileName); - if (FileUtil.copyFile(bakFile, orgFile)) { - bakFile.delete(); - counter++; - } else { - System.err.println("[LocalInstallationAgent]: " + "failed to undo changes for the file " - + orgFile.getAbsolutePath()); - } + Files.copy(bakFile.toPath(), orgFile.toPath(), StandardCopyOption.REPLACE_EXISTING); + bakFile.delete(); + counter++; } completed = (counter == _localizedFiles.length); return completed; Modified: uima/uv3/uimaj-v3/trunk/uimaj-core/src/main/java/org/apache/uima/pear/util/FileUtil.java URL: http://svn.apache.org/viewvc/uima/uv3/uimaj-v3/trunk/uimaj-core/src/main/java/org/apache/uima/pear/util/FileUtil.java?rev=1848166&r1=1848165&r2=1848166&view=diff ============================================================================== --- uima/uv3/uimaj-v3/trunk/uimaj-core/src/main/java/org/apache/uima/pear/util/FileUtil.java (original) +++ uima/uv3/uimaj-v3/trunk/uimaj-core/src/main/java/org/apache/uima/pear/util/FileUtil.java Tue Dec 4 16:56:46 2018 @@ -39,6 +39,9 @@ import java.io.StringWriter; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; +import java.nio.file.CopyOption; +import java.nio.file.Path; +import java.nio.file.attribute.FileAttribute; import java.util.ArrayList; import java.util.Collection; import java.util.Comparator; @@ -381,9 +384,11 @@ public class FileUtil { * otherwise. * @throws IOException * If any I/O exception occurred. + * @deprecated use Java 7 for this see {@link java.nio.file.Files#copy(Path, Path, CopyOption...)} */ + @Deprecated public static boolean copyFile(File source, File destination) throws IOException { - boolean completed = false; + boolean completed; BufferedInputStream iStream = null; BufferedOutputStream oStream = null; try { @@ -425,7 +430,9 @@ public class FileUtil { * otherwise. * @throws IOException * If any I/O exception occurred. + * @deprecated use Java 7 for this see {@link java.nio.file.Files#copy(InputStream, Path, CopyOption...)} */ + @Deprecated public static boolean copyFile(URL sourceUrl, File destination) throws IOException { boolean completed = false; BufferedInputStream iStream = null; @@ -634,7 +641,10 @@ public class FileUtil { * @return The <code>File</code> object denoting the newly created file. * @throws IOException * If a temporary directory not found or other I/O exception occurred. + * @deprecated use Java 7 method for this see + * {@link java.nio.file.Files#createTempFile(String, String, FileAttribute ...)} */ + @Deprecated public static File createTempFile(String prefix, String suffix) throws IOException { String tempDirPath = System.getProperty("java.io.tmpdir"); if (tempDirPath == null) @@ -842,7 +852,9 @@ public class FileUtil { * @param fileLocation * The given file location - local file path or URL. * @return The given file size, if the specified file can be accessed, -1 otherwise. + * @deprecated use Java 7 method for this see {@link java.nio.file.Files#size(Path)} */ + @Deprecated public static long getFileSize(String fileLocation) { long fileSize = 0; // choose file size method: local FS or HTTP @@ -1011,7 +1023,9 @@ public class FileUtil { * @return The array of non-empty strings loaded from the given text file. * @throws IOException * If any I/O exception occurred. + * @deprecated use Java 7 method for this see {@link java.nio.file.Files#readAllLines(Path, Charset)} */ + @Deprecated public static String[] loadListOfStrings(File textFile) throws IOException { BufferedReader iStream = null; String[] outputArray = null; @@ -1137,7 +1151,11 @@ public class FileUtil { * The given text file. * @throws IOException * If any I/O exception occurs. + * @deprecated use main file util for this, see + * {@link org.apache.uima.util.FileUtils#file2String(File)} + * if using the default charset is OK */ + @Deprecated public static String loadTextFile(File textFile) throws IOException { BufferedReader iStream = null; String content = null; @@ -1167,7 +1185,11 @@ public class FileUtil { * The given text file encoding name. * @throws IOException * If any I/O exception occurs. + * @deprecated use main file util for this, see + * {@link org.apache.uima.util.FileUtils#file2String(File, String)} + * if using the default Charset is OK */ + @Deprecated public static String loadTextFile(File textFile, String encoding) throws IOException { BufferedReader iStream = null; String content = null; @@ -1301,7 +1323,9 @@ public class FileUtil { * otherwise. * @throws IOException * If any I/O exception occurred. + * @deprecated use Java 7 for this see {@link java.nio.file.Files#move(Path, Path, CopyOption...)} */ + @Deprecated public static boolean moveFile(File source, File destinationDir) throws IOException { boolean completed = false; File destination = new File(destinationDir, source.getName()); Modified: uima/uv3/uimaj-v3/trunk/uimaj-core/src/main/java/org/apache/uima/util/FileUtils.java URL: http://svn.apache.org/viewvc/uima/uv3/uimaj-v3/trunk/uimaj-core/src/main/java/org/apache/uima/util/FileUtils.java?rev=1848166&r1=1848165&r2=1848166&view=diff ============================================================================== --- uima/uv3/uimaj-v3/trunk/uimaj-core/src/main/java/org/apache/uima/util/FileUtils.java (original) +++ uima/uv3/uimaj-v3/trunk/uimaj-core/src/main/java/org/apache/uima/util/FileUtils.java Tue Dec 4 16:56:46 2018 @@ -29,9 +29,11 @@ import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.Reader; import java.nio.charset.Charset; +import java.nio.file.CopyOption; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardOpenOption; +import java.nio.file.attribute.FileAttribute; import java.util.ArrayList; import java.util.Random; @@ -282,7 +284,10 @@ public class FileUtils { * Prefix of the directory names to be created. * @return A file object corresponding to the newly created dir, or <code>null</code> if none * could be created for some reason (e.g., if the parent is not writable). + * @deprecated use Java 7 methods for this see + * {@link java.nio.file.Files#createTempDirectory(Path, String, FileAttribute ...)} */ + @Deprecated public static final File createTempDir(File parent, String prefix) { Random rand = new Random(); File tempDir; @@ -298,6 +303,11 @@ public class FileUtils { } } + /** + * @deprecated use Java 7 methods for this see + * {@link java.nio.file.File#createTempFile(String, String, File)} + */ + @Deprecated public static final File createTempFile(String prefix, String suffix, File tempDir) throws IOException { File file = File.createTempFile(prefix, suffix, tempDir); @@ -323,7 +333,9 @@ public class FileUtils { * For various reason: if <code>file</code> does not exist or is not readable, if the * destination directory does not exist or isn't a directory, or if the file can't be * copied for any reason. + * @deprecated use Java 7 for this see {@link java.nio.file.Files#copy(Path, Path, CopyOption...)} */ + @Deprecated public static final void copyFile(File file, File dir) throws IOException { if (!file.exists() || !file.canRead()) { throw new IOException("File does not exist or is not readable: " + file.getAbsolutePath());