Author: krosenvold Date: Fri Jun 19 16:43:48 2015 New Revision: 1686450 URL: http://svn.apache.org/r1686450 Log: Extracted duplicated logic into methods
Modified: commons/proper/io/trunk/src/main/java/org/apache/commons/io/EndianUtils.java commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java commons/proper/io/trunk/src/main/java/org/apache/commons/io/monitor/FileAlterationObserver.java Modified: commons/proper/io/trunk/src/main/java/org/apache/commons/io/EndianUtils.java URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/EndianUtils.java?rev=1686450&r1=1686449&r2=1686450&view=diff ============================================================================== --- commons/proper/io/trunk/src/main/java/org/apache/commons/io/EndianUtils.java (original) +++ commons/proper/io/trunk/src/main/java/org/apache/commons/io/EndianUtils.java Fri Jun 19 16:43:48 2015 @@ -218,17 +218,9 @@ public class EndianUtils { * @return the value read */ public static long readSwappedLong(final byte[] data, final int offset) { - final long low = - ( ( data[ offset + 0 ] & 0xff ) << 0 ) + - ( ( data[ offset + 1 ] & 0xff ) << 8 ) + - ( ( data[ offset + 2 ] & 0xff ) << 16 ) + - ( ( data[ offset + 3 ] & 0xff ) << 24 ); - final long high = - ( ( data[ offset + 4 ] & 0xff ) << 0 ) + - ( ( data[ offset + 5 ] & 0xff ) << 8 ) + - ( ( data[ offset + 6 ] & 0xff ) << 16 ) + - ( ( data[ offset + 7 ] & 0xff ) << 24 ); - return (high << 32) + (0xffffffffL & low); + final long low = readSwappedInteger(data, offset); + final long high = readSwappedInteger(data, offset + 4); + return (high << 32) + (0xffffffffL & low); } /** Modified: commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java?rev=1686450&r1=1686449&r2=1686450&view=diff ============================================================================== --- commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java (original) +++ commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java Fri Jun 19 16:43:48 2015 @@ -1065,15 +1065,7 @@ public class FileUtils { */ public static void copyFile(final File srcFile, final File destFile, final boolean preserveFileDate) throws IOException { - if (srcFile == null) { - throw new NullPointerException("Source must not be null"); - } - if (destFile == null) { - throw new NullPointerException("Destination must not be null"); - } - if (srcFile.exists() == false) { - throw new FileNotFoundException("Source '" + srcFile + "' does not exist"); - } + checkFileRequirements(srcFile, destFile); if (srcFile.isDirectory()) { throw new IOException("Source '" + srcFile + "' exists but is a directory"); } @@ -1366,16 +1358,8 @@ public class FileUtils { */ public static void copyDirectory(final File srcDir, final File destDir, final FileFilter filter, final boolean preserveFileDate) throws IOException { - if (srcDir == null) { - throw new NullPointerException("Source must not be null"); - } - if (destDir == null) { - throw new NullPointerException("Destination must not be null"); - } - if (srcDir.exists() == false) { - throw new FileNotFoundException("Source '" + srcDir + "' does not exist"); - } - if (srcDir.isDirectory() == false) { + checkFileRequirements(srcDir, destDir); + if (!srcDir.isDirectory()) { throw new IOException("Source '" + srcDir + "' exists but is not a directory"); } if (srcDir.getCanonicalPath().equals(destDir.getCanonicalPath())) { @@ -1397,6 +1381,18 @@ public class FileUtils { doCopyDirectory(srcDir, destDir, filter, preserveFileDate, exclusionList); } + private static void checkFileRequirements(File srcDir, File destDir) throws FileNotFoundException { + if (srcDir == null) { + throw new NullPointerException("Source must not be null"); + } + if (destDir == null) { + throw new NullPointerException("Destination must not be null"); + } + if (srcDir.exists() == false) { + throw new FileNotFoundException("Source '" + srcDir + "' does not exist"); + } + } + /** * Internal copy directory method. * @@ -1661,6 +1657,23 @@ public class FileUtils { * @throws IllegalArgumentException if {@code directory} does not exist or is not a directory */ public static void cleanDirectory(final File directory) throws IOException { + final File[] files = verifiedListFiles(directory); + + IOException exception = null; + for (final File file : files) { + try { + forceDelete(file); + } catch (final IOException ioe) { + exception = ioe; + } + } + + if (null != exception) { + throw exception; + } + } + + private static File[] verifiedListFiles(File directory) throws IOException { if (!directory.exists()) { final String message = directory + " does not exist"; throw new IllegalArgumentException(message); @@ -1675,19 +1688,7 @@ public class FileUtils { if (files == null) { // null if security restricted throw new IOException("Failed to list contents of " + directory); } - - IOException exception = null; - for (final File file : files) { - try { - forceDelete(file); - } catch (final IOException ioe) { - exception = ioe; - } - } - - if (null != exception) { - throw exception; - } + return files; } //----------------------------------------------------------------------- @@ -2389,20 +2390,7 @@ public class FileUtils { * @throws IOException in case cleaning is unsuccessful */ private static void cleanDirectoryOnExit(final File directory) throws IOException { - if (!directory.exists()) { - final String message = directory + " does not exist"; - throw new IllegalArgumentException(message); - } - - if (!directory.isDirectory()) { - final String message = directory + " is not a directory"; - throw new IllegalArgumentException(message); - } - - final File[] files = directory.listFiles(); - if (files == null) { // null if security restricted - throw new IOException("Failed to list contents of " + directory); - } + final File[] files = verifiedListFiles(directory); IOException exception = null; for (final File file : files) { Modified: commons/proper/io/trunk/src/main/java/org/apache/commons/io/monitor/FileAlterationObserver.java URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/monitor/FileAlterationObserver.java?rev=1686450&r1=1686449&r2=1686450&view=diff ============================================================================== --- commons/proper/io/trunk/src/main/java/org/apache/commons/io/monitor/FileAlterationObserver.java (original) +++ commons/proper/io/trunk/src/main/java/org/apache/commons/io/monitor/FileAlterationObserver.java Fri Jun 19 16:43:48 2015 @@ -273,11 +273,7 @@ public class FileAlterationObserver impl */ public void initialize() throws Exception { rootEntry.refresh(rootEntry.getFile()); - final File[] files = listFiles(rootEntry.getFile()); - final FileEntry[] children = files.length > 0 ? new FileEntry[files.length] : FileEntry.EMPTY_ENTRIES; - for (int i = 0; i < files.length; i++) { - children[i] = createFileEntry(rootEntry, files[i]); - } + final FileEntry[] children = doListFiles(rootEntry.getFile(), rootEntry); rootEntry.setChildren(children); } @@ -358,13 +354,18 @@ public class FileAlterationObserver impl private FileEntry createFileEntry(final FileEntry parent, final File file) { final FileEntry entry = parent.newChildInstance(file); entry.refresh(file); + final FileEntry[] children = doListFiles(file, entry); + entry.setChildren(children); + return entry; + } + + private FileEntry[] doListFiles(File file, FileEntry entry) { final File[] files = listFiles(file); final FileEntry[] children = files.length > 0 ? new FileEntry[files.length] : FileEntry.EMPTY_ENTRIES; for (int i = 0; i < files.length; i++) { children[i] = createFileEntry(entry, files[i]); } - entry.setChildren(children); - return entry; + return children; } /**