diff --git a/src/main/java/spoon/support/compiler/ZipFolder.java b/src/main/java/spoon/support/compiler/ZipFolder.java index e4506cca33f..c165be44210 100644 --- a/src/main/java/spoon/support/compiler/ZipFolder.java +++ b/src/main/java/spoon/support/compiler/ZipFolder.java @@ -74,22 +74,19 @@ public List getFiles() { // Indexing content if (files == null) { files = new ArrayList<>(); - try (ZipInputStream zipInput = new ZipInputStream(new BufferedInputStream(new FileInputStream(file)))) { + final int buffer = 2048; + try (ZipInputStream zipInput = new ZipInputStream(new BufferedInputStream(new FileInputStream(file))); + ByteArrayOutputStream output = new ByteArrayOutputStream(buffer)) { ZipEntry entry; while ((entry = zipInput.getNextEntry()) != null) { // deflate in buffer - final int buffer = 2048; - ByteArrayOutputStream output = new ByteArrayOutputStream(buffer); int count; byte[] data = new byte[buffer]; while ((count = zipInput.read(data, 0, buffer)) != -1) { output.write(data, 0, count); } - output.flush(); - output.close(); - - files.add(new ZipFile(this, entry.getName(), output - .toByteArray())); + files.add(new ZipFile(this, entry.getName(), output.toByteArray())); + output.reset(); } } catch (Exception e) { Launcher.LOGGER.error(e.getMessage(), e); diff --git a/src/test/java/spoon/test/jar/JarTest.java b/src/test/java/spoon/test/jar/JarTest.java index d97a57430e0..d2e816e7e50 100644 --- a/src/test/java/spoon/test/jar/JarTest.java +++ b/src/test/java/spoon/test/jar/JarTest.java @@ -2,20 +2,68 @@ import static org.junit.Assert.assertEquals; +import java.io.ByteArrayOutputStream; import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.UnsupportedEncodingException; import java.util.Arrays; +import java.util.List; import org.junit.Assert; import org.junit.Test; import spoon.Launcher; import spoon.SpoonModelBuilder; +import spoon.compiler.SpoonFile; +import spoon.compiler.SpoonResource; import spoon.compiler.SpoonResourceHelper; import spoon.reflect.factory.Factory; import spoon.support.compiler.VirtualFile; +import spoon.support.compiler.ZipFolder; public class JarTest { + @Test + public void testJarResources() throws Exception { + List resources = SpoonResourceHelper.resources("./src/test/resources/reference-test/EnumJar.jar"); + assertEquals(1, resources.size()); + ZipFolder folder = (ZipFolder) resources.get(0); + List files = folder.getAllFiles(); + assertEquals(5, files.size()); + assertEquals("Manifest-Version: 1.0\r\n\r\n", readFileString(files.stream().filter(f -> f.getName().equals("META-INF/MANIFEST.MF")).findFirst().get(), "ISO-8859-1")); + assertEquals("\n" + + "\n" + + " \n" + + " \n" + + " \n" + + "\n" + + "", readFileString(files.stream().filter(f -> f.getName().equals(".classpath")).findFirst().get(), "ISO-8859-1")); + } + + private byte[] readFileBytes(SpoonFile file) { + byte[] buff = new byte[1024]; + try (InputStream is = file.getContent(); ByteArrayOutputStream baos = new ByteArrayOutputStream()) { + while(true) { + int count = is.read(buff); + if (count < 0) { + break; + } + baos.write(buff, 0, count); + } + return baos.toByteArray(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + private String readFileString(SpoonFile file, String encoding) { + try { + return new String(readFileBytes(file), encoding); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); + } + } + @Test public void testJar() throws Exception { Launcher spoon = new Launcher();