This is an automated email from the ASF dual-hosted git repository. ebourg pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-configuration.git
commit 3970e1ae1066c828a1cdabd5dd2443079ddc02dd Author: Emmanuel Bourg <ebo...@apache.org> AuthorDate: Fri Oct 25 11:21:45 2024 +0200 Replace org.apache.commons.codec.binary.Hex with a trimmed down local copy --- .../apache/commons/configuration2/plist/Hex.java | 92 ++++++++++++++++++++++ .../plist/PropertyListConfiguration.java | 1 - src/main/javacc/PropertyListParser.jj | 2 - 3 files changed, 92 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/apache/commons/configuration2/plist/Hex.java b/src/main/java/org/apache/commons/configuration2/plist/Hex.java new file mode 100644 index 00000000..b4c86124 --- /dev/null +++ b/src/main/java/org/apache/commons/configuration2/plist/Hex.java @@ -0,0 +1,92 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.configuration2.plist; + +import java.text.ParseException; + +/** + * Trimmed down version of the Apache Commons Codec Hex class. + * To be replaced with java.util.HexFormat in Java 17. + */ +class Hex { + + /** Used to build output as hex. */ + private static final char[] DIGITS_LOWER = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; + + /** + * Converts an array of characters representing hexadecimal values into an array of bytes of those same values. The + * returned array will be half the length of the passed array, as it takes two characters to represent any given + * byte. An exception is thrown if the passed char array has an odd number of elements. + * + * @param data An array of characters containing hexadecimal digits + * @return A byte array containing binary data decoded from the supplied char array. + * @throws ParseException Thrown if an odd number of characters or illegal characters are supplied + */ + public static byte[] decodeHex(char[] data) throws ParseException { + byte[] out = new byte[data.length >> 1]; + + int len = data.length; + if ((len & 1) != 0) { + throw new ParseException("Odd number of characters.", 0); + } + + // two characters form the hex value. + for (int i = 0, j = 0; j < len; i++) { + int f = toDigit(data[j], j) << 4; + j++; + f |= toDigit(data[j], j); + j++; + out[i] = (byte) (f & 0xFF); + } + return out; + } + + /** + * Converts an array of bytes into an array of characters representing the hexadecimal values of each byte in order. + * The returned array will be double the length of the passed array, as it takes two characters to represent any + * given byte. + * + * @param data a byte[] to convert to hexadecimal characters + * @return A char[] containing lower-case hexadecimal characters + */ + public static char[] encodeHex(byte[] data) { + char[] out = new char[data.length << 1]; + + for (int i = 0, j = 0; i < data.length; i++) { + out[j++] = DIGITS_LOWER[(0xF0 & data[i]) >>> 4]; + out[j++] = DIGITS_LOWER[0x0F & data[i]]; + } + return out; + } + + /** + * Converts a hexadecimal character to an integer. + * + * @param ch A character to convert to an integer digit + * @param index The index of the character in the source + * @return An integer + * @throws ParseException Thrown if ch is an illegal hexadecimal character + */ + protected static int toDigit(char ch, int index) throws ParseException { + int digit = Character.digit(ch, 16); + if (digit == -1) { + throw new ParseException("Illegal hexadecimal character " + ch + " at index " + index, 0); + } + return digit; + } +} diff --git a/src/main/java/org/apache/commons/configuration2/plist/PropertyListConfiguration.java b/src/main/java/org/apache/commons/configuration2/plist/PropertyListConfiguration.java index 86f0957c..5d71823a 100644 --- a/src/main/java/org/apache/commons/configuration2/plist/PropertyListConfiguration.java +++ b/src/main/java/org/apache/commons/configuration2/plist/PropertyListConfiguration.java @@ -29,7 +29,6 @@ import java.util.List; import java.util.Map; import java.util.TimeZone; -import org.apache.commons.codec.binary.Hex; import org.apache.commons.configuration2.BaseHierarchicalConfiguration; import org.apache.commons.configuration2.Configuration; import org.apache.commons.configuration2.FileBasedConfiguration; diff --git a/src/main/javacc/PropertyListParser.jj b/src/main/javacc/PropertyListParser.jj index 317cdf14..3e516714 100644 --- a/src/main/javacc/PropertyListParser.jj +++ b/src/main/javacc/PropertyListParser.jj @@ -31,8 +31,6 @@ import java.util.ArrayList; import org.apache.commons.configuration2.HierarchicalConfiguration; import org.apache.commons.configuration2.tree.ImmutableNode; -import org.apache.commons.codec.binary.Hex; - /** * JavaCC based parser for the PropertyList format. *