HexEncoder.java
/*
* Copyright (C) 2019 sw4j.org
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 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 Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.sw4j.tool.barcode.random.encoder.impl;
import java.util.Locale;
import javax.xml.bind.DatatypeConverter;
import org.sw4j.tool.barcode.random.encoder.ByteArrayEncoder;
/**
* <p>
* This class encodes a byte array as hex string.
* </p>
* @author Uwe Plonus <u.plonus@gmail.com>
*/
public class HexEncoder extends ByteArrayEncoder {
/**
* <p>
* The encoding name as constant.
* </p>
*/
public static final String TYPE = "hex";
/**
* <p>
* The default constructor.
* </p>
*/
public HexEncoder() {
}
/**
* <p>
* Return {@code true} if the {@code encoding} is hex (case insensitive).
* </p>
* @param encoding the encoding name to check.
* @return {@code true} if the name is hex.
*/
@Override
public boolean supports(final String encoding) {
return encoding != null && TYPE.equals(encoding.toLowerCase(Locale.ROOT));
}
/**
* <p>
* Return the byte array encoded as hex.
* </p>
* @param data the byte array to encode.
* @return the encoded data.
*/
@Override
public String encode(final byte[] data) {
return DatatypeConverter.printHexBinary(data).toLowerCase(Locale.ROOT);
}
}