<commit_before>/*
 * #%L
 * Bitrepository Protocol
 * %%
 * Copyright (C) 2010 - 2012 The State and University Library, The Royal Library and The State Archives, Denmark
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 2.1 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 *
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
 * #L%
 */
package org.bitrepository.common.utils;

import org.bitrepository.common.ArgumentValidator;

import javax.xml.bind.DatatypeConverter;
import java.util.Locale;

/**
 * Utility class for handling encoding and decoding of base16 bytes.
 */
public class Base16Utils {
    private Base16Utils() {
    }

    /**
     * Decodes a base16 encoded byte set into a human-readable string.
     *
     * @param data The data to decode.
     * @return The decoded data, or null if a null is given.
     */
    public static String decodeBase16(byte[] data) {
        if (data == null) {
            return null;
        }
        return DatatypeConverter.printHexBinary(data).toLowerCase(Locale.ROOT);
    }

    /**
     * Encoding a hex string to base16.
     *
     * @param hexString The string to encode to base16.
     * @return The string encoded to base16.
     */
    public static byte[] encodeBase16(String hexString) {
        ArgumentValidator.checkNotNullOrEmpty(hexString, "String hexString");
        // TODO Java 17 has HexFormat.of().parseHex(s) - consider using instead
        return DatatypeConverter.parseHexBinary(hexString);
    }
}<commit_msg>Base16Utils' new usage of DatatypeConverter swapped for Apache Commons Codec-implementation - should have better performance<commit_after>/*
 * #%L
 * Bitrepository Protocol
 * %%
 * Copyright (C) 2010 - 2012 The State and University Library, The Royal Library and The State Archives, Denmark
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 2.1 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 *
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
 * #L%
 */
package org.bitrepository.common.utils;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
import org.bitrepository.common.ArgumentValidator;

/**
 * Utility class for handling encoding and decoding of base16 bytes.
 */
public class Base16Utils {
    private Base16Utils() {
    }

    /**
     * Decodes a base16 encoded byte set into a human-readable string.
     *
     * @param data The data to decode.
     * @return The decoded data, or null if a null is given.
     */
    public static String decodeBase16(byte[] data) {
        if (data == null) {
            return null;
        }
        return Hex.encodeHexString(data);
    }

    /**
     * Encodes a hex string to a set of base16 bytes.
     *
     * @param hexString The string to encode to base16.
     * @return The string encoded to base16.
     */
    public static byte[] encodeBase16(String hexString) {
        ArgumentValidator.checkNotNullOrEmpty(hexString, "String hexString");
        // TODO Java 17 has HexFormat.of().parseHex(s) - consider using instead
        try {
            return Hex.decodeHex(hexString);
        } catch (DecoderException e) {
            throw new IllegalArgumentException("Bad hex-string '" + hexString + "': " + e.getMessage());
        }
    }
}
{'fraction_non_alphanumeric': 0.08516547095579727, 'fraction_numerical': 0.015274242073594076, 'mean_word_length': 2.9944547134935307, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 1}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6081271', 'n_tokens_mistral': 1190, 'n_tokens_neox': 1129, 'n_words': 475}