Example usage for java.lang IllegalStateException IllegalStateException

List of usage examples for java.lang IllegalStateException IllegalStateException

Introduction

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

Prototype

public IllegalStateException(String message, Throwable cause) 

Source Link

Document

Constructs a new exception with the specified detail message and cause.

Usage

From source file:Main.java

/**
 * Constructs a new Cipher.//from   w  w  w .  j a  va2s. co m
 */
public static Cipher newCipher(String algorithm) {
    try {
        return Cipher.getInstance(algorithm);
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalArgumentException("Not a valid encryption algorithm", e);
    } catch (NoSuchPaddingException e) {
        throw new IllegalStateException("Should not happen", e);
    }
}

From source file:Main.java

/**
 * Invokes the Cipher to perform encryption or decryption (depending on the
 * initialized mode).//  www  . ja v  a 2 s  .  co m
 */
public static byte[] doFinal(Cipher cipher, byte[] input, int inputOffSet, int inputLen) {
    try {
        return cipher.doFinal(input, inputOffSet, inputLen);
    } catch (IllegalBlockSizeException e) {
        throw new IllegalStateException("Unable to invoke Cipher due to illegal block size", e);
    } catch (BadPaddingException e) {
        throw new IllegalStateException("Unable to invoke Cipher due to bad padding", e);
    }
}

From source file:com.gooddata.util.ResourceUtils.java

public static <T> T readObjectFromResource(Class testClass, String resourcePath, Class<T> objectClass) {
    notNull(objectClass, "objectClass");

    try {/* ww w. ja v  a 2 s .c o m*/
        return OBJECT_MAPPER.readValue(readFromResource(resourcePath, testClass), objectClass);
    } catch (IOException e) {
        throw new IllegalStateException(
                format("Cannot read class %s from resource %s", objectClass, resourcePath), e);
    }
}

From source file:Main.java

public static <T> T fromJson(@NonNull String content, @NonNull Type type) throws IllegalStateException {
    try {/*from  w  w  w . ja  v  a  2 s  .  c om*/
        return sGson.fromJson(content, type);
    } catch (JsonSyntaxException e) {
        throw new IllegalStateException(JSON_PARSE_ERROR + content, e);
    }
}

From source file:Main.java

/**
 * Execute the given runnable code dedicated to Swing using the Event Dispatcher Thread (EDT)
 * And waits for completion/*from w w  w  .  j a  va2 s  .c  om*/
 * @param runnable runnable code dedicated to Swing
 * @throws IllegalStateException if any exception occurs while the given runnable code executes using EDT
 */
public static void invokeAndWaitEDT(final Runnable runnable) throws IllegalStateException {
    if (isEDT()) {
        // current Thread is EDT, simply execute runnable:
        runnable.run();
    } else {
        // If the current thread is interrupted, then use invoke later EDT (i.e. do not wait):
        if (Thread.currentThread().isInterrupted()) {
            invokeLaterEDT(runnable);
        } else {
            try {
                // Using invokeAndWait to be in sync with the calling thread:
                SwingUtilities.invokeAndWait(runnable);

            } catch (InterruptedException ie) {
                // propagate the exception because it should never happen:
                throw new IllegalStateException(
                        "SwingUtils.invokeAndWaitEDT : interrupted while running " + runnable, ie);
            } catch (InvocationTargetException ite) {
                // propagate the internal exception :
                throw new IllegalStateException(
                        "SwingUtils.invokeAndWaitEDT : an exception occured while running " + runnable,
                        ite.getCause());
            }
        }
    }
}

From source file:com.apabi.qrcode.utils.EncodeUtils.java

/**
 * Hex?./*  w  ww.  j  a  v  a 2 s  .co m*/
 */
public static byte[] hexDecode(final String input) {
    try {
        return Hex.decodeHex(input.toCharArray());
    } catch (DecoderException e) {
        throw new IllegalStateException("Hex Decoder exception", e);
    }
}

From source file:Main.java

/**
 * Convert a bean to XML format using JAXB.
 * /*from ww w .  jav  a2  s  .  c o  m*/
 * @param bean
 *            The bean to marshal as XML.
 * @param bc
 *            Additional classes to register on the JAXB context for
 *            marshalling.
 * @return An XML representation of the bean.
 */
public static <T> String marshal(T bean, Class<?>... bc) {
    assert bean != null;
    Class<?>[] bind;
    if (bc.length > 0) {
        bind = new Class<?>[bc.length + 1];
        bind[0] = bean.getClass();
        for (int i = 0; i < bc.length; i++)
            bind[i + 1] = bc[i];
    } else
        bind = new Class<?>[] { bean.getClass(), };
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(bind);
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(bean, baos);
    } catch (JAXBException e) {
        throw new IllegalStateException("Error marshalling", e);
    }
    return new String(baos.toByteArray());
}

From source file:Main.java

/**
 * Convert a bean to XML format using JAXB.
 * /*from www  . ja va 2s . co  m*/
 * @param bean
 *            The bean to marshal as XML.
 * @param bc
 *            Additional classes to register on the JAXB context for
 *            marshalling.
 * @return An XML representation of the bean.
 */
public static <T> String marshal(T bean, Class<?>... bc) {
    assert bean != null;
    Class<?>[] bind;
    if (bc.length > 0) {
        bind = new Class<?>[bc.length + 1];
        bind[0] = bean.getClass();
        for (int i = 0; i < bc.length; i++)
            bind[i + 1] = bc[i];
    } else
        bind = new Class<?>[] { bean.getClass(), };
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(bind);
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, new Boolean(true));
        marshaller.marshal(bean, baos);
    } catch (JAXBException e) {
        throw new IllegalStateException("Error marshalling", e);
    }
    return new String(baos.toByteArray());
}

From source file:Main.java

/**
 * Convert XML data to a bean using JAXB.
 * /*from   w w w. j  av  a  2 s .co m*/
 * @param b
 *            The bean, represented as XML.
 * @param implClass
 *            The implementation class of the bean.
 * @param bc
 *            Additional classes to add to the JAXB context.
 * @return The bean, unmarshalled from the XML data using JAXB.
 */
public static <T> T unmarshal(String b, Class<T> implClass, Class<?>... bc) {
    Class<?>[] bind;
    if (bc.length > 1) {
        bind = new Class<?>[bc.length + 1];
        bind[0] = implClass;
        for (int i = 0; i < bc.length; i++) {
            bind[i + 1] = bc[i];
        }
    } else {
        bind = new Class<?>[] { implClass, };
    }
    ByteArrayInputStream bais = new ByteArrayInputStream(b.getBytes());
    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(bind);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        return implClass.cast(unmarshaller.unmarshal(bais));
    } catch (JAXBException e) {
        throw new IllegalStateException("Error unmarshalling", e);
    }
}

From source file:com.linkedin.databus2.schemas.utils.Utils.java

public static byte[] md5(byte[] bytes) {
    try {/*ww w . ja va 2 s. c  o m*/
        MessageDigest digest = MessageDigest.getInstance("md5");
        return digest.digest(bytes);
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException("This can't happen.", e);
    }
}