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.CharsetDecoder;
32 import java.nio.charset.CharsetEncoder;
33 import java.nio.charset.CoderResult;
34 import java.nio.charset.CodingErrorAction;
35 import java.util.Arrays;
36
37 /**
38 * Utility class for zipfile name and comment decoding and encoding
39 */
40
41 final class ZipCoder {
42
43 String toString(byte[] ba, int length) {
44 CharsetDecoder cd = decoder().reset();
45 int len = (int)(length * cd.maxCharsPerByte());
46 char[] ca = new char[len];
47 if (len == 0)
48 return new String(ca);
49 ByteBuffer bb = ByteBuffer.wrap(ba, 0, length);
50 CharBuffer cb = CharBuffer.wrap(ca);
70 return ba;
71 ByteBuffer bb = ByteBuffer.wrap(ba);
72 CharBuffer cb = CharBuffer.wrap(ca);
73 CoderResult cr = ce.encode(cb, bb, true);
74 if (!cr.isUnderflow())
75 throw new IllegalArgumentException(cr.toString());
76 cr = ce.flush(bb);
77 if (!cr.isUnderflow())
78 throw new IllegalArgumentException(cr.toString());
79 if (bb.position() == ba.length) // defensive copy?
80 return ba;
81 else
82 return Arrays.copyOf(ba, bb.position());
83 }
84
85 // assume invoked only if "this" is not utf8
86 byte[] getBytesUTF8(String s) {
87 if (isutf8)
88 return getBytes(s);
89 if (utf8 == null)
90 utf8 = new ZipCoder(Charset.forName("UTF-8"));
91 return utf8.getBytes(s);
92 }
93
94
95 String toStringUTF8(byte[] ba, int len) {
96 if (isutf8)
97 return toString(ba, len);
98 if (utf8 == null)
99 utf8 = new ZipCoder(Charset.forName("UTF-8"));
100 return utf8.toString(ba, len);
101 }
102
103 boolean isUTF8() {
104 return isutf8;
105 }
106
107 private Charset cs;
108 private CharsetDecoder dec;
109 private CharsetEncoder enc;
110 private boolean isutf8;
111 private ZipCoder utf8;
112
113 private ZipCoder(Charset cs) {
114 this.cs = cs;
115 this.isutf8 = cs.name().equals("UTF-8");
116 }
117
118 static ZipCoder get(Charset charset) {
119 return new ZipCoder(charset);
120 }
121
122 private CharsetDecoder decoder() {
123 if (dec == null) {
124 dec = cs.newDecoder()
125 .onMalformedInput(CodingErrorAction.REPORT)
126 .onUnmappableCharacter(CodingErrorAction.REPORT);
127 }
128 return dec;
129 }
130
131 private CharsetEncoder encoder() {
132 if (enc == null) {
133 enc = cs.newEncoder()
134 .onMalformedInput(CodingErrorAction.REPORT)
135 .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.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
38 /**
39 * Utility class for zipfile name and comment decoding and encoding
40 */
41
42 final class ZipCoder {
43
44 String toString(byte[] ba, int length) {
45 CharsetDecoder cd = decoder().reset();
46 int len = (int)(length * cd.maxCharsPerByte());
47 char[] ca = new char[len];
48 if (len == 0)
49 return new String(ca);
50 ByteBuffer bb = ByteBuffer.wrap(ba, 0, length);
51 CharBuffer cb = CharBuffer.wrap(ca);
71 return ba;
72 ByteBuffer bb = ByteBuffer.wrap(ba);
73 CharBuffer cb = CharBuffer.wrap(ca);
74 CoderResult cr = ce.encode(cb, bb, true);
75 if (!cr.isUnderflow())
76 throw new IllegalArgumentException(cr.toString());
77 cr = ce.flush(bb);
78 if (!cr.isUnderflow())
79 throw new IllegalArgumentException(cr.toString());
80 if (bb.position() == ba.length) // defensive copy?
81 return ba;
82 else
83 return Arrays.copyOf(ba, bb.position());
84 }
85
86 // assume invoked only if "this" is not utf8
87 byte[] getBytesUTF8(String s) {
88 if (isutf8)
89 return getBytes(s);
90 if (utf8 == null)
91 utf8 = new ZipCoder(StandardCharset.UTF_8);
92 return utf8.getBytes(s);
93 }
94
95
96 String toStringUTF8(byte[] ba, int len) {
97 if (isutf8)
98 return toString(ba, len);
99 if (utf8 == null)
100 utf8 = new ZipCoder(StandardCharset.UTF_8);
101 return utf8.toString(ba, len);
102 }
103
104 boolean isUTF8() {
105 return isutf8;
106 }
107
108 private Charset cs;
109 private CharsetDecoder dec;
110 private CharsetEncoder enc;
111 private boolean isutf8;
112 private ZipCoder utf8;
113
114 private ZipCoder(Charset cs) {
115 this.cs = cs;
116 this.isutf8 = cs.name().equals(StandardCharset.UTF_8.name());
117 }
118
119 static ZipCoder get(Charset charset) {
120 return new ZipCoder(charset);
121 }
122
123 private CharsetDecoder decoder() {
124 if (dec == null) {
125 dec = cs.newDecoder()
126 .onMalformedInput(CodingErrorAction.REPORT)
127 .onUnmappableCharacter(CodingErrorAction.REPORT);
128 }
129 return dec;
130 }
131
132 private CharsetEncoder encoder() {
133 if (enc == null) {
134 enc = cs.newEncoder()
135 .onMalformedInput(CodingErrorAction.REPORT)
136 .onUnmappableCharacter(CodingErrorAction.REPORT);
|