11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
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.
24 */
25
26 package java.util.zip;
27
28 import java.nio.ByteBuffer;
29 import java.nio.CharBuffer;
30 import java.nio.charset.Charset;
31 import java.nio.charset.StandardCharset;
32 import java.nio.charset.CharsetDecoder;
33 import java.nio.charset.CharsetEncoder;
34 import java.nio.charset.CoderResult;
35 import java.nio.charset.CodingErrorAction;
36 import java.util.Arrays;
37 import sun.nio.cs.ArrayDecoder;
38 import sun.nio.cs.ArrayEncoder;
39
40 /**
41 * Utility class for zipfile name and comment decoding and encoding
42 */
43
44 final class ZipCoder {
45
46 String toString(byte[] ba, int length) {
47 CharsetDecoder cd = decoder().reset();
48 int len = (int)(length * cd.maxCharsPerByte());
49 char[] ca = new char[len];
50 if (len == 0)
51 return new String(ca);
90 }
91 ByteBuffer bb = ByteBuffer.wrap(ba);
92 CharBuffer cb = CharBuffer.wrap(ca);
93 CoderResult cr = ce.encode(cb, bb, true);
94 if (!cr.isUnderflow())
95 throw new IllegalArgumentException(cr.toString());
96 cr = ce.flush(bb);
97 if (!cr.isUnderflow())
98 throw new IllegalArgumentException(cr.toString());
99 if (bb.position() == ba.length) // defensive copy?
100 return ba;
101 else
102 return Arrays.copyOf(ba, bb.position());
103 }
104
105 // assume invoked only if "this" is not utf8
106 byte[] getBytesUTF8(String s) {
107 if (isUTF8)
108 return getBytes(s);
109 if (utf8 == null)
110 utf8 = new ZipCoder(StandardCharset.UTF_8);
111 return utf8.getBytes(s);
112 }
113
114
115 String toStringUTF8(byte[] ba, int len) {
116 if (isUTF8)
117 return toString(ba, len);
118 if (utf8 == null)
119 utf8 = new ZipCoder(StandardCharset.UTF_8);
120 return utf8.toString(ba, len);
121 }
122
123 boolean isUTF8() {
124 return isUTF8;
125 }
126
127 private Charset cs;
128 private CharsetDecoder dec;
129 private CharsetEncoder enc;
130 private boolean isUTF8;
131 private ZipCoder utf8;
132
133 private ZipCoder(Charset cs) {
134 this.cs = cs;
135 this.isUTF8 = cs.name().equals(StandardCharset.UTF_8.name());
136 }
137
138 static ZipCoder get(Charset charset) {
139 return new ZipCoder(charset);
140 }
141
142 private CharsetDecoder decoder() {
143 if (dec == null) {
144 dec = cs.newDecoder()
145 .onMalformedInput(CodingErrorAction.REPORT)
146 .onUnmappableCharacter(CodingErrorAction.REPORT);
147 }
148 return dec;
149 }
150
151 private CharsetEncoder encoder() {
152 if (enc == null) {
153 enc = cs.newEncoder()
154 .onMalformedInput(CodingErrorAction.REPORT)
155 .onUnmappableCharacter(CodingErrorAction.REPORT);
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
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.
24 */
25
26 package java.util.zip;
27
28 import java.nio.ByteBuffer;
29 import java.nio.CharBuffer;
30 import java.nio.charset.Charset;
31 import java.nio.charset.StandardCharsets;
32 import java.nio.charset.CharsetDecoder;
33 import java.nio.charset.CharsetEncoder;
34 import java.nio.charset.CoderResult;
35 import java.nio.charset.CodingErrorAction;
36 import java.util.Arrays;
37 import sun.nio.cs.ArrayDecoder;
38 import sun.nio.cs.ArrayEncoder;
39
40 /**
41 * Utility class for zipfile name and comment decoding and encoding
42 */
43
44 final class ZipCoder {
45
46 String toString(byte[] ba, int length) {
47 CharsetDecoder cd = decoder().reset();
48 int len = (int)(length * cd.maxCharsPerByte());
49 char[] ca = new char[len];
50 if (len == 0)
51 return new String(ca);
90 }
91 ByteBuffer bb = ByteBuffer.wrap(ba);
92 CharBuffer cb = CharBuffer.wrap(ca);
93 CoderResult cr = ce.encode(cb, bb, true);
94 if (!cr.isUnderflow())
95 throw new IllegalArgumentException(cr.toString());
96 cr = ce.flush(bb);
97 if (!cr.isUnderflow())
98 throw new IllegalArgumentException(cr.toString());
99 if (bb.position() == ba.length) // defensive copy?
100 return ba;
101 else
102 return Arrays.copyOf(ba, bb.position());
103 }
104
105 // assume invoked only if "this" is not utf8
106 byte[] getBytesUTF8(String s) {
107 if (isUTF8)
108 return getBytes(s);
109 if (utf8 == null)
110 utf8 = new ZipCoder(StandardCharsets.UTF_8);
111 return utf8.getBytes(s);
112 }
113
114
115 String toStringUTF8(byte[] ba, int len) {
116 if (isUTF8)
117 return toString(ba, len);
118 if (utf8 == null)
119 utf8 = new ZipCoder(StandardCharsets.UTF_8);
120 return utf8.toString(ba, len);
121 }
122
123 boolean isUTF8() {
124 return isUTF8;
125 }
126
127 private Charset cs;
128 private CharsetDecoder dec;
129 private CharsetEncoder enc;
130 private boolean isUTF8;
131 private ZipCoder utf8;
132
133 private ZipCoder(Charset cs) {
134 this.cs = cs;
135 this.isUTF8 = cs.name().equals(StandardCharsets.UTF_8.name());
136 }
137
138 static ZipCoder get(Charset charset) {
139 return new ZipCoder(charset);
140 }
141
142 private CharsetDecoder decoder() {
143 if (dec == null) {
144 dec = cs.newDecoder()
145 .onMalformedInput(CodingErrorAction.REPORT)
146 .onUnmappableCharacter(CodingErrorAction.REPORT);
147 }
148 return dec;
149 }
150
151 private CharsetEncoder encoder() {
152 if (enc == null) {
153 enc = cs.newEncoder()
154 .onMalformedInput(CodingErrorAction.REPORT)
155 .onUnmappableCharacter(CodingErrorAction.REPORT);
|