jdk/src/share/classes/java/util/zip/ZipCoder.java
author lana
Mon, 07 Jun 2010 17:08:26 -0700
changeset 5631 cfb4f43485f3
parent 5506 202f599c92aa
child 9526 a80328f995f1
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2592
diff changeset
     2
 * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
     4
 *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2592
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2592
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    10
 *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    15
 * accompanied this code).
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    16
 *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2592
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2592
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2592
diff changeset
    23
 * questions.
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    24
 */
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    25
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    26
package java.util.zip;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    27
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    28
import java.nio.ByteBuffer;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    29
import java.nio.CharBuffer;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    30
import java.nio.charset.Charset;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    31
import java.nio.charset.CharsetDecoder;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    32
import java.nio.charset.CharsetEncoder;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    33
import java.nio.charset.CoderResult;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    34
import java.nio.charset.CodingErrorAction;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    35
import java.util.Arrays;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    36
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    37
/**
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    38
 * Utility class for zipfile name and comment decoding and encoding
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    39
 */
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    40
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    41
final class ZipCoder {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    42
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    43
    String toString(byte[] ba, int length) {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    44
        CharsetDecoder cd = decoder().reset();
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    45
        int len = (int)(length * cd.maxCharsPerByte());
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    46
        char[] ca = new char[len];
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    47
        if (len == 0)
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    48
            return new String(ca);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    49
        ByteBuffer bb = ByteBuffer.wrap(ba, 0, length);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    50
        CharBuffer cb = CharBuffer.wrap(ca);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    51
        CoderResult cr = cd.decode(bb, cb, true);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    52
        if (!cr.isUnderflow())
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    53
            throw new IllegalArgumentException(cr.toString());
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    54
        cr = cd.flush(cb);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    55
        if (!cr.isUnderflow())
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    56
            throw new IllegalArgumentException(cr.toString());
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    57
        return new String(ca, 0, cb.position());
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    58
    }
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    59
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    60
    String toString(byte[] ba) {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    61
        return toString(ba, ba.length);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    62
    }
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    63
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    64
    byte[] getBytes(String s) {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    65
        CharsetEncoder ce = encoder().reset();
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    66
        char[] ca = s.toCharArray();
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    67
        int len = (int)(ca.length * ce.maxBytesPerChar());
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    68
        byte[] ba = new byte[len];
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    69
        if (len == 0)
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    70
            return ba;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    71
        ByteBuffer bb = ByteBuffer.wrap(ba);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    72
        CharBuffer cb = CharBuffer.wrap(ca);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    73
        CoderResult cr = ce.encode(cb, bb, true);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    74
        if (!cr.isUnderflow())
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    75
            throw new IllegalArgumentException(cr.toString());
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    76
        cr = ce.flush(bb);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    77
        if (!cr.isUnderflow())
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    78
            throw new IllegalArgumentException(cr.toString());
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    79
        if (bb.position() == ba.length)  // defensive copy?
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    80
            return ba;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    81
        else
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    82
            return Arrays.copyOf(ba, bb.position());
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    83
    }
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    84
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    85
    // assume invoked only if "this" is not utf8
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    86
    byte[] getBytesUTF8(String s) {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    87
        if (isutf8)
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    88
            return getBytes(s);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    89
        if (utf8 == null)
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    90
            utf8 = new ZipCoder(Charset.forName("UTF-8"));
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    91
        return utf8.getBytes(s);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    92
    }
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    93
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    94
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    95
    String toStringUTF8(byte[] ba, int len) {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    96
        if (isutf8)
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    97
            return toString(ba, len);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    98
        if (utf8 == null)
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
    99
            utf8 = new ZipCoder(Charset.forName("UTF-8"));
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   100
        return utf8.toString(ba, len);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   101
    }
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   102
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   103
    boolean isUTF8() {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   104
        return isutf8;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   105
    }
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   106
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   107
    private Charset cs;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   108
    private CharsetDecoder dec;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   109
    private CharsetEncoder enc;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   110
    private boolean isutf8;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   111
    private ZipCoder utf8;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   112
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   113
    private ZipCoder(Charset cs) {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   114
        this.cs = cs;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   115
        this.isutf8 = cs.name().equals("UTF-8");
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   116
    }
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   117
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   118
    static ZipCoder get(Charset charset) {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   119
        return new ZipCoder(charset);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   120
    }
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   121
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   122
    private CharsetDecoder decoder() {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   123
        if (dec == null) {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   124
            dec = cs.newDecoder()
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   125
              .onMalformedInput(CodingErrorAction.REPORT)
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   126
              .onUnmappableCharacter(CodingErrorAction.REPORT);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   127
        }
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   128
        return dec;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   129
    }
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   130
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   131
    private CharsetEncoder encoder() {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   132
        if (enc == null) {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   133
            enc = cs.newEncoder()
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   134
              .onMalformedInput(CodingErrorAction.REPORT)
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   135
              .onUnmappableCharacter(CodingErrorAction.REPORT);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   136
        }
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   137
        return enc;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   138
    }
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff changeset
   139
}