org.springframework.util.FileCopyUtils



Project spring-projects/spring-boot in file ...ngframework.boot.loader.tools.RepackagerTests.java (2014-03-17)
@@ -30,6 +30,7 @@ import org.junit.rules.ExpectedException;
 import org.junit.rules.TemporaryFolder;
 import org.springframework.boot.loader.tools.sample.ClassWithMainMethod;
 import org.springframework.boot.loader.tools.sample.ClassWithoutMainMethod;
+import org.springframework.util.FileCopyUtils;
 
 import static org.hamcrest.Matchers.equalTo;
 import static org.junit.Assert.assertThat;
@@ -256,6 +257,8 @@ public class RepackagerTests {
 		TestJarFile libJar = new TestJarFile(this.temporaryFolder);
 		libJar.addClass("a/b/C.class", ClassWithoutMainMethod.class);
 		final File libJarFile = libJar.getFile();
+		final File libNonJarFile = this.temporaryFolder.newFile();
+		FileCopyUtils.copy(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 }, libNonJarFile);
 		this.testJarFile.addClass("a/b/C.class", ClassWithMainMethod.class);
 		File file = this.testJarFile.getFile();
 		Repackager repackager = new Repackager(file);
@@ -263,9 +266,11 @@ public class RepackagerTests {
 			@Override
 			public void doWithLibraries(LibraryCallback callback) throws IOException {
 				callback.library(libJarFile, LibraryScope.COMPILE);
+				callback.library(libNonJarFile, LibraryScope.COMPILE);
 			}
 		});
 		assertThat(hasEntry(file, "lib/" + libJarFile.getName()), equalTo(true));
+		assertThat(hasEntry(file, "lib/" + libNonJarFile.getName()), equalTo(false));
 	}
 
 	@Test
Project flyway/flyway in file ...ore.dbsupport.oracle.OracleSqlScriptSmallTest.java (2010-07-20)
@@ -19,7 +19,10 @@ package com.googlecode.flyway.core.dbsupport.oracle;
 import com.googlecode.flyway.core.runtime.SqlStatement;
 import org.junit.Test;
 import org.springframework.core.io.ClassPathResource;
+import org.springframework.util.FileCopyUtils;
 
+import java.io.InputStreamReader;
+import java.nio.charset.Charset;
 import java.util.HashMap;
 import java.util.List;
 
