1 /*
   2  * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  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);
  52         CoderResult cr = cd.decode(bb, cb, true);
  53         if (!cr.isUnderflow())
  54             throw new IllegalArgumentException(cr.toString());
  55         cr = cd.flush(cb);
  56         if (!cr.isUnderflow())
  57             throw new IllegalArgumentException(cr.toString());
  58         return new String(ca, 0, cb.position());
  59     }
  60 
  61     String toString(byte[] ba) {
  62         return toString(ba, ba.length);
  63     }
  64 
  65     byte[] getBytes(String s) {
  66         CharsetEncoder ce = encoder().reset();
  67         char[] ca = s.toCharArray();
  68         int len = (int)(ca.length * ce.maxBytesPerChar());
  69         byte[] ba = new byte[len];
  70         if (len == 0)
  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);
 137         }
 138         return enc;
 139     }
 140 }