author | mchung |
Tue, 06 Nov 2018 10:01:16 -0800 | |
changeset 52427 | 3c6aa484536c |
parent 49442 | d6d1c06becda |
permissions | -rw-r--r-- |
2592
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff
changeset
|
1 |
/* |
52427
3c6aa484536c
8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents:
49442
diff
changeset
|
2 |
* Copyright (c) 2009, 2018, 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 | 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 | 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 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
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; |
48262 | 33 |
import java.nio.charset.CharacterCodingException; |
2592
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff
changeset
|
34 |
import java.nio.charset.CodingErrorAction; |
48262 | 35 |
|
36 |
import static java.nio.charset.StandardCharsets.UTF_8; |
|
2592
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 |
/** |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff
changeset
|
39 |
* 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
|
40 |
*/ |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff
changeset
|
41 |
|
48262 | 42 |
class ZipCoder { |
43 |
||
52427
3c6aa484536c
8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents:
49442
diff
changeset
|
44 |
private static final jdk.internal.access.JavaLangAccess JLA = |
3c6aa484536c
8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents:
49442
diff
changeset
|
45 |
jdk.internal.access.SharedSecrets.getJavaLangAccess(); |
48262 | 46 |
|
47 |
static final class UTF8 extends ZipCoder { |
|
48 |
||
49 |
UTF8(Charset utf8) { |
|
50 |
super(utf8); |
|
51 |
} |
|
2592
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff
changeset
|
52 |
|
48262 | 53 |
@Override |
54 |
boolean isUTF8() { |
|
55 |
return true; |
|
47987
85ea7e83af30
8189611: JarFile versioned stream and real name support
sherman
parents:
47216
diff
changeset
|
56 |
} |
48262 | 57 |
|
58 |
@Override |
|
59 |
String toString(byte[] ba, int off, int length) { |
|
60 |
return JLA.newStringUTF8NoRepl(ba, off, length); |
|
61 |
} |
|
62 |
||
63 |
@Override |
|
64 |
byte[] getBytes(String s) { |
|
65 |
return JLA.getBytesUTF8NoRepl(s); |
|
66 |
} |
|
47987
85ea7e83af30
8189611: JarFile versioned stream and real name support
sherman
parents:
47216
diff
changeset
|
67 |
} |
85ea7e83af30
8189611: JarFile versioned stream and real name support
sherman
parents:
47216
diff
changeset
|
68 |
|
48262 | 69 |
// UTF_8.ArrayEn/Decoder is stateless, so make it singleton. |
70 |
private static ZipCoder utf8 = new UTF8(UTF_8); |
|
71 |
||
72 |
public static ZipCoder get(Charset charset) { |
|
73 |
if (charset == UTF_8) |
|
74 |
return utf8; |
|
75 |
return new ZipCoder(charset); |
|
47987
85ea7e83af30
8189611: JarFile versioned stream and real name support
sherman
parents:
47216
diff
changeset
|
76 |
} |
85ea7e83af30
8189611: JarFile versioned stream and real name support
sherman
parents:
47216
diff
changeset
|
77 |
|
34686
29ea8310a27a
8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents:
34526
diff
changeset
|
78 |
String toString(byte[] ba, int off, int length) { |
48262 | 79 |
try { |
49442 | 80 |
return decoder().decode(ByteBuffer.wrap(ba, off, length)).toString(); |
48262 | 81 |
} catch (CharacterCodingException x) { |
82 |
throw new IllegalArgumentException(x); |
|
9547
454881baaca0
7040220: java/char_encodin Optimize UTF-8 charset for String.getBytes()/new String(byte[])
sherman
parents:
9526
diff
changeset
|
83 |
} |
2592
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 |
|
34686
29ea8310a27a
8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents:
34526
diff
changeset
|
86 |
String toString(byte[] ba, int length) { |
29ea8310a27a
8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents:
34526
diff
changeset
|
87 |
return toString(ba, 0, length); |
29ea8310a27a
8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents:
34526
diff
changeset
|
88 |
} |
29ea8310a27a
8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents:
34526
diff
changeset
|
89 |
|
2592
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff
changeset
|
90 |
String toString(byte[] ba) { |
34686
29ea8310a27a
8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents:
34526
diff
changeset
|
91 |
return toString(ba, 0, ba.length); |
2592
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 |
byte[] getBytes(String s) { |
48262 | 95 |
try { |
96 |
ByteBuffer bb = encoder().encode(CharBuffer.wrap(s)); |
|
97 |
int pos = bb.position(); |
|
98 |
int limit = bb.limit(); |
|
99 |
if (bb.hasArray() && pos == 0 && limit == bb.capacity()) { |
|
100 |
return bb.array(); |
|
47987
85ea7e83af30
8189611: JarFile versioned stream and real name support
sherman
parents:
47216
diff
changeset
|
101 |
} |
48262 | 102 |
byte[] bytes = new byte[bb.limit() - bb.position()]; |
103 |
bb.get(bytes); |
|
104 |
return bytes; |
|
105 |
} catch (CharacterCodingException x) { |
|
106 |
throw new IllegalArgumentException(x); |
|
47987
85ea7e83af30
8189611: JarFile versioned stream and real name support
sherman
parents:
47216
diff
changeset
|
107 |
} |
2592
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff
changeset
|
108 |
} |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff
changeset
|
109 |
|
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff
changeset
|
110 |
// assume invoked only if "this" is not utf8 |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff
changeset
|
111 |
byte[] getBytesUTF8(String s) { |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff
changeset
|
112 |
return utf8.getBytes(s); |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff
changeset
|
113 |
} |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff
changeset
|
114 |
|
34686
29ea8310a27a
8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents:
34526
diff
changeset
|
115 |
String toStringUTF8(byte[] ba, int len) { |
48262 | 116 |
return utf8.toString(ba, 0, len); |
34686
29ea8310a27a
8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents:
34526
diff
changeset
|
117 |
} |
34526
f1f852f5f477
8144958: changes by JDK-8142508 seems to have broken jtreg
sherman
parents:
34525
diff
changeset
|
118 |
|
34686
29ea8310a27a
8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents:
34526
diff
changeset
|
119 |
String toStringUTF8(byte[] ba, int off, int len) { |
29ea8310a27a
8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents:
34526
diff
changeset
|
120 |
return utf8.toString(ba, off, len); |
2592
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 |
|
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff
changeset
|
123 |
boolean isUTF8() { |
48262 | 124 |
return false; |
2592
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff
changeset
|
125 |
} |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff
changeset
|
126 |
|
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff
changeset
|
127 |
private Charset cs; |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff
changeset
|
128 |
private CharsetDecoder dec; |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff
changeset
|
129 |
private CharsetEncoder enc; |
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 ZipCoder(Charset cs) { |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff
changeset
|
132 |
this.cs = cs; |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff
changeset
|
133 |
} |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff
changeset
|
134 |
|
48262 | 135 |
protected CharsetDecoder decoder() { |
2592
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff
changeset
|
136 |
if (dec == null) { |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff
changeset
|
137 |
dec = cs.newDecoder() |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff
changeset
|
138 |
.onMalformedInput(CodingErrorAction.REPORT) |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff
changeset
|
139 |
.onUnmappableCharacter(CodingErrorAction.REPORT); |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff
changeset
|
140 |
} |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff
changeset
|
141 |
return dec; |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff
changeset
|
142 |
} |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff
changeset
|
143 |
|
48262 | 144 |
protected CharsetEncoder encoder() { |
2592
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff
changeset
|
145 |
if (enc == null) { |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff
changeset
|
146 |
enc = cs.newEncoder() |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff
changeset
|
147 |
.onMalformedInput(CodingErrorAction.REPORT) |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff
changeset
|
148 |
.onUnmappableCharacter(CodingErrorAction.REPORT); |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff
changeset
|
149 |
} |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff
changeset
|
150 |
return enc; |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff
changeset
|
151 |
} |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
diff
changeset
|
152 |
} |