41 * Utility class for zipfile name and comment decoding and encoding |
41 * Utility class for zipfile name and comment decoding and encoding |
42 */ |
42 */ |
43 |
43 |
44 final class ZipCoder { |
44 final class ZipCoder { |
45 |
45 |
46 String toString(byte[] ba, int length) { |
46 String toString(byte[] ba, int off, int length) { |
47 CharsetDecoder cd = decoder().reset(); |
47 CharsetDecoder cd = decoder().reset(); |
48 int len = (int)(length * cd.maxCharsPerByte()); |
48 int len = (int)(length * cd.maxCharsPerByte()); |
49 char[] ca = new char[len]; |
49 char[] ca = new char[len]; |
50 if (len == 0) |
50 if (len == 0) |
51 return new String(ca); |
51 return new String(ca); |
52 // UTF-8 only for now. Other ArrayDeocder only handles |
52 // UTF-8 only for now. Other ArrayDeocder only handles |
53 // CodingErrorAction.REPLACE mode. ZipCoder uses |
53 // CodingErrorAction.REPLACE mode. ZipCoder uses |
54 // REPORT mode. |
54 // REPORT mode. |
55 if (isUTF8 && cd instanceof ArrayDecoder) { |
55 if (isUTF8 && cd instanceof ArrayDecoder) { |
56 int clen = ((ArrayDecoder)cd).decode(ba, 0, length, ca); |
56 int clen = ((ArrayDecoder)cd).decode(ba, off, length, ca); |
57 if (clen == -1) // malformed |
57 if (clen == -1) // malformed |
58 throw new IllegalArgumentException("MALFORMED"); |
58 throw new IllegalArgumentException("MALFORMED"); |
59 return new String(ca, 0, clen); |
59 return new String(ca, 0, clen); |
60 } |
60 } |
61 ByteBuffer bb = ByteBuffer.wrap(ba, 0, length); |
61 ByteBuffer bb = ByteBuffer.wrap(ba, off, length); |
62 CharBuffer cb = CharBuffer.wrap(ca); |
62 CharBuffer cb = CharBuffer.wrap(ca); |
63 CoderResult cr = cd.decode(bb, cb, true); |
63 CoderResult cr = cd.decode(bb, cb, true); |
64 if (!cr.isUnderflow()) |
64 if (!cr.isUnderflow()) |
65 throw new IllegalArgumentException(cr.toString()); |
65 throw new IllegalArgumentException(cr.toString()); |
66 cr = cd.flush(cb); |
66 cr = cd.flush(cb); |
67 if (!cr.isUnderflow()) |
67 if (!cr.isUnderflow()) |
68 throw new IllegalArgumentException(cr.toString()); |
68 throw new IllegalArgumentException(cr.toString()); |
69 return new String(ca, 0, cb.position()); |
69 return new String(ca, 0, cb.position()); |
70 } |
70 } |
71 |
71 |
|
72 String toString(byte[] ba, int length) { |
|
73 return toString(ba, 0, length); |
|
74 } |
|
75 |
72 String toString(byte[] ba) { |
76 String toString(byte[] ba) { |
73 return toString(ba, ba.length); |
77 return toString(ba, 0, ba.length); |
74 } |
78 } |
75 |
79 |
76 byte[] getBytes(String s) { |
80 byte[] getBytes(String s) { |
77 CharsetEncoder ce = encoder().reset(); |
81 CharsetEncoder ce = encoder().reset(); |
78 char[] ca = s.toCharArray(); |
82 char[] ca = s.toCharArray(); |
109 if (utf8 == null) |
113 if (utf8 == null) |
110 utf8 = new ZipCoder(StandardCharsets.UTF_8); |
114 utf8 = new ZipCoder(StandardCharsets.UTF_8); |
111 return utf8.getBytes(s); |
115 return utf8.getBytes(s); |
112 } |
116 } |
113 |
117 |
|
118 String toStringUTF8(byte[] ba, int len) { |
|
119 return toStringUTF8(ba, 0, len); |
|
120 } |
114 |
121 |
115 String toStringUTF8(byte[] ba, int len) { |
122 String toStringUTF8(byte[] ba, int off, int len) { |
116 if (isUTF8) |
123 if (isUTF8) |
117 return toString(ba, len); |
124 return toString(ba, off, len); |
118 if (utf8 == null) |
125 if (utf8 == null) |
119 utf8 = new ZipCoder(StandardCharsets.UTF_8); |
126 utf8 = new ZipCoder(StandardCharsets.UTF_8); |
120 return utf8.toString(ba, len); |
127 return utf8.toString(ba, off, len); |
121 } |
128 } |
122 |
129 |
123 boolean isUTF8() { |
130 boolean isUTF8() { |
124 return isUTF8; |
131 return isUTF8; |
125 } |
132 } |