Why doesn’t Java allow private members in interface?

From the Java Language Spec, (Access Control): “The Java programming language provides mechanisms for access control, to prevent the users of a package or class from depending on unnecessary details of the implementation of that package or class.” Access control is all about hiding implementation details. An interface has no implementation to hide.

HMAC-SHA256 Algorithm for signature calculation

Here is my solution: public static String encode(String key, String data) throws Exception { Mac sha256_HMAC = Mac.getInstance(“HmacSHA256”); SecretKeySpec secret_key = new SecretKeySpec(key.getBytes(“UTF-8”), “HmacSHA256”); sha256_HMAC.init(secret_key); return Hex.encodeHexString(sha256_HMAC.doFinal(data.getBytes(“UTF-8”))); } public static void main(String [] args) throws Exception { System.out.println(encode(“key”, “The quick brown fox jumps over the lazy dog”)); } Or you can return the hash encoded …

Read more

Evaluate list.contains string in JSTL

there is no built-in feature to check that – what you would do is write your own tld function which takes a list and an item, and calls the list’s contains() method. e.g. //in your own WEB-INF/custom-functions.tld file add this <?xml version=”1.0″ encoding=”ISO-8859-1″ ?> <!DOCTYPE taglib PUBLIC “-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN” “http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd”> …

Read more

Is there an equivalent to memcpy() in Java?

Use System.arraycopy() System.arraycopy(sourceArray, sourceStartIndex, targetArray, targetStartIndex, length); Example, String[] source = { “alpha”, “beta”, “gamma” }; String[] target = new String[source.length]; System.arraycopy(source, 0, target, 0, source.length); or use Arrays.copyOf() Example, target = Arrays.copyOf(source, length); java.util.Arrays.copyOf(byte[] source, int length) was added in JDK 1.6. The copyOf() method uses System.arrayCopy() to make a copy of the array, …

Read more