1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| public class BinaryByteToStringServiceImpl implements ByteToStringService { @Override public String encode(final byte[] bytes, final Encoding encoding) { return switch (encoding) { case HEX -> Hex.encodeHexString(bytes); case BASE_64 -> Base64.getEncoder().encodeToString(bytes); case ISO_8859_1 -> new String(bytes, StandardCharsets.ISO_8859_1); }; }
@Override public byte[] decode(final String str, final Encoding encoding) throws DecoderException { return switch (encoding) { case HEX -> Hex.decodeHex(str); case BASE_64 -> Base64.getDecoder().decode(str); case ISO_8859_1 -> str.getBytes(StandardCharsets.ISO_8859_1); }; } }
|