implement jar and file URL stream handlers

This commit is contained in:
Joel Dice
2011-03-25 19:14:21 -06:00
parent 31eb047391
commit 3dd091c67a
13 changed files with 342 additions and 23 deletions

View File

@ -0,0 +1,15 @@
/* Copyright (c) 2011, Avian Contributors
Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice appear
in all copies.
There is NO WARRANTY for this software. See license.txt for
details. */
package java.util.jar;
import java.util.zip.ZipEntry;
public abstract class JarEntry extends ZipEntry { }

View File

@ -0,0 +1,77 @@
/* Copyright (c) 2011, Avian Contributors
Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice appear
in all copies.
There is NO WARRANTY for this software. See license.txt for
details. */
package java.util.jar;
import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipFile;
import java.util.zip.ZipEntry;
public class JarFile extends ZipFile {
public JarFile(String name) throws IOException {
super(name);
}
public JarFile(File file) throws IOException {
super(file);
}
public Enumeration<JarEntry> entries() {
return (Enumeration<JarEntry>) makeEnumeration(JarEntryFactory.Instance);
}
public JarEntry getJarEntry(String name) {
return (JarEntry) getEntry(JarEntryFactory.Instance, name);
}
private static class MyJarEntry extends JarEntry {
public final Window window;
public final int pointer;
public MyJarEntry(Window window, int pointer) {
this.window = window;
this.pointer = pointer;
}
public String getName() {
try {
return entryName(window, pointer);
} catch (IOException e) {
return null;
}
}
public int getCompressedSize() {
try {
return compressedSize(window, pointer);
} catch (IOException e) {
return 0;
}
}
public int getSize() {
try {
return uncompressedSize(window, pointer);
} catch (IOException e) {
return 0;
}
}
}
private static class JarEntryFactory implements EntryFactory {
public static final JarEntryFactory Instance = new JarEntryFactory();
public ZipEntry makeEntry(Window window, int pointer) {
return new MyJarEntry(window, pointer);
}
}
}

View File

@ -13,4 +13,5 @@ package java.util.zip;
public abstract class ZipEntry {
public abstract String getName();
public abstract int getCompressedSize();
public abstract int getSize();
}

View File

@ -63,13 +63,23 @@ public class ZipFile {
return index.size();
}
protected Enumeration<? extends ZipEntry> makeEnumeration
(EntryFactory factory)
{
return new MyEnumeration(factory, window, index.values().iterator());
}
public Enumeration<? extends ZipEntry> entries() {
return new MyEnumeration(window, index.values().iterator());
return makeEnumeration(ZipEntryFactory.Instance);
}
protected ZipEntry getEntry(EntryFactory factory, String name) {
Integer pointer = index.get(name);
return (pointer == null ? null : factory.makeEntry(window, pointer));
}
public ZipEntry getEntry(String name) {
Integer pointer = index.get(name);
return (pointer == null ? null : new MyZipEntry(window, pointer));
return getEntry(ZipEntryFactory.Instance, name);
}
public InputStream getInputStream(ZipEntry entry) throws IOException {
@ -126,7 +136,7 @@ public class ZipFile {
return get2(w, p + 28);
}
private static String entryName(Window w, int p) throws IOException {
protected static String entryName(Window w, int p) throws IOException {
int length = entryNameLength(w, p);
return new String(w.data, w.seek(p + 46, length), length);
}
@ -135,10 +145,14 @@ public class ZipFile {
return get2(w, p + 10);
}
private static int compressedSize(Window w, int p) throws IOException {
protected static int compressedSize(Window w, int p) throws IOException {
return get4(w, p + 20);
}
protected static int uncompressedSize(Window w, int p) throws IOException {
return get4(w, p + 24);
}
private static int fileNameLength(Window w, int p) throws IOException {
return get2(w, p + 28);
}
@ -186,7 +200,7 @@ public class ZipFile {
file.close();
}
private static class Window {
protected static class Window {
private final RandomAccessFile file;
public final byte[] data;
public int start;
@ -255,13 +269,37 @@ public class ZipFile {
return 0;
}
}
public int getSize() {
try {
return uncompressedSize(window, pointer);
} catch (IOException e) {
return 0;
}
}
}
protected interface EntryFactory {
public ZipEntry makeEntry(Window window, int pointer);
}
private static class ZipEntryFactory implements EntryFactory {
public static final ZipEntryFactory Instance = new ZipEntryFactory();
public ZipEntry makeEntry(Window window, int pointer) {
return new MyZipEntry(window, pointer);
}
}
private static class MyEnumeration implements Enumeration<ZipEntry> {
private final EntryFactory factory;
private final Window window;
private final Iterator<Integer> iterator;
public MyEnumeration(Window window, Iterator<Integer> iterator) {
public MyEnumeration(EntryFactory factory, Window window,
Iterator<Integer> iterator)
{
this.factory = factory;
this.window = window;
this.iterator = iterator;
}
@ -271,7 +309,7 @@ public class ZipFile {
}
public ZipEntry nextElement() {
return new MyZipEntry(window, iterator.next());
return factory.makeEntry(window, iterator.next());
}
}