jdk/src/java.base/share/classes/java/util/zip/ZipCoder.java
changeset 34526 f1f852f5f477
parent 34525 14d2d84cae6d
child 34686 29ea8310a27a
equal deleted inserted replaced
34525:14d2d84cae6d 34526:f1f852f5f477
    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 off, int length) {
    46     String toString(byte[] ba, 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, off, length, ca);
    56             int clen = ((ArrayDecoder)cd).decode(ba, 0, 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, off, length);
    61         ByteBuffer bb = ByteBuffer.wrap(ba, 0, 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 
       
    76     String toString(byte[] ba) {
    72     String toString(byte[] ba) {
    77         return toString(ba, 0, ba.length);
    73         return toString(ba, ba.length);
    78     }
    74     }
    79 
    75 
    80     byte[] getBytes(String s) {
    76     byte[] getBytes(String s) {
    81         CharsetEncoder ce = encoder().reset();
    77         CharsetEncoder ce = encoder().reset();
    82         char[] ca = s.toCharArray();
    78         char[] ca = s.toCharArray();
   113         if (utf8 == null)
   109         if (utf8 == null)
   114             utf8 = new ZipCoder(StandardCharsets.UTF_8);
   110             utf8 = new ZipCoder(StandardCharsets.UTF_8);
   115         return utf8.getBytes(s);
   111         return utf8.getBytes(s);
   116     }
   112     }
   117 
   113 
       
   114 
   118     String toStringUTF8(byte[] ba, int len) {
   115     String toStringUTF8(byte[] ba, int len) {
   119         return toStringUTF8(ba, 0, len);
       
   120     }
       
   121 
       
   122     String toStringUTF8(byte[] ba, int off, int len) {
       
   123         if (isUTF8)
   116         if (isUTF8)
   124             return toString(ba, off, len);
   117             return toString(ba, len);
   125         if (utf8 == null)
   118         if (utf8 == null)
   126             utf8 = new ZipCoder(StandardCharsets.UTF_8);
   119             utf8 = new ZipCoder(StandardCharsets.UTF_8);
   127         return utf8.toString(ba, off, len);
   120         return utf8.toString(ba, len);
   128     }
   121     }
   129 
   122 
   130     boolean isUTF8() {
   123     boolean isUTF8() {
   131         return isUTF8;
   124         return isUTF8;
   132     }
   125     }