jdk/src/share/classes/java/util/zip/ZipOutputStream.java
author sherman
Wed, 04 Mar 2009 09:26:41 -0800
changeset 2178 5a730fff4d0d
parent 1228 1515928f48cd
child 2438 21c111b51aa8
permissions -rw-r--r--
6812879: Excess code line in ArrayList method Summary: Removed the line of "oldData" which is no longer used. Reviewed-by: martin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
1228
1515928f48cd 6440786: Cannot create a ZIP file containing zero entries
bristor
parents: 2
diff changeset
     2
 * Copyright 1996-2008 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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * This class implements an output stream filter for writing files in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * ZIP file format. Includes support for both compressed and uncompressed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @author      David Connelly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
class ZipOutputStream extends DeflaterOutputStream implements ZipConstants {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private static class XEntry {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        public final ZipEntry entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        public final long offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        public final int flag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        public XEntry(ZipEntry entry, long offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            this.entry = entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            this.offset = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            this.flag = (entry.method == DEFLATED &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                         (entry.size  == -1 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                          entry.csize == -1 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
                          entry.crc   == -1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                // store size, compressed size, and crc-32 in data descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                // immediately following the compressed entry data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                ? 8
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                // store size, compressed size, and crc-32 in LOC header
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private XEntry current;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private Vector<XEntry> xentries = new Vector<XEntry>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private HashSet<String> names = new HashSet<String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private CRC32 crc = new CRC32();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private long written = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private long locoff = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private String comment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private int method = DEFLATED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private boolean finished;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private boolean closed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private static int version(ZipEntry e) throws ZipException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        switch (e.method) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        case DEFLATED: return 20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        case STORED:   return 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        default: throw new ZipException("unsupported compression method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
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
     * Checks to make sure that this stream has not been closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private void ensureOpen() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        if (closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            throw new IOException("Stream closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * Compression method for uncompressed (STORED) entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public static final int STORED = ZipEntry.STORED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Compression method for compressed (DEFLATED) entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public static final int DEFLATED = ZipEntry.DEFLATED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Creates a new ZIP output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @param out the actual output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public ZipOutputStream(OutputStream out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        super(out, new Deflater(Deflater.DEFAULT_COMPRESSION, true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        usesDefaultDeflater = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * Sets the ZIP file comment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @param comment the comment string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @exception IllegalArgumentException if the length of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *            ZIP file comment is greater than 0xFFFF bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public void setComment(String comment) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        if (comment != null && comment.length() > 0xffff/3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                                           && getUTF8Length(comment) > 0xffff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            throw new IllegalArgumentException("ZIP file comment too long.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        this.comment = comment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * Sets the default compression method for subsequent entries. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * default will be used whenever the compression method is not specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * for an individual ZIP file entry, and is initially set to DEFLATED.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @param method the default compression method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @exception IllegalArgumentException if the specified compression method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *            is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public void setMethod(int method) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        if (method != DEFLATED && method != STORED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            throw new IllegalArgumentException("invalid compression method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        this.method = method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * Sets the compression level for subsequent entries which are DEFLATED.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * The default setting is DEFAULT_COMPRESSION.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @param level the compression level (0-9)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @exception IllegalArgumentException if the compression level is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    public void setLevel(int level) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        def.setLevel(level);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * Begins writing a new ZIP file entry and positions the stream to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * start of the entry data. Closes the current entry if still active.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * The default compression method will be used if no compression method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * was specified for the entry, and the current time will be used if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * the entry has no set modification time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @param e the ZIP entry to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @exception ZipException if a ZIP format error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @exception IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    public void putNextEntry(ZipEntry e) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if (current != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            closeEntry();       // close previous entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        if (e.time == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            e.setTime(System.currentTimeMillis());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        if (e.method == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            e.method = method;  // use default method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        switch (e.method) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        case DEFLATED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        case STORED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            // compressed size, uncompressed size, and crc-32 must all be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            // set for entries using STORED compression method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            if (e.size == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                e.size = e.csize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            } else if (e.csize == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                e.csize = e.size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            } else if (e.size != e.csize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                throw new ZipException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                    "STORED entry where compressed != uncompressed size");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            if (e.size == -1 || e.crc == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                throw new ZipException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                    "STORED entry missing size, compressed size, or crc-32");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            throw new ZipException("unsupported compression method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        if (! names.add(e.name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            throw new ZipException("duplicate entry: " + e.name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        current = new XEntry(e, written);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        xentries.add(current);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        writeLOC(current);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * Closes the current ZIP entry and positions the stream for writing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * the next entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @exception ZipException if a ZIP format error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * @exception IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    public void closeEntry() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        if (current != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            ZipEntry e = current.entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            switch (e.method) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            case DEFLATED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                def.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                while (!def.finished()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                    deflate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                if ((current.flag & 8) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                    // verify size, compressed size, and crc-32 settings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                    if (e.size != def.getBytesRead()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                        throw new ZipException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                            "invalid entry size (expected " + e.size +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                            " but got " + def.getBytesRead() + " bytes)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                    if (e.csize != def.getBytesWritten()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                        throw new ZipException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                            "invalid entry compressed size (expected " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                            e.csize + " but got " + def.getBytesWritten() + " bytes)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                    if (e.crc != crc.getValue()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                        throw new ZipException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                            "invalid entry CRC-32 (expected 0x" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                            Long.toHexString(e.crc) + " but got 0x" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                            Long.toHexString(crc.getValue()) + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                    e.size  = def.getBytesRead();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                    e.csize = def.getBytesWritten();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                    e.crc = crc.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                    writeEXT(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                def.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                written += e.csize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            case STORED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                // we already know that both e.size and e.csize are the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                if (e.size != written - locoff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                    throw new ZipException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                        "invalid entry size (expected " + e.size +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                        " but got " + (written - locoff) + " bytes)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                if (e.crc != crc.getValue()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                    throw new ZipException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                         "invalid entry crc-32 (expected 0x" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                         Long.toHexString(e.crc) + " but got 0x" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                         Long.toHexString(crc.getValue()) + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                throw new ZipException("invalid compression method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            crc.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            current = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
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
     * Writes an array of bytes to the current ZIP entry data. This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * will block until all the bytes are written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @param b the data to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * @param off the start offset in the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * @param len the number of bytes that are written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * @exception ZipException if a ZIP file error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * @exception IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    public synchronized void write(byte[] b, int off, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        if (off < 0 || len < 0 || off > b.length - len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        } else if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        if (current == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            throw new ZipException("no current ZIP entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        ZipEntry entry = current.entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        switch (entry.method) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        case DEFLATED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            super.write(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        case STORED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            written += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            if (written - locoff > entry.size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                throw new ZipException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                    "attempt to write past end of STORED entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            out.write(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            throw new ZipException("invalid compression method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        crc.update(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * Finishes writing the contents of the ZIP output stream without closing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * the underlying stream. Use this method when applying multiple filters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * in succession to the same output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * @exception ZipException if a ZIP file error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * @exception IOException if an I/O exception has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    public void finish() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        if (finished) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        if (current != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            closeEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        // write central directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        long off = written;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        for (XEntry xentry : xentries)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            writeCEN(xentry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        writeEND(off, written - off);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        finished = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * Closes the ZIP output stream as well as the stream being filtered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * @exception ZipException if a ZIP file error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * @exception IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        if (!closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            super.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            closed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        }
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
     * Writes local file (LOC) header for specified entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    private void writeLOC(XEntry xentry) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        ZipEntry e = xentry.entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        int flag = xentry.flag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        writeInt(LOCSIG);           // LOC header signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        writeShort(version(e));     // version needed to extract
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        writeShort(flag);           // general purpose bit flag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        writeShort(e.method);       // compression method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        writeInt(e.time);           // last modification time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        if ((flag & 8) == 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            // store size, uncompressed size, and crc-32 in data descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            // immediately following compressed entry data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            writeInt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            writeInt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            writeInt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            writeInt(e.crc);        // crc-32
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            writeInt(e.csize);      // compressed size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            writeInt(e.size);       // uncompressed size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        byte[] nameBytes = getUTF8Bytes(e.name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        writeShort(nameBytes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        writeShort(e.extra != null ? e.extra.length : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        writeBytes(nameBytes, 0, nameBytes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        if (e.extra != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            writeBytes(e.extra, 0, e.extra.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        locoff = written;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * Writes extra data descriptor (EXT) for specified entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    private void writeEXT(ZipEntry e) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        writeInt(EXTSIG);           // EXT header signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        writeInt(e.crc);            // crc-32
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        writeInt(e.csize);          // compressed size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        writeInt(e.size);           // uncompressed size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * Write central directory (CEN) header for specified entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * REMIND: add support for file attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    private void writeCEN(XEntry xentry) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        ZipEntry e  = xentry.entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        int flag = xentry.flag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        int version = version(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        writeInt(CENSIG);           // CEN header signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        writeShort(version);        // version made by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        writeShort(version);        // version needed to extract
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        writeShort(flag);           // general purpose bit flag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        writeShort(e.method);       // compression method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        writeInt(e.time);           // last modification time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        writeInt(e.crc);            // crc-32
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        writeInt(e.csize);          // compressed size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        writeInt(e.size);           // uncompressed size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        byte[] nameBytes = getUTF8Bytes(e.name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        writeShort(nameBytes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        writeShort(e.extra != null ? e.extra.length : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        byte[] commentBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        if (e.comment != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            commentBytes = getUTF8Bytes(e.comment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            writeShort(commentBytes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            commentBytes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            writeShort(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        writeShort(0);              // starting disk number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        writeShort(0);              // internal file attributes (unused)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        writeInt(0);                // external file attributes (unused)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        writeInt(xentry.offset);    // relative offset of local header
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        writeBytes(nameBytes, 0, nameBytes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        if (e.extra != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            writeBytes(e.extra, 0, e.extra.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        if (commentBytes != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            writeBytes(commentBytes, 0, commentBytes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * Writes end of central directory (END) header.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    private void writeEND(long off, long len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        int count = xentries.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        writeInt(ENDSIG);           // END record signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        writeShort(0);              // number of this disk
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        writeShort(0);              // central directory start disk
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        writeShort(count);          // number of directory entries on disk
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        writeShort(count);          // total number of directory entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        writeInt(len);              // length of central directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        writeInt(off);              // offset of central directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        if (comment != null) {      // zip file comment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            byte[] b = getUTF8Bytes(comment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            writeShort(b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            writeBytes(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            writeShort(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * Writes a 16-bit short to the output stream in little-endian byte order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    private void writeShort(int v) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        OutputStream out = this.out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        out.write((v >>> 0) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        out.write((v >>> 8) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        written += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * Writes a 32-bit int to the output stream in little-endian byte order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    private void writeInt(long v) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        OutputStream out = this.out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        out.write((int)((v >>>  0) & 0xff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        out.write((int)((v >>>  8) & 0xff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        out.write((int)((v >>> 16) & 0xff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        out.write((int)((v >>> 24) & 0xff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        written += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * Writes an array of bytes to the output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    private void writeBytes(byte[] b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        super.out.write(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        written += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * Returns the length of String's UTF8 encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    static int getUTF8Length(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        for (int i = 0; i < s.length(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            char ch = s.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            if (ch <= 0x7f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            } else if (ch <= 0x7ff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                count += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                count += 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * Returns an array of bytes representing the UTF8 encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * of the specified String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    private static byte[] getUTF8Bytes(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        char[] c = s.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        int len = c.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        // Count the number of encoded bytes...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            int ch = c[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            if (ch <= 0x7f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            } else if (ch <= 0x7ff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                count += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                count += 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        // Now return the encoded bytes...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        byte[] b = new byte[count];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        int off = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            int ch = c[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            if (ch <= 0x7f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                b[off++] = (byte)ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            } else if (ch <= 0x7ff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                b[off++] = (byte)((ch >> 6) | 0xc0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                b[off++] = (byte)((ch & 0x3f) | 0x80);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                b[off++] = (byte)((ch >> 12) | 0xe0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                b[off++] = (byte)(((ch >> 6) & 0x3f) | 0x80);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                b[off++] = (byte)((ch & 0x3f) | 0x80);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
}