+JarFile.JarEntry

This commit is contained in:
Evgeny Pasynkov
2012-06-18 14:04:08 +02:00
parent ca58ba33e1
commit 4072483a08
5 changed files with 76 additions and 25 deletions
+1 -5
View File
@@ -20,11 +20,7 @@
<entry name="!*.class" />
<entry name="!*.groovy" />
</wildcardResourcePatterns>
<annotationProcessing>
<profile default="true" name="Default" enabled="false">
<processorPath useClasspath="true" />
</profile>
</annotationProcessing>
<annotationProcessing enabled="false" useClasspath="true" />
</component>
<component name="EclipseCompilerSettings">
<option name="GENERATE_NO_WARNINGS" value="true" />
@@ -80,7 +80,7 @@ public class JarVersionDetectionUtil {
return null;
}
try {
final ZipEntry zipEntry = zipFile.getEntry(JarFile.MANIFEST_NAME);
final com.intellij.openapi.vfs.JarFile.JarEntry zipEntry = zipFile.getEntry(JarFile.MANIFEST_NAME);
if (zipEntry == null) {
return null;
}
@@ -20,16 +20,22 @@ import org.jetbrains.annotations.Nullable;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
/**
* Encapsulates operations with .jar file
*/
public interface JarFile {
ZipEntry getEntry(String name);
InputStream getInputStream(ZipEntry entry) throws IOException;
Enumeration<? extends ZipEntry> entries();
interface JarEntry {
String getName();
long getSize();
long getTime();
boolean isDirectory();
}
JarEntry getEntry(String name);
InputStream getInputStream(JarEntry entry) throws IOException;
Enumeration<? extends JarEntry> entries();
@Nullable ZipFile getZipFile();
}
@@ -69,9 +69,9 @@ public class JarHandlerBase {
map = new THashMap<String, EntryInfo>();
if (zip != null) {
map.put("", new EntryInfo("", null, true));
final Enumeration<? extends ZipEntry> entries = zip.entries();
final Enumeration<? extends JarFile.JarEntry> entries = zip.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
JarFile.JarEntry entry = entries.nextElement();
final String name = entry.getName();
final boolean isDirectory = name.endsWith("/");
getOrCreate(isDirectory ? name.substring(0, name.length() - 1) : name, isDirectory, map);
@@ -104,20 +104,70 @@ public class JarHandlerBase {
protected JarFile createJarFile() {
try {
final ZipFile zipFile = new ZipFile(getMirrorFile(getOriginalFile()));
class MyJarEntry implements JarFile.JarEntry {
private ZipEntry myEntry;
MyJarEntry(ZipEntry entry) {
myEntry = entry;
}
public ZipEntry getEntry() {
return myEntry;
}
@Override
public String getName() {
return myEntry.getName();
}
@Override
public long getSize() {
return myEntry.getSize();
}
@Override
public long getTime() {
return myEntry.getTime();
}
@Override
public boolean isDirectory() {
return myEntry.isDirectory();
}
}
return new JarFile() {
@Override
public ZipEntry getEntry(String name) {
return zipFile.getEntry(name);
public JarFile.JarEntry getEntry(String name) {
ZipEntry entry = zipFile.getEntry(name);
if (entry == null)
return null;
return new MyJarEntry(entry);
}
@Override
public InputStream getInputStream(ZipEntry entry) throws IOException {
return zipFile.getInputStream(entry);
public InputStream getInputStream(JarFile.JarEntry entry) throws IOException {
return zipFile.getInputStream(((MyJarEntry)entry).myEntry);
}
@Override
public Enumeration<? extends ZipEntry> entries() {
return zipFile.entries();
public Enumeration<? extends JarFile.JarEntry> entries() {
final Enumeration<? extends ZipEntry> entries = zipFile.entries();
return new Enumeration<JarEntry>() {
@Override
public boolean hasMoreElements() {
return entries.hasMoreElements();
}
@Override
public JarEntry nextElement() {
ZipEntry entry = entries.nextElement();
if (entry == null)
return null;
return new MyJarEntry(entry);
}
};
}
@Override
@@ -187,7 +237,7 @@ public class JarHandlerBase {
}
@Nullable
private ZipEntry convertToEntry(VirtualFile file) {
private JarFile.JarEntry convertToEntry(VirtualFile file) {
String path = getRelativePath(file);
final JarFile jar = getJar();
return jar != null ? jar.getEntry(path) : null;
@@ -195,7 +245,7 @@ public class JarHandlerBase {
public long getLength(@NotNull final VirtualFile file) {
synchronized (lock) {
final ZipEntry entry = convertToEntry(file);
final JarFile.JarEntry entry = convertToEntry(file);
return entry != null ? entry.getSize() : 0;
}
}
@@ -208,7 +258,7 @@ public class JarHandlerBase {
@NotNull
public byte[] contentsToByteArray(@NotNull final VirtualFile file) throws IOException {
synchronized (lock) {
final ZipEntry entry = convertToEntry(file);
final JarFile.JarEntry entry = convertToEntry(file);
if (entry == null) {
return new byte[0];
}
@@ -231,7 +281,7 @@ public class JarHandlerBase {
public long getTimeStamp(@NotNull final VirtualFile file) {
if (file.getParent() == null) return getOriginalFile().lastModified(); // Optimization
synchronized (lock) {
final ZipEntry entry = convertToEntry(file);
final JarFile.JarEntry entry = convertToEntry(file);
return entry != null ? entry.getTime() : -1L;
}
}
@@ -257,7 +307,7 @@ public class JarHandlerBase {
@Nullable
public FileAttributes getAttributes(@NotNull final VirtualFile file) {
synchronized (lock) {
final ZipEntry entry = convertToEntry(file);
final JarFile.JarEntry entry = convertToEntry(file);
return entry != null ? new FileAttributes(entry.isDirectory(), false, false, entry.getSize(), entry.getTime(), false) : null;
}
}
@@ -44,7 +44,6 @@ import java.util.List;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.util.zip.ZipEntry;
public class JdkUtil {
private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.projectRoots.JdkUtil");
@@ -96,7 +95,7 @@ public class JdkUtil {
return null;
}
try {
ZipEntry entry = manifestJarFile.getEntry(JarFile.MANIFEST_NAME);
com.intellij.openapi.vfs.JarFile.JarEntry entry = manifestJarFile.getEntry(JarFile.MANIFEST_NAME);
if (entry == null) {
return null;
}