You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by el...@apache.org on 2020/05/27 15:08:58 UTC
[maven-shared-utils] branch master updated: [MSHARED-893] deprecate
file methods we don't need in Java 7+ (#35)
This is an automated email from the ASF dual-hosted git repository.
elharo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-shared-utils.git
The following commit(s) were added to refs/heads/master by this push:
new 2c2b747 [MSHARED-893] deprecate file methods we don't need in Java 7+ (#35)
2c2b747 is described below
commit 2c2b747835fdb6610723a7bf494c99be7bce50ff
Author: Elliotte Rusty Harold <el...@users.noreply.github.com>
AuthorDate: Wed May 27 11:08:51 2020 -0400
[MSHARED-893] deprecate file methods we don't need in Java 7+ (#35)
* deprecate file methods we don't need in Java 7+
* fix line length
* we only support Java 7+ now
* remove unused method
---
.../apache/maven/shared/utils/io/FileUtils.java | 130 +++++++++++----------
.../maven/shared/utils/io/FileUtilsTest.java | 2 -
2 files changed, 69 insertions(+), 63 deletions(-)
diff --git a/src/main/java/org/apache/maven/shared/utils/io/FileUtils.java b/src/main/java/org/apache/maven/shared/utils/io/FileUtils.java
index 3bce615..a3014ec 100644
--- a/src/main/java/org/apache/maven/shared/utils/io/FileUtils.java
+++ b/src/main/java/org/apache/maven/shared/utils/io/FileUtils.java
@@ -161,47 +161,52 @@ public class FileUtils
* Returns the directory path portion of a file specification string.
* Matches the equally named unix command.
*
- * @param filename the file path
+ * @param path the file path
* @return the directory portion excluding the ending file separator
+ * @deprecated use {@code Paths.get(path).getParent().getName()}
*/
- @Nonnull public static String dirname( @Nonnull String filename )
+ @Deprecated
+ @Nonnull public static String dirname( @Nonnull String path )
{
- int i = filename.lastIndexOf( File.separator );
- return ( i >= 0 ? filename.substring( 0, i ) : "" );
+ int i = path.lastIndexOf( File.separator );
+ return ( i >= 0 ? path.substring( 0, i ) : "" );
}
/**
- * Returns the filename portion of a file specification string.
+ * Returns the filename portion of a path.
*
- * @param filename the file path
+ * @param path the file path
* @return the filename string with extension
+ * @deprecated use {@code Paths.get(path).getName()}
*/
- @Nonnull public static String filename( @Nonnull String filename )
+ @Deprecated
+ @Nonnull public static String filename( @Nonnull String path )
{
- int i = filename.lastIndexOf( File.separator );
- return ( i >= 0 ? filename.substring( i + 1 ) : filename );
+ int i = path.lastIndexOf( File.separator );
+ return ( i >= 0 ? path.substring( i + 1 ) : path );
}
/**
- * Returns the extension portion of a file specification string.
- * This everything after the last dot '.' in the filename (NOT including
- * the dot).
+ * Returns the extension portion of a file path.
+ * This is everything after the last dot '.' in the path (NOT including the dot).
*
- * @param filename the file path
+ * @param path the file path
* @return the extension of the file
+ * @deprecated use {@code org.apache.commons.io.FilenameUtils.getExtension}
*/
- @Nonnull public static String extension( @Nonnull String filename )
+ @Deprecated
+ @Nonnull public static String extension( @Nonnull String path )
{
// Ensure the last dot is after the last file separator
- int lastSep = filename.lastIndexOf( File.separatorChar );
+ int lastSep = path.lastIndexOf( File.separatorChar );
int lastDot;
if ( lastSep < 0 )
{
- lastDot = filename.lastIndexOf( '.' );
+ lastDot = path.lastIndexOf( '.' );
}
else
{
- lastDot = filename.substring( lastSep + 1 ).lastIndexOf( '.' );
+ lastDot = path.substring( lastSep + 1 ).lastIndexOf( '.' );
if ( lastDot >= 0 )
{
lastDot += lastSep + 1;
@@ -210,7 +215,7 @@ public class FileUtils
if ( lastDot >= 0 && lastDot > lastSep )
{
- return filename.substring( lastDot + 1 );
+ return path.substring( lastDot + 1 );
}
return "";
@@ -221,7 +226,9 @@ public class FileUtils
*
* @param fileName the file path
* @return true if file exists
+ * @deprecated use {@code java.io.File.exists()}
*/
+ @Deprecated
public static boolean fileExists( @Nonnull String fileName )
{
File file = new File( fileName );
@@ -234,7 +241,9 @@ public class FileUtils
* @param file the file path
* @return the file content using the platform encoding
* @throws IOException if any
+ * @deprecated use {@code new String(java.nio.files.Files.readAllBytes(file))}
*/
+ @Deprecated
@Nonnull public static String fileRead( @Nonnull String file )
throws IOException
{
@@ -246,7 +255,9 @@ public class FileUtils
* @param encoding the wanted encoding
* @return the file content using the specified encoding
* @throws IOException if any
+ * @deprecated use {@code new String(java.nio.files.Files.readAllBytes(Paths.get(file)), encoding)}
*/
+ @Deprecated
@Nonnull private static String fileRead( @Nonnull String file, @Nullable String encoding )
throws IOException
{
@@ -259,7 +270,9 @@ public class FileUtils
* @param file the file path
* @return the file content using the platform encoding
* @throws IOException if any
+ * @deprecated use {@code new String(java.nio.files.Files.readAllBytes(file.toPath()))}
*/
+ @Deprecated
@Nonnull public static String fileRead( @Nonnull File file )
throws IOException
{
@@ -271,7 +284,9 @@ public class FileUtils
* @param encoding the wanted encoding
* @return the file content using the specified encoding
* @throws IOException if any
+ * @deprecated use {@code new String(java.nio.files.Files.readAllBytes(file.toPath()), encoding)}
*/
+ @Deprecated
@Nonnull public static String fileRead( @Nonnull File file, @Nullable String encoding )
throws IOException
{
@@ -298,7 +313,9 @@ public class FileUtils
* @return the file content lines as String[] using the system default encoding.
* An empty List if the file doesn't exist.
* @throws IOException in case of failure
+ * @deprecated use {@code java.nio.files.Files.readAllLines()}
*/
+ @Deprecated
@Nonnull public static String[] fileReadArray( @Nonnull File file )
throws IOException
{
@@ -314,7 +331,10 @@ public class FileUtils
* @param fileName the path of the file to write
* @param data the content to write to the file
* @throws IOException if any
+ * @deprecated use {@code java.nio.files.Files.write(filename, data.getBytes(),
+ * StandardOpenOption.APPEND, StandardOpenOption.CREATE)}
*/
+ @Deprecated
public static void fileAppend( @Nonnull String fileName, @Nonnull String data )
throws IOException
{
@@ -328,7 +348,10 @@ public class FileUtils
* @param encoding the encoding of the file
* @param data the content to write to the file
* @throws IOException if any
+ * @deprecated use {@code java.nio.files.Files.write(filename, data.getBytes(encoding),
+ * StandardOpenOption.APPEND, StandardOpenOption.CREATE)}
*/
+ @Deprecated
public static void fileAppend( @Nonnull String fileName, @Nullable String encoding, @Nonnull String data )
throws IOException
{
@@ -347,7 +370,10 @@ public class FileUtils
* @param fileName the path of the file to write
* @param data the content to write to the file
* @throws IOException if any
+ * @deprecated use {@code java.nio.files.Files.write(filename,
+ * data.getBytes(), StandardOpenOption.CREATE)}
*/
+ @Deprecated
public static void fileWrite( @Nonnull String fileName, @Nonnull String data )
throws IOException
{
@@ -361,7 +387,10 @@ public class FileUtils
* @param encoding the encoding of the file
* @param data the content to write to the file
* @throws IOException if any
+ * @deprecated use {@code java.nio.files.Files.write(Paths.get(filename),
+ * data.getBytes(encoding), StandardOpenOption.CREATE)}
*/
+ @Deprecated
public static void fileWrite( @Nonnull String fileName, @Nullable String encoding, @Nonnull String data )
throws IOException
{
@@ -376,7 +405,10 @@ public class FileUtils
* @param encoding the encoding of the file
* @param data the content to write to the file
* @throws IOException if any
+ * @deprecated use {@code java.nio.files.Files.write(file.toPath(),
+ * data.getBytes(encoding), StandardOpenOption.CREATE)}
*/
+ @Deprecated
public static void fileWrite( @Nonnull File file, @Nullable String encoding, @Nonnull String data )
throws IOException
{
@@ -395,7 +427,10 @@ public class FileUtils
* @param file the path of the file to write
* @param data the content to write to the file
* @throws IOException if any
+ * @deprecated use {@code java.nio.files.Files.write(file.toPath(),
+ * data.getBytes(encoding), StandardOpenOption.CREATE)}
*/
+ @Deprecated
public static void fileWriteArray( @Nonnull File file, @Nullable String... data )
throws IOException
{
@@ -405,11 +440,14 @@ public class FileUtils
/**
* Writes String array data to a file. The file is created if it does not exist.
*
- * @param file the path of the file to write
+ * @param file the path of the file to write
* @param encoding the encoding of the file
- * @param data the content to write to the file
+ * @param data the content to write to the file
* @throws IOException if any
+ * @deprecated use {@code java.nio.files.Files.write(file.toPath(),
+ * data.getBytes(encoding), StandardOpenOption.CREATE)}
*/
+ @Deprecated
public static void fileWriteArray( @Nonnull File file, @Nullable String encoding, @Nullable String... data )
throws IOException
{
@@ -744,7 +782,7 @@ public class FileUtils
* @param destination a non-directory <code>File</code> to write bytes to (possibly
* overwriting)
* @throws IOException if <code>source</code> does not exist, <code>destination</code> cannot be
- * written to, or an IO error occurs during copying.
+ * written to, or an IO error occurs during copying
* @throws java.io.FileNotFoundException if <code>destination</code> is a directory
*/
public static void copyFile( @Nonnull final File source, @Nonnull final File destination )
@@ -970,8 +1008,8 @@ public class FileUtils
*
* @param baseFile Where to resolve <code>filename</code> from, if <code>filename</code> is
* relative.
- * @param filename Absolute or relative file path to resolve.
- * @return The canonical <code>File</code> of <code>filename</code>.
+ * @param filename absolute or relative file path to resolve
+ * @return the canonical <code>File</code> of <code>filename</code>
*/
@Nonnull public static File resolveFile( final File baseFile, @Nonnull String filename )
{
@@ -1285,7 +1323,7 @@ public class FileUtils
* Recursively count size of a directory.
*
* @param directory a directory
- * @return size of directory in bytes.
+ * @return size of directory in bytes
*/
public static long sizeOfDirectory( @Nonnull final String directory )
{
@@ -1296,7 +1334,7 @@ public class FileUtils
* Recursively count size of a directory.
*
* @param directory a directory
- * @return size of directory in bytes.
+ * @return size of directory in bytes
*/
public static long sizeOfDirectory( @Nonnull final File directory )
{
@@ -2008,18 +2046,12 @@ public class FileUtils
/**
* Checks whether a given file is a symbolic link.
*
- * This only works reliably on java7 and higher. For earlier version we use a highly crappy heuristic
- * that mostly does not work.
- * <p>
- * It doesn't really test for symbolic links but whether the canonical and absolute paths of the file are identical
- * - this may lead to false positives on some platforms.
- * </p>
- *
* @param file the file to check
* @throws IOException in case of failure.
* @return true if symbolic link false otherwise.
- *
+ * @deprecated use {@code java.nio.file.Files.isSymbolicLink(file.toPath())}
*/
+ @Deprecated
public static boolean isSymbolicLink( @Nonnull final File file )
throws IOException
{
@@ -2030,11 +2062,12 @@ public class FileUtils
* Checks whether a given file is a symbolic link.
*
* @param file the file to check
- * @return true if and only if we reliably can say this is a symlink. This will
- * always return false for java versions prior to 1.7.
+ * @return true if and only if we reliably can say this is a symlink
*
- * @throws IOException in case of failure.
+ * @throws IOException in case of failure
+ * @deprecated use {@code java.nio.file.Files.isSymbolicLink(file.toPath())}
*/
+ @Deprecated
public static boolean isSymbolicLinkForSure( @Nonnull final File file )
throws IOException
{
@@ -2042,31 +2075,6 @@ public class FileUtils
}
/**
- * Checks whether a given file is a symbolic link.
- * <p>
- * It doesn't really test for symbolic links but whether the canonical and absolute
- * paths of the file are identical - this may lead to false positives on some platforms.
- *
- * It also returns true for any file that has been reached via a symbolic link,
- * if you decide to traverse into the symlink.
- *
- * As can be seen from the "return" clause of this method, there is really no
- * guarantee of any sort from this method. Small wonder this ever got used for
- * anything.
- * </p>
- *
- * @param file the file to check
- * @return true if the file is a symbolic link or if we're on some crappy os.
- * false if the file is not a symlink or we're not able to detect it.
- */
- static boolean isSymbolicLinkLegacy( @Nonnull final File file )
- throws IOException
- {
- final File canonical = new File( file.getCanonicalPath() );
- return !file.getAbsolutePath().equals( canonical.getPath() );
- }
-
- /**
* @param symlink The link name.
* @param target The target.
* @return The linked file.
diff --git a/src/test/java/org/apache/maven/shared/utils/io/FileUtilsTest.java b/src/test/java/org/apache/maven/shared/utils/io/FileUtilsTest.java
index 3a09bac..90d93c6 100644
--- a/src/test/java/org/apache/maven/shared/utils/io/FileUtilsTest.java
+++ b/src/test/java/org/apache/maven/shared/utils/io/FileUtilsTest.java
@@ -1713,7 +1713,6 @@ public class FileUtilsTest
File file = new File( "src/test/resources/symlinks/src/symDir" );
assertTrue(FileUtils.isSymbolicLink(file ));
- assertTrue(FileUtils.isSymbolicLinkLegacy(file ));
}
@Test
@@ -1723,7 +1722,6 @@ public class FileUtilsTest
{
File file = new File( "src/test/resources/symlinks/src/" );
assertFalse(FileUtils.isSymbolicLink(file ));
- assertFalse(FileUtils.isSymbolicLinkLegacy(file ));
}
@Test