Java tutorial
/** * Copyright (c) 2010-2019 Contributors to the openHAB project * * See the NOTICE file(s) distributed with this work for additional * information. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at * http://www.eclipse.org/legal/epl-2.0 * * SPDX-License-Identifier: EPL-2.0 */ package org.openhab.binding.miio.internal; import java.io.IOException; import java.net.URL; import org.apache.commons.io.IOUtils; import com.google.gson.JsonElement; import com.google.gson.JsonIOException; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.google.gson.JsonSyntaxException; /** * Utility class for common tasks within the Xiaomi vacuum binding. * * @author Marcel Verpaalen - Initial contribution * */ public final class Utils { /** * Convert a string representation of hexadecimal to a byte array. * * For example: String s = "00010203" returned byte array is {0x00, 0x01, 0x03} * * @param hex hex input string * @return byte array equivalent to hex string **/ public static byte[] hexStringToByteArray(String hex) { String s = hex.replace(" ", ""); int len = s.length(); byte[] data = new byte[len / 2]; for (int i = 0; i < len; i += 2) { data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16)); } return data; } private static final String HEXES = "0123456789ABCDEF"; /** * Convert a byte array to a string representation of hexadecimals. * * For example: byte array is {0x00, 0x01, 0x03} returned String s = * "00 01 02 03" * * @param raw byte array * @return String equivalent to hex string **/ public static String getSpacedHex(byte[] raw) { if (raw == null) { return ""; } final StringBuilder hex = new StringBuilder(3 * raw.length); for (final byte b : raw) { hex.append(HEXES.charAt((b & 0xF0) >> 4)).append(HEXES.charAt((b & 0x0F))).append(" "); } hex.delete(hex.length() - 1, hex.length()); return hex.toString(); } public static String getHex(byte[] raw) { if (raw == null) { return ""; } final StringBuilder hex = new StringBuilder(2 * raw.length); for (final byte b : raw) { hex.append(HEXES.charAt((b & 0xF0) >> 4)).append(HEXES.charAt((b & 0x0F))); } return hex.toString(); } public static JsonObject convertFileToJSON(URL fileName) throws JsonIOException, JsonSyntaxException, IOException { JsonObject jsonObject = new JsonObject(); JsonParser parser = new JsonParser(); JsonElement jsonElement = parser.parse(IOUtils.toString(fileName)); jsonObject = jsonElement.getAsJsonObject(); return jsonObject; } }