Example usage for java.io UnsupportedEncodingException getMessage

List of usage examples for java.io UnsupportedEncodingException getMessage

Introduction

In this page you can find the example usage for java.io UnsupportedEncodingException getMessage.

Prototype

public String getMessage() 

Source Link

Document

Returns the detail message string of this throwable.

Usage

From source file:Main.java

/**
 * Encode a String of URL into UTF-8, and handles exception by returning a default value.
 * @param url The URL to encode/* w  w  w . j  a  v  a2 s.co  m*/
 */
public static String urlEncode(String url, String def) {
    try {
        return URLEncoder.encode(url, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        Log.w("Transit URL encode", "Unsupported encoding: " + e.getMessage());
        return def;
    }
}

From source file:Main.java

public static String urldecode(String paramString1, String paramString2) {
    if (paramString1 == null)
        return "";
    try {/*  ww  w . j a va  2s.c  o m*/
        String str = URLDecoder.decode(paramString1, paramString2);
        return str;
    } catch (UnsupportedEncodingException localUnsupportedEncodingException) {
        throw new RuntimeException(localUnsupportedEncodingException.getMessage(),
                localUnsupportedEncodingException);
    }
}

From source file:Main.java

/**
 * Generates an encryption key for devices with API level lower than 18 using the
 * ANDROID_ID value as a seed.//from w w  w.j  a v a  2s .  c o m
 * In production scenarios, you should come up with your own implementation of this method.
 * Consider that your algorithm must return the same key so it can encrypt/decrypt values
 * successfully.
 *
 * @return The encryption key in a 32 byte long array.
 */
private static byte[] generateSecretKey() {
    byte[] key = new byte[32];
    byte[] android_id = null;

    try {
        android_id = Settings.Secure.ANDROID_ID.getBytes("UTF-8");
    } catch (UnsupportedEncodingException e) {
        Log.e(TAG, "generateSecretKey - " + e.getMessage());
    }

    for (int i = 0; i < key.length; i++) {
        key[i] = android_id[i % android_id.length];
    }

    return key;
}

From source file:Main.java

public static int getBytesLengthOfEncoding(String encoding, String str) {
    if (str == null || str.length() == 0)
        return 0;
    try {// w w  w.  j a v a  2  s. c o m
        byte bytes[] = str.getBytes(encoding);
        int length = bytes.length;
        return length;
    } catch (UnsupportedEncodingException exception) {
        System.err.println(exception.getMessage());
    }
    return 0;
}

From source file:Main.java

/**
 * Should always be able convert to/from UTF-8, so encoding exceptions are converted to an Error to avoid adding
 * throws declarations in lots of methods.
 * @param str String//from www  .ja v  a2  s .c  om
 * @return utf8 bytes
 * @see String#getBytes()
 */
public static byte[] getBytes(String str) {
    try {
        return str.getBytes("UTF8");
    } catch (java.io.UnsupportedEncodingException e) {
        throw new Error("String to UTF-8 conversion failed: " + e.getMessage());
    }
}

From source file:net.sourceforge.fenixedu.util.tests.ResponseExternalization.java

private static Response getResponse(String xmlResponse) {
    XMLDecoder decoder = null;// ww w  .ja v  a2  s. c  o  m
    try {
        decoder = new XMLDecoder(new ByteArrayInputStream(xmlResponse.getBytes(CharEncoding.UTF_8)));
    } catch (UnsupportedEncodingException e1) {
        logger.error(e1.getMessage(), e1);
    }
    Response response = null;
    try {
        response = (Response) decoder.readObject();
    } catch (ArrayIndexOutOfBoundsException e) {
        logger.error(e.getMessage(), e);
    }
    decoder.close();
    return response;
}

From source file:Main.java

/**
 * Should always be able convert to/from UTF-8, so encoding exceptions are converted to an Error to avoid adding
 * throws declarations in lots of methods.
 * @param bytes byte array//from  w w w . j a v a2  s  .  co m
 * @param offset starting offset in byte array
 * @param length length in byte array starting from offset
 * @return same as <code>new String(bytes, offset, length, "UTF8")</code>
 */
public static String getString(byte[] bytes, int offset, int length) {
    try {
        return new String(bytes, offset, length, "UTF8");
    } catch (java.io.UnsupportedEncodingException e) {
        throw new Error("UTF-8 to string conversion failed: " + e.getMessage());
    }
}

From source file:Main.java

public static String md5(String string) {
    if (isEmptyStr(string)) {
        return "";
    }/*from  w  w  w.  j av  a 2  s. c o  m*/
    try {
        return getMD5(string.getBytes("UTF-8"));
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}

From source file:Main.java

public static String urlencode(String paramString1, String paramString2) {
    if (paramString1 == null)
        return "";
    try {/*w  w  w  .j  a  v  a2 s.c  o  m*/
        String str = URLEncoder.encode(paramString1, paramString2);
        return str;
    } catch (UnsupportedEncodingException localUnsupportedEncodingException) {
        throw new RuntimeException(localUnsupportedEncodingException.getMessage(),
                localUnsupportedEncodingException);
    }
}

From source file:name.chengchao.myhttpclient.version3_1.HttpClient3Util.java

/**
 * ?url?ResponseBody,method=get/*ww  w .  j  a  v a  2 s. c  om*/
 * 
 * @param url exp:http://192.168.1.1:8080/dir/target.html
 * @param charsetName exp:http://192.168.1.1:8080/dir/target.html
 * @param timeout
 * @return String?
 * @throws UnsupportedEncodingException
 */
public static String getDataAsStringFromUrl(String url, String charsetName, int timeout) {
    if (StringUtils.isBlank(url)) {
        logger.error("url is blank!");
        return null;
    }
    if (StringUtils.isBlank(charsetName)) {
        charsetName = DEFAULT_CHARSETNAME;
    }
    byte[] responseBody = getDataFromUrl(url, timeout);
    if (null != responseBody) {
        try {
            return new String(responseBody, charsetName);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e.getMessage());
        }
    }
    return null;
}