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

public static Serializable toSerializable(byte[] bytes) {
    if (bytes == null) {
        return null;
    }//from   ww w . j  a va2 s .co m
    try {
        final ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        final ObjectInputStream ois = new ObjectInputStream(bais);
        try {
            return (Serializable) ois.readObject();
        } finally {
            ois.close();
        }
    } catch (Exception e) {
        String msg = "Failed to convert the object to binary: bytes.length=" + bytes.length;
        throw new IllegalStateException(msg, e);
    }
}

From source file:Main.java

/**
 * Initializes the Cipher for use.//from ww  w.j  a v a2  s  . c  o m
 */
public static void initCipher(Cipher cipher, int mode, Key secretKey, AlgorithmParameterSpec parameterSpec) {
    try {
        if (parameterSpec != null) {
            cipher.init(mode, secretKey, parameterSpec);
        } else {
            cipher.init(mode, secretKey);
        }
    } catch (InvalidKeyException e) {
        throw new IllegalArgumentException("Unable to initialize due to invalid secret key", e);
    } catch (InvalidAlgorithmParameterException e) {
        throw new IllegalStateException("Unable to initialize due to invalid decryption parameter spec", e);
    }
}

From source file:cn.hxh.springside.utils.EncodeUtils.java

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

From source file:com.ning.arecibo.util.timeline.times.TimesAndSamplesCoder.java

public static int getSizeOfTimeBytes(final byte[] timesAndSamples) {
    final DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(timesAndSamples));
    try {/*w  w w  .ja  va 2s  . c  o m*/
        return inputStream.readInt();
    } catch (IOException e) {
        throw new IllegalStateException(String.format(
                "Exception reading timeByteCount in TimelineChunkMapper.map() for timesAndSamples %s",
                Hex.encodeHex(timesAndSamples)), e);
    }
}

From source file:com.yahoo.pulsar.broker.ServiceConfigurationUtils.java

public static String unsafeLocalhostResolve() {
    try {/*from w w  w  .  j a v  a 2s  .co m*/
        return InetAddress.getLocalHost().getHostName();
    } catch (UnknownHostException ex) {
        LOG.error(ex.getMessage(), ex);
        throw new IllegalStateException("Failed to resolve localhost name.", ex);
    }
}

From source file:Main.java

public static byte[] toBinary(Serializable obj) {
    if (obj == null) {
        return null;
    }/* w ww .j  a  v a2s.  co  m*/
    if (obj instanceof byte[]) {
        return (byte[]) obj;
    }
    try {
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        final ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(obj);
        try {
            return baos.toByteArray();
        } finally {
            oos.close();
        }
    } catch (Exception e) {
        String msg = "Failed to convert the object to binary: obj=" + obj;
        throw new IllegalStateException(msg, e);
    }
}

From source file:Main.java

/**
 * Parses the given file into an XML {@link Document}.
 * @param file The file to parse.//from   w  w  w  .j  a v  a  2s  .  c o  m
 * @return The created XML {@link Document}.
 */
public static Document parseXml(File file) {
    if (!file.exists()) {
        throw new IllegalArgumentException("File " + file + " does not exist.");
    }
    try {
        DocumentBuilder docBuilder = createDocumentBuilder();
        return docBuilder.parse(file);
    } catch (SAXException | IOException e) {
        throw new IllegalStateException("Unable to parse XML file " + file, e);
    }
}

From source file:Main.java

private static Object evaluateXPath(String path, Node e, QName type) {
    try {/*  www.  j a  v a 2 s. c o  m*/
        XPathExpression expr = compiledString.get(path);
        if (expr == null) {
            expr = xpath.compile(path);
            compiledString.put(path, expr);
        }
        return expr.evaluate(e, type);
    } catch (XPathExpressionException e1) {
        throw new IllegalStateException("Wrong XPATH expression: " + path, e1);
        //SHOULD NEVER HAPPEN IF THE EXPRESSIONS ARE WELL MADE INSIDE THE CODE
    }
}

From source file:com.lll.util.EncodeUtils.java

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

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

public static String readStringFromResource(String resourcePath) {
    try {/*from w ww .j  a v  a2 s  .com*/
        return IOUtils.toString(readFromResource(resourcePath), StandardCharsets.UTF_8);
    } catch (IOException e) {
        throw new IllegalStateException(format("Cannot read from resource %s", resourcePath), e);
    }
}