|
| 1 | +/* |
| 2 | + * Copyright (c) 1996, 2020, 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 io.github.scala_cli.zip; |
| 27 | + |
| 28 | +import java.io.UnsupportedEncodingException; |
| 29 | +import java.nio.ByteBuffer; |
| 30 | +import java.nio.CharBuffer; |
| 31 | +import java.nio.charset.Charset; |
| 32 | +import java.nio.charset.CharsetDecoder; |
| 33 | +import java.nio.charset.CharsetEncoder; |
| 34 | +import java.nio.charset.CharacterCodingException; |
| 35 | +import java.nio.charset.CodingErrorAction; |
| 36 | + |
| 37 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 38 | + |
| 39 | +/** |
| 40 | + * Utility class for zipfile name and comment decoding and encoding |
| 41 | + */ |
| 42 | + |
| 43 | +class ZipCoder { |
| 44 | + |
| 45 | + static final class UTF8 extends ZipCoder { |
| 46 | + |
| 47 | + UTF8(Charset utf8) { |
| 48 | + super(utf8); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + boolean isUTF8() { |
| 53 | + return true; |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + String toString(byte[] ba, int off, int length) { |
| 58 | + try { |
| 59 | + return new String(ba, off, length, "UTF-8"); |
| 60 | + } catch (UnsupportedEncodingException ex) { |
| 61 | + throw new RuntimeException(ex); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + byte[] getBytes(String s) { |
| 67 | + try { |
| 68 | + return s.getBytes("UTF-8"); |
| 69 | + } catch (UnsupportedEncodingException ex) { |
| 70 | + throw new RuntimeException(ex); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + // UTF_8.ArrayEn/Decoder is stateless, so make it singleton. |
| 76 | + private static ZipCoder utf8 = new UTF8(UTF_8); |
| 77 | + |
| 78 | + public static ZipCoder get(Charset charset) { |
| 79 | + if (charset == UTF_8) |
| 80 | + return utf8; |
| 81 | + return new ZipCoder(charset); |
| 82 | + } |
| 83 | + |
| 84 | + String toString(byte[] ba, int off, int length) { |
| 85 | + try { |
| 86 | + return decoder().decode(ByteBuffer.wrap(ba, off, length)).toString(); |
| 87 | + } catch (CharacterCodingException x) { |
| 88 | + throw new IllegalArgumentException(x); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + String toString(byte[] ba, int length) { |
| 93 | + return toString(ba, 0, length); |
| 94 | + } |
| 95 | + |
| 96 | + String toString(byte[] ba) { |
| 97 | + return toString(ba, 0, ba.length); |
| 98 | + } |
| 99 | + |
| 100 | + byte[] getBytes(String s) { |
| 101 | + try { |
| 102 | + ByteBuffer bb = encoder().encode(CharBuffer.wrap(s)); |
| 103 | + int pos = bb.position(); |
| 104 | + int limit = bb.limit(); |
| 105 | + if (bb.hasArray() && pos == 0 && limit == bb.capacity()) { |
| 106 | + return bb.array(); |
| 107 | + } |
| 108 | + byte[] bytes = new byte[bb.limit() - bb.position()]; |
| 109 | + bb.get(bytes); |
| 110 | + return bytes; |
| 111 | + } catch (CharacterCodingException x) { |
| 112 | + throw new IllegalArgumentException(x); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + // assume invoked only if "this" is not utf8 |
| 117 | + byte[] getBytesUTF8(String s) { |
| 118 | + return utf8.getBytes(s); |
| 119 | + } |
| 120 | + |
| 121 | + static String toStringUTF8(byte[] ba, int len) { |
| 122 | + return utf8.toString(ba, 0, len); |
| 123 | + } |
| 124 | + |
| 125 | + static String toStringUTF8(byte[] ba, int off, int len) { |
| 126 | + return utf8.toString(ba, off, len); |
| 127 | + } |
| 128 | + |
| 129 | + boolean isUTF8() { |
| 130 | + return false; |
| 131 | + } |
| 132 | + |
| 133 | + private Charset cs; |
| 134 | + private CharsetDecoder dec; |
| 135 | + private CharsetEncoder enc; |
| 136 | + |
| 137 | + private ZipCoder(Charset cs) { |
| 138 | + this.cs = cs; |
| 139 | + } |
| 140 | + |
| 141 | + protected CharsetDecoder decoder() { |
| 142 | + if (dec == null) { |
| 143 | + dec = cs.newDecoder() |
| 144 | + .onMalformedInput(CodingErrorAction.REPORT) |
| 145 | + .onUnmappableCharacter(CodingErrorAction.REPORT); |
| 146 | + } |
| 147 | + return dec; |
| 148 | + } |
| 149 | + |
| 150 | + protected CharsetEncoder encoder() { |
| 151 | + if (enc == null) { |
| 152 | + enc = cs.newEncoder() |
| 153 | + .onMalformedInput(CodingErrorAction.REPORT) |
| 154 | + .onUnmappableCharacter(CodingErrorAction.REPORT); |
| 155 | + } |
| 156 | + return enc; |
| 157 | + } |
| 158 | +} |
| 159 | + |
0 commit comments