diff options
Diffstat (limited to 'luni/src/main/java/java/util/jar/Manifest.java')
-rw-r--r-- | luni/src/main/java/java/util/jar/Manifest.java | 80 |
1 files changed, 15 insertions, 65 deletions
diff --git a/luni/src/main/java/java/util/jar/Manifest.java b/luni/src/main/java/java/util/jar/Manifest.java index b6ebddc..6a3936d 100644 --- a/luni/src/main/java/java/util/jar/Manifest.java +++ b/luni/src/main/java/java/util/jar/Manifest.java @@ -17,11 +17,9 @@ package java.util.jar; -import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.lang.reflect.Field; import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.charset.CharsetEncoder; @@ -43,26 +41,12 @@ public class Manifest implements Cloneable { private static final byte[] VALUE_SEPARATOR = new byte[] { ':', ' ' }; - private static final Field BAIS_BUF = getByteArrayInputStreamField("buf"); - private static final Field BAIS_POS = getByteArrayInputStreamField("pos"); + private final Attributes mainAttributes; + private final HashMap<String, Attributes> entries; - private static Field getByteArrayInputStreamField(String name) { - try { - Field f = ByteArrayInputStream.class.getDeclaredField(name); - f.setAccessible(true); - return f; - } catch (Exception ex) { - throw new AssertionError(ex); - } - } - - private Attributes mainAttributes = new Attributes(); - - private HashMap<String, Attributes> entries = new HashMap<String, Attributes>(); - - static class Chunk { - int start; - int end; + static final class Chunk { + final int start; + final int end; Chunk(int start, int end) { this.start = start; @@ -82,6 +66,8 @@ public class Manifest implements Cloneable { * Creates a new {@code Manifest} instance. */ public Manifest() { + entries = new HashMap<String, Attributes>(); + mainAttributes = new Attributes(); } /** @@ -94,7 +80,8 @@ public class Manifest implements Cloneable { * if an IO error occurs while creating this {@code Manifest} */ public Manifest(InputStream is) throws IOException { - read(is); + this(); + read(Streams.readFully(is)); } /** @@ -111,11 +98,12 @@ public class Manifest implements Cloneable { .getEntries()).clone(); } - Manifest(InputStream is, boolean readChunks) throws IOException { + Manifest(byte[] manifestBytes, boolean readChunks) throws IOException { + this(); if (readChunks) { chunks = new HashMap<String, Chunk>(); } - read(is); + read(manifestBytes); } /** @@ -192,58 +180,20 @@ public class Manifest implements Cloneable { * If an error occurs reading the manifest. */ public void read(InputStream is) throws IOException { - byte[] buf; - if (is instanceof ByteArrayInputStream) { - buf = exposeByteArrayInputStreamBytes((ByteArrayInputStream) is); - } else { - buf = Streams.readFullyNoClose(is); - } + read(Streams.readFullyNoClose(is)); + } + private void read(byte[] buf) throws IOException { if (buf.length == 0) { return; } - // a workaround for HARMONY-5662 - // replace EOF and NUL with another new line - // which does not trigger an error - byte b = buf[buf.length - 1]; - if (b == 0 || b == 26) { - buf[buf.length - 1] = '\n'; - } - ManifestReader im = new ManifestReader(buf, mainAttributes); mainEnd = im.getEndOfMainSection(); im.readEntries(entries, chunks); } /** - * Returns a byte[] containing all the bytes from a ByteArrayInputStream. - * Where possible, this returns the actual array rather than a copy. - */ - private static byte[] exposeByteArrayInputStreamBytes(ByteArrayInputStream bais) { - byte[] buffer; - synchronized (bais) { - byte[] buf; - int pos; - try { - buf = (byte[]) BAIS_BUF.get(bais); - pos = BAIS_POS.getInt(bais); - } catch (IllegalAccessException iae) { - throw new AssertionError(iae); - } - int available = bais.available(); - if (pos == 0 && buf.length == available) { - buffer = buf; - } else { - buffer = new byte[available]; - System.arraycopy(buf, pos, buffer, 0, available); - } - bais.skip(available); - } - return buffer; - } - - /** * Returns the hash code for this instance. * * @return this {@code Manifest}'s hashCode. |