@@ -30,9 +33,10 @@ import static org.junit.Assert.assertEquals;
  */
 public class OracleSqlScriptSmallTest {
     @Test
-    public void testSqlStatements() {
-        OracleSqlScript sqlScript =
-                new OracleSqlScript(new ClassPathResource("migration/oracle/sql/V1.sql"), new HashMap<String, String>());
+    public void testSqlStatements() throws Exception {
+        String source = FileCopyUtils.copyToString(new InputStreamReader(new ClassPathResource("migration/oracle/sql/V1.sql").getInputStream(), Charset.forName("UTF-8")));
+
+        OracleSqlScript sqlScript = new OracleSqlScript(source, new HashMap<String, String>());
         List<SqlStatement> sqlStatements = sqlScript.getSqlStatements();
         assertEquals(3, sqlStatements.size());
         assertEquals(18, sqlStatements.get(0).getLineNumber());
Project SpringSource/spring-framework in file ....org.springframework.core.io.AbstractResource.java (2012-02-14)
@@ -25,6 +25,7 @@ import java.net.URISyntaxException;
 import java.net.URL;
 
 import org.springframework.core.NestedIOException;
+import org.springframework.util.FileCopyUtils;
 import org.springframework.util.ResourceUtils;
 
 /**
@@ -108,12 +109,13 @@ public abstract class AbstractResource implements Resource {
 	}
 
 	/**
-	 * This implementation checks the length of the underlying File,
-	 * if available.
-	 * @see #getFile()
+	 * This implementation reads the entire InputStream to calculate the
+	 * content length. Subclasses will almost always be able to provide
+	 * a more optimal version of this, e.g. checking a File length.
+	 * @see #getInputStream()
 	 */
 	public long contentLength() throws IOException {
-		return getFile().length();
+		return FileCopyUtils.copyToByteArray(getInputStream()).length;
 	}
 
 	/**
Project spring-projects/spring-boot in file ...framework.boot.gradle.repackage.RepackageTask.java (2014-06-18)
@@ -27,6 +27,7 @@ import org.gradle.api.tasks.TaskAction;
 import org.gradle.api.tasks.bundling.Jar;
 import org.springframework.boot.gradle.SpringBootPluginExtension;
 import org.springframework.boot.loader.tools.Repackager;
+import org.springframework.util.FileCopyUtils;
 
 /**
  * Repackage task.
@@ -44,6 +45,10 @@ public class RepackageTask extends DefaultTask {
 
 	private String mainClass;
 
+	private String classifier;
+
+	private File outputFile;
+
 	public void setCustomConfiguration(String customConfiguration) {
 		this.customConfiguration = customConfiguration;
 	}
@@ -55,11 +60,19 @@ public class RepackageTask extends DefaultTask {
 	public void setMainClass(String mainClass) {
 		this.mainClass = mainClass;
 	}
-	
+
 	public String getMainClass() {
 		return mainClass;
 	}
 
+	public String getClassifier() {
+		return classifier;
+	}
+
+	public void setClassifier(String classifier) {
+		this.classifier = classifier;
+	}
+
 	@TaskAction
 	public void repackage() {
 		Project project = getProject();
@@ -102,13 +115,24 @@ public class RepackageTask extends DefaultTask {
 				File file = archive.getArchivePath();
 				if (file.exists()) {
 					Repackager repackager = new LoggingRepackager(file);
+					File out = RepackageTask.this.outputFile;
+					if (out != null) {
+						try {
+							FileCopyUtils.copy(file, out);
+						}
+						catch (IOException e) {
+							throw new IllegalStateException(e.getMessage(), e);
+						}
+						file = out;
+					}
+					RepackageTask.this.outputFile = file;
 					setMainClass(repackager);
 					if (this.extension.convertLayout() != null) {
 						repackager.setLayout(this.extension.convertLayout());
 					}
 					repackager.setBackupSource(this.extension.isBackupSource());
 					try {
-						repackager.repackage(this.libraries);
+						repackager.repackage(file, this.libraries);
 					}
 					catch (IOException ex) {
 						throw new IllegalStateException(ex.getMessage(), ex);
@@ -121,10 +145,13 @@ public class RepackageTask extends DefaultTask {
 			String mainClass = (String) getProject().property("mainClassName");
 			if (RepackageTask.this.mainClass != null) {
 				mainClass = RepackageTask.this.mainClass;
-			} else if (this.extension.getMainClass() != null) {
+			}
+			else if (this.extension.getMainClass() != null) {
 				mainClass = this.extension.getMainClass();
-			} else if (getProject().getTasks().getByName("run").hasProperty("main")) {
-				mainClass = (String) getProject().getTasks().getByName("run").property("main");
+			}
+			else if (getProject().getTasks().getByName("run").hasProperty("main")) {
+				mainClass = (String) getProject().getTasks().getByName("run")
+						.property("main");
 			}
 			getLogger().info("Setting mainClass: " + mainClass);
 			repackager.setMainClass(mainClass);
@@ -154,4 +181,9 @@ public class RepackageTask extends DefaultTask {
 			}
 		};
 	}
+
+	void setOutputFile(File file) {
+		this.outputFile = file;
+	}
+
 }
Project spring-projects/spring-framework in file ....org.springframework.core.io.AbstractResource.java (2012-02-14)
@@ -25,6 +25,7 @@ import java.net.URISyntaxException;
 import java.net.URL;
 
 import org.springframework.core.NestedIOException;
+import org.springframework.util.FileCopyUtils;
 import org.springframework.util.ResourceUtils;
 
 /**
@@ -108,12 +109,13 @@ public abstract class AbstractResource implements Resource {
 	}
 
 	/**
-	 * This implementation checks the length of the underlying File,
-	 * if available.
-	 * @see #getFile()
+	 * This implementation reads the entire InputStream to calculate the
+	 * content length. Subclasses will almost always be able to provide
+	 * a more optimal version of this, e.g. checking a File length.
+	 * @see #getInputStream()
 	 */
 	public long contentLength() throws IOException {
-		return getFile().length();
+		return FileCopyUtils.copyToByteArray(getInputStream()).length;
 	}
 
 	/**