Skip to content

Commit

Permalink
refactor: properly close resource in ZipFolder.java (#2623)
Browse files Browse the repository at this point in the history
  • Loading branch information
zielint0 authored and monperrus committed Oct 9, 2018
1 parent 3ff8fd6 commit 798ca3d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 8 deletions.
13 changes: 5 additions & 8 deletions src/main/java/spoon/support/compiler/ZipFolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,19 @@ public List<SpoonFile> 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);
Expand Down
48 changes: 48 additions & 0 deletions src/test/java/spoon/test/jar/JarTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,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<SpoonResource> resources = SpoonResourceHelper.resources("./src/test/resources/reference-test/EnumJar.jar");
assertEquals(1, resources.size());
ZipFolder folder = (ZipFolder) resources.get(0);
List<SpoonFile> 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("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<classpath>\n" +
" <classpathentry kind=\"con\" path=\"org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8\"/>\n" +
" <classpathentry kind=\"src\" path=\"src\"/>\n" +
" <classpathentry kind=\"output\" path=\"bin\"/>\n" +
"</classpath>\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();
Expand Down

0 comments on commit 798ca3d

Please sign in to comment.