jdk/src/share/classes/java/util/zip/ZipOutputStream.java
author sherman
Thu, 02 Apr 2009 15:35:46 -0700
changeset 2438 21c111b51aa8
parent 1228 1515928f48cd
child 2592 ef26f663a2ba
permissions -rw-r--r--
4681995: Add support for large (> 4GB) zip/jar files Summary: The ZIP64 format support is added for > 4GB jar/zip files Reviewed-by: alanb, martin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
     2
 * Copyright 1996-2009 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.util.zip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.HashSet;
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
    32
import static java.util.zip.ZipConstants64.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * This class implements an output stream filter for writing files in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * ZIP file format. Includes support for both compressed and uncompressed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @author      David Connelly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
class ZipOutputStream extends DeflaterOutputStream implements ZipConstants {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private static class XEntry {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        public final ZipEntry entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        public final long offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        public final int flag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        public XEntry(ZipEntry entry, long offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            this.entry = entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            this.offset = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            this.flag = (entry.method == DEFLATED &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                         (entry.size  == -1 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
                          entry.csize == -1 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                          entry.crc   == -1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                // store size, compressed size, and crc-32 in data descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                // immediately following the compressed entry data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                ? 8
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                // store size, compressed size, and crc-32 in LOC header
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private XEntry current;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private Vector<XEntry> xentries = new Vector<XEntry>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private HashSet<String> names = new HashSet<String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private CRC32 crc = new CRC32();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private long written = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private long locoff = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private String comment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private int method = DEFLATED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private boolean finished;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private boolean closed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private static int version(ZipEntry e) throws ZipException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        switch (e.method) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        case DEFLATED: return 20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        case STORED:   return 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        default: throw new ZipException("unsupported compression method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * Checks to make sure that this stream has not been closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private void ensureOpen() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        if (closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            throw new IOException("Stream closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * Compression method for uncompressed (STORED) entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public static final int STORED = ZipEntry.STORED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * Compression method for compressed (DEFLATED) entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public static final int DEFLATED = ZipEntry.DEFLATED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * Creates a new ZIP output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @param out the actual output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public ZipOutputStream(OutputStream out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        super(out, new Deflater(Deflater.DEFAULT_COMPRESSION, true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        usesDefaultDeflater = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * Sets the ZIP file comment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @param comment the comment string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @exception IllegalArgumentException if the length of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *            ZIP file comment is greater than 0xFFFF bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public void setComment(String comment) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        if (comment != null && comment.length() > 0xffff/3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                                           && getUTF8Length(comment) > 0xffff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            throw new IllegalArgumentException("ZIP file comment too long.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        this.comment = comment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * Sets the default compression method for subsequent entries. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * default will be used whenever the compression method is not specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * for an individual ZIP file entry, and is initially set to DEFLATED.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @param method the default compression method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @exception IllegalArgumentException if the specified compression method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *            is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public void setMethod(int method) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        if (method != DEFLATED && method != STORED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            throw new IllegalArgumentException("invalid compression method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        this.method = method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * Sets the compression level for subsequent entries which are DEFLATED.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * The default setting is DEFAULT_COMPRESSION.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @param level the compression level (0-9)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @exception IllegalArgumentException if the compression level is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    public void setLevel(int level) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        def.setLevel(level);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * Begins writing a new ZIP file entry and positions the stream to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * start of the entry data. Closes the current entry if still active.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * The default compression method will be used if no compression method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * was specified for the entry, and the current time will be used if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * the entry has no set modification time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @param e the ZIP entry to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @exception ZipException if a ZIP format error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @exception IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public void putNextEntry(ZipEntry e) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        if (current != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            closeEntry();       // close previous entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        if (e.time == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            e.setTime(System.currentTimeMillis());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        if (e.method == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            e.method = method;  // use default method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        switch (e.method) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        case DEFLATED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        case STORED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            // compressed size, uncompressed size, and crc-32 must all be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            // set for entries using STORED compression method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            if (e.size == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                e.size = e.csize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            } else if (e.csize == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                e.csize = e.size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            } else if (e.size != e.csize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                throw new ZipException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                    "STORED entry where compressed != uncompressed size");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            if (e.size == -1 || e.crc == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                throw new ZipException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                    "STORED entry missing size, compressed size, or crc-32");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            throw new ZipException("unsupported compression method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        if (! names.add(e.name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            throw new ZipException("duplicate entry: " + e.name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        current = new XEntry(e, written);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        xentries.add(current);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        writeLOC(current);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * Closes the current ZIP entry and positions the stream for writing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * the next entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * @exception ZipException if a ZIP format error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * @exception IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    public void closeEntry() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        if (current != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            ZipEntry e = current.entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            switch (e.method) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            case DEFLATED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                def.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                while (!def.finished()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                    deflate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                if ((current.flag & 8) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                    // verify size, compressed size, and crc-32 settings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                    if (e.size != def.getBytesRead()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                        throw new ZipException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                            "invalid entry size (expected " + e.size +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                            " but got " + def.getBytesRead() + " bytes)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                    if (e.csize != def.getBytesWritten()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                        throw new ZipException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                            "invalid entry compressed size (expected " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                            e.csize + " but got " + def.getBytesWritten() + " bytes)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                    if (e.crc != crc.getValue()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                        throw new ZipException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                            "invalid entry CRC-32 (expected 0x" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                            Long.toHexString(e.crc) + " but got 0x" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                            Long.toHexString(crc.getValue()) + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                    e.size  = def.getBytesRead();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                    e.csize = def.getBytesWritten();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                    e.crc = crc.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                    writeEXT(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                def.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                written += e.csize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            case STORED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                // we already know that both e.size and e.csize are the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                if (e.size != written - locoff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                    throw new ZipException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                        "invalid entry size (expected " + e.size +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                        " but got " + (written - locoff) + " bytes)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                if (e.crc != crc.getValue()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                    throw new ZipException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                         "invalid entry crc-32 (expected 0x" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                         Long.toHexString(e.crc) + " but got 0x" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                         Long.toHexString(crc.getValue()) + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                throw new ZipException("invalid compression method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            crc.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            current = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * Writes an array of bytes to the current ZIP entry data. This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * will block until all the bytes are written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * @param b the data to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * @param off the start offset in the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * @param len the number of bytes that are written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * @exception ZipException if a ZIP file error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @exception IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    public synchronized void write(byte[] b, int off, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        if (off < 0 || len < 0 || off > b.length - len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        } else if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        if (current == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            throw new ZipException("no current ZIP entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        ZipEntry entry = current.entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        switch (entry.method) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        case DEFLATED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            super.write(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        case STORED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            written += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            if (written - locoff > entry.size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                throw new ZipException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                    "attempt to write past end of STORED entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            out.write(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            throw new ZipException("invalid compression method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        crc.update(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * Finishes writing the contents of the ZIP output stream without closing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * the underlying stream. Use this method when applying multiple filters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * in succession to the same output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * @exception ZipException if a ZIP file error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * @exception IOException if an I/O exception has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    public void finish() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        if (finished) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        if (current != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            closeEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        // write central directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        long off = written;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        for (XEntry xentry : xentries)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            writeCEN(xentry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        writeEND(off, written - off);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        finished = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * Closes the ZIP output stream as well as the stream being filtered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * @exception ZipException if a ZIP file error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * @exception IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        if (!closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            super.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            closed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * Writes local file (LOC) header for specified entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    private void writeLOC(XEntry xentry) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        ZipEntry e = xentry.entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        int flag = xentry.flag;
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   347
        int elen = (e.extra != null) ? e.extra.length : 0;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   348
        boolean hasZip64 = false;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   349
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        writeInt(LOCSIG);           // LOC header signature
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   351
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        if ((flag & 8) == 8) {
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   353
            writeShort(version(e));     // version needed to extract
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   354
            writeShort(flag);           // general purpose bit flag
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   355
            writeShort(e.method);       // compression method
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   356
            writeInt(e.time);           // last modification time
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   357
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            // store size, uncompressed size, and crc-32 in data descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            // immediately following compressed entry data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            writeInt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            writeInt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            writeInt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        } else {
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   364
            if (e.csize >= ZIP64_MAGICVAL || e.size >= ZIP64_MAGICVAL) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   365
                hasZip64 = true;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   366
                writeShort(45);         // ver 4.5 for zip64
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   367
            } else {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   368
                writeShort(version(e)); // version needed to extract
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   369
            }
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   370
            writeShort(flag);           // general purpose bit flag
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   371
            writeShort(e.method);       // compression method
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   372
            writeInt(e.time);           // last modification time
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   373
            writeInt(e.crc);            // crc-32
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   374
            if (hasZip64) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   375
                writeInt(ZIP64_MAGICVAL);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   376
                writeInt(ZIP64_MAGICVAL);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   377
                elen += 20;        //headid(2) + size(2) + size(8) + csize(8)
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   378
            } else {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   379
                writeInt(e.csize);  // compressed size
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   380
                writeInt(e.size);   // uncompressed size
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   381
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        byte[] nameBytes = getUTF8Bytes(e.name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        writeShort(nameBytes.length);
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   385
        writeShort(elen);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        writeBytes(nameBytes, 0, nameBytes.length);
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   387
        if (hasZip64) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   388
            writeShort(ZIP64_EXTID);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   389
            writeShort(16);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   390
            writeLong(e.size);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   391
            writeLong(e.csize);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   392
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        if (e.extra != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            writeBytes(e.extra, 0, e.extra.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        locoff = written;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * Writes extra data descriptor (EXT) for specified entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    private void writeEXT(ZipEntry e) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        writeInt(EXTSIG);           // EXT header signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        writeInt(e.crc);            // crc-32
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   405
        if (e.csize >= ZIP64_MAGICVAL || e.size >= ZIP64_MAGICVAL) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   406
            writeLong(e.csize);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   407
            writeLong(e.size);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   408
        } else {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   409
            writeInt(e.csize);          // compressed size
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   410
            writeInt(e.size);           // uncompressed size
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   411
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * Write central directory (CEN) header for specified entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * REMIND: add support for file attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    private void writeCEN(XEntry xentry) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        ZipEntry e  = xentry.entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        int flag = xentry.flag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        int version = version(e);
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   422
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   423
        long csize = e.csize;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   424
        long size = e.size;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   425
        long offset = xentry.offset;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   426
        int e64len = 0;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   427
        boolean hasZip64 = false;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   428
        if (e.csize >= ZIP64_MAGICVAL) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   429
            csize = ZIP64_MAGICVAL;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   430
            e64len += 8;              // csize(8)
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   431
            hasZip64 = true;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   432
        }
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   433
        if (e.size >= ZIP64_MAGICVAL) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   434
            size = ZIP64_MAGICVAL;    // size(8)
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   435
            e64len += 8;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   436
            hasZip64 = true;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   437
        }
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   438
        if (xentry.offset >= ZIP64_MAGICVAL) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   439
            offset = ZIP64_MAGICVAL;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   440
            e64len += 8;              // offset(8)
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   441
            hasZip64 = true;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   442
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        writeInt(CENSIG);           // CEN header signature
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   444
        if (hasZip64) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   445
            writeShort(45);         // ver 4.5 for zip64
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   446
            writeShort(45);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   447
        } else {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   448
            writeShort(version);    // version made by
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   449
            writeShort(version);    // version needed to extract
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   450
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        writeShort(flag);           // general purpose bit flag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        writeShort(e.method);       // compression method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        writeInt(e.time);           // last modification time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        writeInt(e.crc);            // crc-32
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   455
        writeInt(csize);            // compressed size
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   456
        writeInt(size);             // uncompressed size
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        byte[] nameBytes = getUTF8Bytes(e.name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        writeShort(nameBytes.length);
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   459
        if (hasZip64) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   460
            // + headid(2) + datasize(2)
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   461
            writeShort(e64len + 4 + (e.extra != null ? e.extra.length : 0));
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   462
        } else {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   463
            writeShort(e.extra != null ? e.extra.length : 0);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   464
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        byte[] commentBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        if (e.comment != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            commentBytes = getUTF8Bytes(e.comment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            writeShort(commentBytes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            commentBytes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            writeShort(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        writeShort(0);              // starting disk number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        writeShort(0);              // internal file attributes (unused)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        writeInt(0);                // external file attributes (unused)
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   476
        writeInt(offset);           // relative offset of local header
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        writeBytes(nameBytes, 0, nameBytes.length);
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   478
        if (hasZip64) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   479
            writeShort(ZIP64_EXTID);// Zip64 extra
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   480
            writeShort(e64len);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   481
            if (size == ZIP64_MAGICVAL)
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   482
                writeLong(e.size);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   483
            if (csize == ZIP64_MAGICVAL)
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   484
                writeLong(e.csize);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   485
            if (offset == ZIP64_MAGICVAL)
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   486
                writeLong(xentry.offset);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   487
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        if (e.extra != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            writeBytes(e.extra, 0, e.extra.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        if (commentBytes != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            writeBytes(commentBytes, 0, commentBytes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * Writes end of central directory (END) header.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    private void writeEND(long off, long len) throws IOException {
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   500
        boolean hasZip64 = false;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   501
        long xlen = len;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   502
        long xoff = off;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   503
        if (xlen >= ZIP64_MAGICVAL) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   504
            xlen = ZIP64_MAGICVAL;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   505
            hasZip64 = true;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   506
        }
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   507
        if (xoff >= ZIP64_MAGICVAL) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   508
            xoff = ZIP64_MAGICVAL;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   509
            hasZip64 = true;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   510
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        int count = xentries.size();
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   512
        if (count >= ZIP64_MAGICCOUNT) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   513
            count = ZIP64_MAGICCOUNT;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   514
            hasZip64 = true;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   515
        }
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   516
        if (hasZip64) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   517
            long off64 = written;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   518
            //zip64 end of central directory record
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   519
            writeInt(ZIP64_ENDSIG);        // zip64 END record signature
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   520
            writeLong(ZIP64_ENDHDR - 12);  // size of zip64 end
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   521
            writeShort(45);                // version made by
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   522
            writeShort(45);                // version needed to extract
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   523
            writeInt(0);                   // number of this disk
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   524
            writeInt(0);                   // central directory start disk
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   525
            writeLong(xentries.size());    // number of directory entires on disk
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   526
            writeLong(xentries.size());    // number of directory entires
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   527
            writeLong(len);                // length of central directory
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   528
            writeLong(off);                // offset of central directory
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   529
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   530
            //zip64 end of central directory locator
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   531
            writeInt(ZIP64_LOCSIG);        // zip64 END locator signature
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   532
            writeInt(0);                   // zip64 END start disk
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   533
            writeLong(off64);              // offset of zip64 END
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   534
            writeInt(1);                   // total number of disks (?)
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   535
        }
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   536
        writeInt(ENDSIG);                 // END record signature
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   537
        writeShort(0);                    // number of this disk
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   538
        writeShort(0);                    // central directory start disk
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   539
        writeShort(count);                // number of directory entries on disk
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   540
        writeShort(count);                // total number of directory entries
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   541
        writeInt(xlen);                   // length of central directory
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   542
        writeInt(xoff);                   // offset of central directory
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   543
        if (comment != null) {            // zip file comment
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            byte[] b = getUTF8Bytes(comment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            writeShort(b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            writeBytes(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            writeShort(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * Writes a 16-bit short to the output stream in little-endian byte order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    private void writeShort(int v) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        OutputStream out = this.out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        out.write((v >>> 0) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        out.write((v >>> 8) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        written += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * Writes a 32-bit int to the output stream in little-endian byte order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    private void writeInt(long v) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        OutputStream out = this.out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        out.write((int)((v >>>  0) & 0xff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        out.write((int)((v >>>  8) & 0xff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        out.write((int)((v >>> 16) & 0xff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        out.write((int)((v >>> 24) & 0xff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        written += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    /*
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   575
     * Writes a 64-bit int to the output stream in little-endian byte order.
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   576
     */
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   577
    private void writeLong(long v) throws IOException {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   578
        OutputStream out = this.out;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   579
        out.write((int)((v >>>  0) & 0xff));
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   580
        out.write((int)((v >>>  8) & 0xff));
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   581
        out.write((int)((v >>> 16) & 0xff));
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   582
        out.write((int)((v >>> 24) & 0xff));
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   583
        out.write((int)((v >>> 32) & 0xff));
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   584
        out.write((int)((v >>> 40) & 0xff));
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   585
        out.write((int)((v >>> 48) & 0xff));
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   586
        out.write((int)((v >>> 56) & 0xff));
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   587
        written += 8;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   588
    }
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   589
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 1228
diff changeset
   590
    /*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * Writes an array of bytes to the output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    private void writeBytes(byte[] b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        super.out.write(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        written += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * Returns the length of String's UTF8 encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    static int getUTF8Length(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        for (int i = 0; i < s.length(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            char ch = s.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            if (ch <= 0x7f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            } else if (ch <= 0x7ff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                count += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                count += 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * Returns an array of bytes representing the UTF8 encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * of the specified String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    private static byte[] getUTF8Bytes(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        char[] c = s.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        int len = c.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        // Count the number of encoded bytes...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            int ch = c[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            if (ch <= 0x7f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            } else if (ch <= 0x7ff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                count += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                count += 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        // Now return the encoded bytes...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        byte[] b = new byte[count];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        int off = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            int ch = c[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            if (ch <= 0x7f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                b[off++] = (byte)ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            } else if (ch <= 0x7ff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                b[off++] = (byte)((ch >> 6) | 0xc0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                b[off++] = (byte)((ch & 0x3f) | 0x80);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                b[off++] = (byte)((ch >> 12) | 0xe0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                b[off++] = (byte)(((ch >> 6) & 0x3f) | 0x80);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                b[off++] = (byte)((ch & 0x3f) | 0x80);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
}