FileDocCategorySizeDatePackage
InitManifest.javaAPI DocAndroid 1.5 API10212Wed May 06 22:41:02 BST 2009java.util.jar

InitManifest

public class InitManifest extends Object

Fields Summary
private final byte[]
inbuf
private int
inbufCount
private int
inbufPos
private byte[]
buffer
private char[]
charbuf
private final ByteArrayOutputStream
out
private String
encoding
private boolean
usingUTF8
private final Map
attributeNames
private final byte[]
mainAttributesChunk
Constructors Summary
InitManifest(InputStream is, Attributes main, Map entries, Map chunks, String verString)


             
                
        encoding = AccessController.doPrivileged(new PriviAction<String>(
                "manifest.read.encoding")); //$NON-NLS-1$
        if ("".equals(encoding)) { //$NON-NLS-1$
            encoding = null;
        }

        Attributes current = main;
        ArrayList<String> list = new ArrayList<String>();

        // Return the chunk of main attributes in the manifest.
        mainAttributesChunk = nextChunk(is, list);

        Iterator<String> it = list.iterator();
        while (it.hasNext()) {
            addAttribute(it.next(), current);
        }

        // Check for version attribute
        if (verString != null && main.getValue(verString) == null) {
            throw new IOException(Messages.getString("archive.2D", verString)); //$NON-NLS-1$
        }

        list.clear();
        byte[] chunk = null;
        while (chunks == null ? readLines(is, list) : (chunk = nextChunk(is,
                list)) != null) {
            it = list.iterator();
            String line = it.next();
            if (line.length() < 7
                    || !Util.toASCIILowerCase(line.substring(0, 5)).equals("name:")) { //$NON-NLS-1$
                throw new IOException(Messages.getString("archive.23")); //$NON-NLS-1$
            }
            // Name: length required space char
            String name = line.substring(6, line.length());
            current = new Attributes(12);
            if (chunks != null) {
                chunks.put(name, chunk);
            }
            entries.put(name, current);
            while (it.hasNext()) {
                addAttribute(it.next(), current);
            }
            list.clear();
        }

    
Methods Summary
private voidaddAttribute(java.lang.String line, java.util.jar.Attributes current)

        String header;
        int hdrIdx = line.indexOf(':");
        if (hdrIdx < 1) {
            throw new IOException(Messages.getString("archive.2F", line)); //$NON-NLS-1$
        }
        header = line.substring(0, hdrIdx);
        Attributes.Name name = attributeNames.get(header);
        if (name == null) {
            try {
                name = new Attributes.Name(header);
            } catch (IllegalArgumentException e) {
                throw new IOException(e.toString());
            }
            attributeNames.put(header, name);
        }
        if (hdrIdx + 1 >= line.length() || line.charAt(hdrIdx + 1) != ' ") {
            throw new IOException(Messages.getString("archive.2F", line)); //$NON-NLS-1$
        }
        // +2 due to required SPACE char
        current.put(name, line.substring(hdrIdx + 2, line.length()));
    
private voidaddLine(int length, java.util.List lines)

        if (encoding != null) {
            lines.add(new String(buffer, 0, length, encoding));
        } else {
            if (usingUTF8) {
                try {
                    if (charbuf.length < length) {
                        charbuf = new char[length];
                    }
                    lines.add(Util.convertUTF8WithBuf(buffer, charbuf, 0,
                            length));
                } catch (UTFDataFormatException e) {
                    usingUTF8 = false;
                }
            }
            if (!usingUTF8) {
                if (charbuf.length < length) {
                    charbuf = new char[length];
                }
                // If invalid UTF8, convert bytes to chars setting the upper
                // bytes to zeros
                int charOffset = 0;
                int offset = 0;
                for (int i = length; --i >= 0;) {
                    charbuf[charOffset++] = (char) (buffer[offset++] & 0xff);
                }
                lines.add(new String(charbuf, 0, length));
            }
        }
    
byte[]getMainAttributesChunk()

        return mainAttributesChunk;
    
private byte[]nextChunk(java.io.InputStream in, java.util.List lines)

        if (inbufCount == -1) {
            return null;
        }
        byte next;
        int pos = 0;
        boolean blankline = false, lastCr = false;
        out.reset();
        while (true) {
            if (inbufPos == inbufCount) {
                if ((inbufCount = in.read(inbuf)) == -1) {
                    if (out.size() == 0) {
                        return null;
                    }
                    if (blankline) {
                        addLine(pos, lines);
                    }
                    return out.toByteArray();
                }
                if (inbufCount == inbuf.length && in.available() == 0) {
                    /* archive.2E = "line too long" */
                    throw new IOException(Messages.getString("archive.2E")); //$NON-NLS-1$
                }
                inbufPos = 0;
            }
            next = inbuf[inbufPos++];
            if (lastCr) {
                if (next != '\n") {
                    inbufPos--;
                    next = '\r";
                } else {
                    if (out.size() == 0) {
                        continue;
                    }
                    out.write('\r");
                }
                lastCr = false;
            } else if (next == '\r") {
                lastCr = true;
                continue;
            }
            if (blankline) {
                if (next == ' ") {
                    out.write(next);
                    blankline = false;
                    continue;
                }
                addLine(pos, lines);
                if (next == '\n") {
                    out.write(next);
                    return out.toByteArray();
                }
                pos = 0;
            } else if (next == '\n") {
                if (out.size() == 0) {
                    continue;
                }
                out.write(next);
                blankline = true;
                continue;
            }
            blankline = false;
            out.write(next);
            if (pos == buffer.length) {
                byte[] newBuf = new byte[buffer.length * 2];
                System.arraycopy(buffer, 0, newBuf, 0, buffer.length);
                buffer = newBuf;
            }
            buffer[pos++] = next;
        }
    
private booleanreadLines(java.io.InputStream in, java.util.List lines)

        if (inbufCount == -1) {
            return false;
        }
        byte next;
        int pos = 0;
        boolean blankline = false, lastCr = false;
        while (true) {
            if (inbufPos == inbufCount) {
                if ((inbufCount = in.read(inbuf)) == -1) {
                    if (blankline) {
                        addLine(pos, lines);
                    }
                    return lines.size() != 0;
                }
                if (inbufCount == inbuf.length && in.available() == 0) {
                    /* archive.2E = "line too long" */
                    throw new IOException(Messages.getString("archive.2E")); //$NON-NLS-1$
                }
                inbufPos = 0;
            }
            next = inbuf[inbufPos++];
            if (lastCr) {
                if (next != '\n") {
                    inbufPos--;
                    next = '\r";
                }
                lastCr = false;
            } else if (next == '\r") {
                lastCr = true;
                continue;
            }
            if (blankline) {
                if (next == ' ") {
                    blankline = false;
                    continue;
                }
                addLine(pos, lines);
                if (next == '\n") {
                    return true;
                }
                pos = 0;
            } else if (next == '\n") {
                if (pos == 0 && lines.size() == 0) {
                    continue;
                }
                blankline = true;
                continue;
            }
            blankline = false;
            if (pos == buffer.length) {
                byte[] newBuf = new byte[buffer.length * 2];
                System.arraycopy(buffer, 0, newBuf, 0, buffer.length);
                buffer = newBuf;
            }
            buffer[pos++] = next;
        }