SLING-8421: add getFileFromURL and getJarFileFromURL method to IOUtils.
diff --git a/src/main/java/org/apache/sling/feature/io/IOUtils.java b/src/main/java/org/apache/sling/feature/io/IOUtils.java
index 1264c3b..fa64815 100644
--- a/src/main/java/org/apache/sling/feature/io/IOUtils.java
+++ b/src/main/java/org/apache/sling/feature/io/IOUtils.java
@@ -185,8 +185,7 @@
String innerURL = url.getPath();
if (innerURL.endsWith("!/") && innerURL.indexOf("!/") == innerURL.lastIndexOf("!/")) {
innerURL = innerURL.substring(0, innerURL.indexOf("!/"));
- try
- {
+ try {
result = getFileFromURL(new URL(innerURL), cache, tmpDir);
} catch (IOException ex) {
result = null;
@@ -225,15 +224,12 @@
* @throws IOException if the url can't be represented as a jarfile
*/
public static JarFile getJarFileFromURL(URL url, boolean cache, File tmpDir) throws IOException {
- try
- {
+ try {
URL targetURL = url;
- if (!url.getProtocol().equals("jar"))
- {
+ if (!url.getProtocol().equals("jar")) {
targetURL = new URL("jar:" + toURLString(url) + "!/");
}
- else if (!url.getPath().endsWith("!/"))
- {
+ else if (!url.getPath().endsWith("!/")) {
targetURL = new URL(toURLString(url) + "!/");
}
return ((JarURLConnection) targetURL.openConnection()).getJarFile();
@@ -249,12 +245,9 @@
}
private static String toURLString(URL url) {
- try
- {
+ try {
return url.toURI().toURL().toString();
- }
- catch (URISyntaxException | MalformedURLException e)
- {
+ } catch (URISyntaxException | MalformedURLException e) {
return url.toString();
}
}
diff --git a/src/test/java/org/apache/sling/feature/io/IOUtilsTest.java b/src/test/java/org/apache/sling/feature/io/IOUtilsTest.java
index 47790a2..4c72e0f 100644
--- a/src/test/java/org/apache/sling/feature/io/IOUtilsTest.java
+++ b/src/test/java/org/apache/sling/feature/io/IOUtilsTest.java
@@ -23,15 +23,12 @@
import static org.junit.Assert.fail;
import java.io.BufferedReader;
-import java.io.BufferedWriter;
-import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
-import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.URL;
@@ -45,7 +42,6 @@
import java.util.jar.JarFile;
import java.util.jar.JarOutputStream;
-import org.apache.sling.feature.io.IOUtils;
import org.junit.Test;
public class IOUtilsTest {
@@ -69,8 +65,7 @@
}
}
- @Test public void testGetFileFromURL() throws IOException
- {
+ @Test public void testGetFileFromURL() throws IOException {
File file = File.createTempFile("IOUtilsTest \\\\+%23öäü^^^°$::", ".test");
try (PrintWriter writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"))) {
@@ -81,15 +76,12 @@
assertEquals(file, IOUtils.getFileFromURL(file.toURI().toURL(), false, null));
- URL url = new URL(null,"bla:" + file.toURI().toURL(), new URLStreamHandler()
- {
+ URL url = new URL(null,"bla:" + file.toURI().toURL(), new URLStreamHandler() {
@Override
- protected URLConnection openConnection(URL u) throws IOException
- {
- return new URLConnection(u)
- {
+ protected URLConnection openConnection(URL u){
+ return new URLConnection(u) {
@Override
- public void connect() throws IOException
+ public void connect()
{
}
@@ -135,8 +127,7 @@
output.write("Hello".getBytes());
output.closeEntry();
output.putNextEntry(new JarEntry("test.jar"));
- try (JarOutputStream inner = new JarOutputStream(output))
- {
+ try (JarOutputStream inner = new JarOutputStream(output)) {
inner.putNextEntry(new JarEntry("inner"));
inner.write("Hello".getBytes());
inner.closeEntry();
@@ -154,16 +145,14 @@
assertNotNull(tmpJar);
assertNotNull(tmpJar.getEntry("inner"));
- try
- {
+ try {
IOUtils.getJarFileFromURL(new URL("jar:" + jarFile.toURI().toURL() + "!/test"), true, null);
fail();
} catch (IOException ex) {
// Expected
}
- try
- {
+ try {
IOUtils.getJarFileFromURL(new URL("jar:" + jarFile.toURI().toURL() + "!/test.jar"), false, null);
fail();
} catch (IOException ex) {