diff options
author | Olivier Lamy <olamy@apache.org> | 2014-04-16 18:11:38 +1000 |
---|---|---|
committer | Olivier Lamy <olamy@apache.org> | 2014-04-16 18:11:38 +1000 |
commit | ec1e655c9d996f20bd231b21505a6df865d8a1f7 (patch) | |
tree | 83a6e56ba6b0e2a0f9e0bd97c2ddd1b05b5d90aa /archiva-modules/archiva-base | |
parent | 767e00c182085b193c9f2481a815e90c5c054095 (diff) | |
download | archiva-ec1e655c9d996f20bd231b21505a6df865d8a1f7.tar.gz archiva-ec1e655c9d996f20bd231b21505a6df865d8a1f7.zip |
use Files.copy from java.nio.file
Diffstat (limited to 'archiva-modules/archiva-base')
4 files changed, 36 insertions, 52 deletions
diff --git a/archiva-modules/archiva-base/archiva-filelock/src/test/java/org/apache/archiva/common/filelock/DefaultFileLockManagerTest.java b/archiva-modules/archiva-base/archiva-filelock/src/test/java/org/apache/archiva/common/filelock/DefaultFileLockManagerTest.java index 40c47442f..e72704e35 100644 --- a/archiva-modules/archiva-base/archiva-filelock/src/test/java/org/apache/archiva/common/filelock/DefaultFileLockManagerTest.java +++ b/archiva-modules/archiva-base/archiva-filelock/src/test/java/org/apache/archiva/common/filelock/DefaultFileLockManagerTest.java @@ -21,8 +21,6 @@ package org.apache.archiva.common.filelock; import edu.umd.cs.mtc.MultithreadedTestCase; import edu.umd.cs.mtc.TestFramework; -import org.apache.commons.io.FileUtils; -import org.apache.commons.io.IOUtils; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -35,11 +33,14 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import javax.inject.Inject; import javax.inject.Named; import java.io.File; -import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.concurrent.atomic.AtomicInteger; +//import org.apache.commons.io.IOUtils; + /** * @author Olivier Lamy */ @@ -89,7 +90,7 @@ public class DefaultFileLockManagerTest try { lock.getFile().delete(); - FileUtils.copyFile( largeJar, lock.getFile() ); + Files.copy( largeJar.toPath(), lock.getFile().toPath() ); } finally { @@ -107,7 +108,7 @@ public class DefaultFileLockManagerTest try { lock.getFile().delete(); - FileUtils.copyFile( largeJar, lock.getFile() ); + Files.copy( largeJar.toPath(), lock.getFile().toPath() ); } finally { @@ -124,8 +125,8 @@ public class DefaultFileLockManagerTest Lock lock = fileLockManager.readFileLock( this.file ); try { - IOUtils.copy( new FileInputStream( lock.getFile() ), - new FileOutputStream( File.createTempFile( "foo", ".jar" ) ) ); + Files.copy( Paths.get( lock.getFile().getPath() ), + new FileOutputStream( File.createTempFile( "foo", ".jar" ) ) ); } finally { @@ -143,7 +144,7 @@ public class DefaultFileLockManagerTest try { lock.getFile().delete(); - FileUtils.copyFile( largeJar, lock.getFile() ); + Files.copy( largeJar.toPath(), lock.getFile().toPath() ); } finally { @@ -161,7 +162,7 @@ public class DefaultFileLockManagerTest try { lock.getFile().delete(); - FileUtils.copyFile( largeJar, lock.getFile() ); + Files.copy( largeJar.toPath(), lock.getFile().toPath() ); } finally { @@ -178,8 +179,8 @@ public class DefaultFileLockManagerTest Lock lock = fileLockManager.readFileLock( this.file ); try { - IOUtils.copy( new FileInputStream( lock.getFile() ), - new FileOutputStream( File.createTempFile( "foo", ".jar" ) ) ); + Files.copy( lock.getFile().toPath(), + new FileOutputStream( File.createTempFile( "foo", ".jar" ) ) ); } finally { @@ -197,7 +198,7 @@ public class DefaultFileLockManagerTest try { lock.getFile().delete(); - FileUtils.copyFile( largeJar, lock.getFile() ); + Files.copy( largeJar.toPath(), lock.getFile().toPath() ); } finally { @@ -214,8 +215,7 @@ public class DefaultFileLockManagerTest Lock lock = fileLockManager.readFileLock( this.file ); try { - IOUtils.copy( new FileInputStream( lock.getFile() ), - new FileOutputStream( File.createTempFile( "foo", ".jar" ) ) ); + Files.copy( lock.getFile().toPath(), new FileOutputStream( File.createTempFile( "foo", ".jar" ) ) ); } finally { @@ -233,7 +233,7 @@ public class DefaultFileLockManagerTest try { lock.getFile().delete(); - FileUtils.copyFile( largeJar, lock.getFile() ); + Files.copy( largeJar.toPath(), lock.getFile().toPath() ); } finally { @@ -250,8 +250,8 @@ public class DefaultFileLockManagerTest Lock lock = fileLockManager.readFileLock( this.file ); try { - IOUtils.copy( new FileInputStream( lock.getFile() ), - new FileOutputStream( File.createTempFile( "foo", ".jar" ) ) ); + Files.copy( lock.getFile().toPath(), + new FileOutputStream( File.createTempFile( "foo", ".jar" ) ) ); } finally { diff --git a/archiva-modules/archiva-base/archiva-filelock/src/test/java/org/apache/archiva/common/filelock/DefaultFileLockManagerTimeoutTest.java b/archiva-modules/archiva-base/archiva-filelock/src/test/java/org/apache/archiva/common/filelock/DefaultFileLockManagerTimeoutTest.java index acf7fe66e..281d13105 100644 --- a/archiva-modules/archiva-base/archiva-filelock/src/test/java/org/apache/archiva/common/filelock/DefaultFileLockManagerTimeoutTest.java +++ b/archiva-modules/archiva-base/archiva-filelock/src/test/java/org/apache/archiva/common/filelock/DefaultFileLockManagerTimeoutTest.java @@ -19,11 +19,6 @@ package org.apache.archiva.common.filelock; * under the License. */ -import edu.umd.cs.mtc.MultithreadedTestCase; -import edu.umd.cs.mtc.TestFramework; -import org.apache.commons.io.FileUtils; -import org.apache.commons.io.IOUtils; -import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -35,10 +30,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import javax.inject.Inject; import javax.inject.Named; import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.util.concurrent.atomic.AtomicInteger; +import java.nio.file.Files; +import java.nio.file.StandardCopyOption; /** * @author Olivier Lamy @@ -64,18 +57,20 @@ public class DefaultFileLockManagerTimeoutTest fileLockManager.clearLockFiles(); } - @Test( expected = FileLockTimeoutException.class ) + @Test(expected = FileLockTimeoutException.class) public void testTimeout() throws Throwable { File file = new File( System.getProperty( "buildDirectory" ), "foo.txt" ); + Files.deleteIfExists( file.toPath() ); + File largeJar = new File( System.getProperty( "basedir" ), "src/test/cassandra-all-2.0.3.jar" ); Lock lock = fileLockManager.writeFileLock( file ); - FileUtils.copyFile( largeJar, lock.getFile() ); + Files.copy( largeJar.toPath(), lock.getFile().toPath(), StandardCopyOption.REPLACE_EXISTING ); lock = fileLockManager.writeFileLock( file ); diff --git a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/metadata/RepositoryMetadataWriter.java b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/metadata/RepositoryMetadataWriter.java index ab47e78c1..0eb06b225 100644 --- a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/metadata/RepositoryMetadataWriter.java +++ b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/metadata/RepositoryMetadataWriter.java @@ -25,7 +25,6 @@ import org.apache.archiva.xml.XMLException; import org.apache.archiva.xml.XMLWriter; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.io.FileUtils; -import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; import org.dom4j.Document; import org.dom4j.DocumentHelper; @@ -35,6 +34,7 @@ import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.Writer; +import java.nio.file.Files; import java.util.Collections; import java.util.Comparator; import java.util.Iterator; @@ -49,10 +49,8 @@ public class RepositoryMetadataWriter throws RepositoryMetadataException { boolean thrown = false; - FileWriter writer = null; - try + try (FileWriter writer = new FileWriter( outputFile )) { - writer = new FileWriter( outputFile ); write( metadata, writer ); writer.flush(); } @@ -64,7 +62,6 @@ public class RepositoryMetadataWriter } finally { - IOUtils.closeQuietly( writer ); if ( thrown ) { FileUtils.deleteQuietly( outputFile ); diff --git a/archiva-modules/archiva-base/archiva-xml-tools/src/main/java/org/apache/archiva/xml/XMLReader.java b/archiva-modules/archiva-base/archiva-xml-tools/src/main/java/org/apache/archiva/xml/XMLReader.java index 43cb09c4b..603fc884e 100644 --- a/archiva-modules/archiva-base/archiva-xml-tools/src/main/java/org/apache/archiva/xml/XMLReader.java +++ b/archiva-modules/archiva-base/archiva-xml-tools/src/main/java/org/apache/archiva/xml/XMLReader.java @@ -19,7 +19,6 @@ package org.apache.archiva.xml; * under the License. */ -import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; import org.dom4j.Attribute; import org.dom4j.Document; @@ -44,9 +43,7 @@ import java.util.List; import java.util.Map; /** - * XMLReader - a set of common xml utility methods for reading content out of an xml file. - * - * + * XMLReader - a set of common xml utility methods for reading content out of an xml file. */ public class XMLReader { @@ -98,12 +95,10 @@ public class XMLReader this.documentType = type; this.xmlUrl = url; - InputStream in = null; SAXReader reader = new SAXReader(); - - try + + try (InputStream in = url.openStream()) { - in = url.openStream(); InputStreamReader inReader = new InputStreamReader( in, "UTF-8" ); LatinEntityResolutionReader latinReader = new LatinEntityResolutionReader( inReader ); this.document = reader.read( latinReader ); @@ -116,10 +111,6 @@ public class XMLReader { throw new XMLException( "Unable to open stream to " + url + ": " + e.getMessage(), e ); } - finally - { - IOUtils.closeQuietly( in ); - } Element root = this.document.getRootElement(); if ( root == null ) @@ -129,8 +120,9 @@ public class XMLReader if ( !StringUtils.equals( root.getName(), documentType ) ) { - throw new XMLException( "Invalid " + documentType + " xml: Unexpected root element <" + root.getName() - + ">, expected <" + documentType + ">" ); + throw new XMLException( + "Invalid " + documentType + " xml: Unexpected root element <" + root.getName() + ">, expected <" + + documentType + ">" ); } } @@ -164,7 +156,7 @@ public class XMLReader { // Unknown evaluated type. throw new XMLException( ".getElement( Expr: " + xpathExpr + " ) resulted in non-Element type -> (" - + evaluated.getClass().getName() + ") " + evaluated ); + + evaluated.getClass().getName() + ") " + evaluated ); } } @@ -246,8 +238,8 @@ public class XMLReader else { // Unknown evaluated type. - throw new XMLException( ".getElementText( Node, Expr: " + xpathExpr - + " ) resulted in non-Element type -> (" + evaluated.getClass().getName() + ") " + evaluated ); + throw new XMLException( ".getElementText( Node, Expr: " + xpathExpr + " ) resulted in non-Element type -> (" + + evaluated.getClass().getName() + ") " + evaluated ); } } @@ -271,7 +263,7 @@ public class XMLReader { // Unknown evaluated type. throw new XMLException( ".getElementText( Expr: " + xpathExpr + " ) resulted in non-Element type -> (" - + evaluated.getClass().getName() + ") " + evaluated ); + + evaluated.getClass().getName() + ") " + evaluated ); } } @@ -306,7 +298,7 @@ public class XMLReader { // Unknown evaluated type. throw new XMLException( ".getElementList( Expr: " + xpathExpr + " ) resulted in non-List type -> (" - + evaluated.getClass().getName() + ") " + evaluated ); + + evaluated.getClass().getName() + ") " + evaluated ); } } |