Example usage for java.lang IllegalArgumentException IllegalArgumentException

List of usage examples for java.lang IllegalArgumentException IllegalArgumentException

Introduction

In this page you can find the example usage for java.lang IllegalArgumentException IllegalArgumentException.

Prototype

public IllegalArgumentException(Throwable cause) 

Source Link

Document

Constructs a new exception with the specified cause and a detail message of (cause==null ?

Usage

From source file:Main.java

public static String[] shift(String[] array, int n) {
    if (n <= 0) {
        throw new IllegalArgumentException("n must a non-negative number.");
    }/*from w  w w  . j  a va2  s. c  o  m*/

    if (n >= array.length) {
        return new String[0];
    }

    String[] dest = new String[array.length - n];
    System.arraycopy(array, n, dest, 0, array.length - n);
    return dest;
}

From source file:Main.java

public static int byteArrayToUint32(byte[] src, int offset) throws IllegalArgumentException {
    int mask = 0xff;
    int temp = 0;
    int dest = 0;

    if (src.length < 4 + offset) {
        throw new IllegalArgumentException(
                "Failed to translate " + src.length + " bytes to uint32 with " + offset + " offset");
    }/*  www  . j a  va 2 s  . c  o  m*/

    for (int i = 0; i < 4; i++) {
        dest <<= 8;
        temp = src[offset + 3 - i] & mask;
        dest |= temp;
    }

    return dest;
}

From source file:Main.java

private static void checkByteOrderLittleEndian(final ByteBuffer buffer) {
    if (buffer.order() != ByteOrder.LITTLE_ENDIAN) {
        throw new IllegalArgumentException("ByteBuffer byte order must be little endian");
    }//  w w w .j  a v a2 s  . co  m
}

From source file:Main.java

@SuppressWarnings("rawtypes")
public static void notEmpty(Object obj, String message) {
    if (obj == null) {
        throw new IllegalArgumentException(message + " must be specified");
    }//from  w w w . j  a  v  a 2  s  .co m
    if (obj instanceof String && obj.toString().trim().length() == 0) {
        throw new IllegalArgumentException(message + " must be specified");
    }
    if (obj.getClass().isArray() && Array.getLength(obj) == 0) {
        throw new IllegalArgumentException(message + " must be specified");
    }
    if (obj instanceof Collection && ((Collection) obj).isEmpty()) {
        throw new IllegalArgumentException(message + " must be specified");
    }
    if (obj instanceof Map && ((Map) obj).isEmpty()) {
        throw new IllegalArgumentException(message + " must be specified");
    }
}

From source file:Main.java

/**
 * Throws an {@link java.lang.IllegalArgumentException} if called on the main thread.
 *///from ww  w. j  a v  a  2 s  .  c o m
public static void assertBackgroundThread() {
    if (!isOnBackgroundThread()) {
        throw new IllegalArgumentException("You must call this method on a background thread");
    }
}

From source file:Main.java

public static <InstanceType> InstanceType instantiate(Class<InstanceType> cls) throws IllegalArgumentException {
    try {/* w  w w . j a v a  2  s.c  om*/
        return cls.newInstance();
    } catch (Exception e) {
        throw new IllegalArgumentException(e);
    }
}

From source file:Main.java

public static int byteArrayToInt(byte[] bytes) throws IllegalArgumentException {
    if (bytes.length != (Integer.SIZE / Byte.SIZE)) {
        throw new IllegalArgumentException("Incorrect array size to convert to an int");
    }//from   w  w  w  .  ja  v a  2  s.c  o m
    ByteBuffer buffer = ByteBuffer.wrap(bytes);
    buffer.order(ByteOrder.BIG_ENDIAN);
    return buffer.getInt();
}

From source file:Main.java

public static int byteArrayToIntLE(byte[] bytes) throws IllegalArgumentException {
    if (bytes.length != (Integer.SIZE / Byte.SIZE)) {
        throw new IllegalArgumentException("Incorrect array size to convert to an int");
    }/*from  ww  w  . ja va  2s  . c  o  m*/
    ByteBuffer buffer = ByteBuffer.wrap(bytes);
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    return buffer.getInt();
}

From source file:Main.java

public static String getFileMd5String(File file) {
    if (!file.isFile())
        throw new IllegalArgumentException("only file can calculate MD5 value!");

    MessageDigest digest;/*from w  ww.jav a  2s  .c  om*/
    FileInputStream in = null;
    byte buffer[] = new byte[8192];
    int len;
    try {
        digest = MessageDigest.getInstance("MD5");
        in = new FileInputStream(file);
        while ((len = in.read(buffer)) != -1) {
            digest.update(buffer, 0, len);
        }
        BigInteger bigInt = new BigInteger(1, digest.digest());
        return bigInt.toString(16);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    } finally {
        try {
            if (in != null)
                in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static String urlencode(String[] params) {
    if (params.length % 2 != 0) {
        throw new IllegalArgumentException("Params must have an even number of elements.");
    }//w w w  .  java  2  s  .com

    String result = "";
    try {
        boolean firstTime = true;
        for (int i = 0; i < params.length; i += 2) {
            if (params[i + 1] != null) {
                if (firstTime) {
                    firstTime = false;
                } else {
                    result += "&";
                }
                result += URLEncoder.encode(params[i], "UTF-8") + "="
                        + URLEncoder.encode(params[i + 1], "UTF-8");
            }
        }
        result.replace("*", "%2A");
    } catch (UnsupportedEncodingException e) {
        return null;
    }

    return result;
}