Today's Question:  What does your personal desk look like?        GIVE A SHOUT

 ALL


  The Greatest Regex Trick Ever (Simplified)

There is a post which is really hot recently which showcased a best ever regular expression trick. In this post, it provides a trick which can be adapted to situations where one wants to match some patterns desired and exclude the patterns undesired. In simple, the trick is :Pattern not desired | (Pattern desired)Then taking the group 1 match of the capturing group of matched. This group will contain the strings matching the desired patterns.Have to say this trick is really neat and brilliant. Here is a simple explanation on how this works. The above regular expression will first try to m...

4,702 0       JAVA PROGRAMMING REGULAR EXPRESSION


  Set file permission in Java

Prior to Java 6,  there is no good solution at the Java level to provide file permission management. You need to implement your own native methods or call the Runtime.exec() to invoke the system routine such as chmod in LInux. Since Java 6, Java introduces a set of methods which can be used to set the file permission at Java level. These methods include:File.setReadable(boolean);File.setWritable(boolean);FIle.setExecutable(boolean);File.setReadable(boolean, boolean);File.setWritable(boolean, boolean);FIle.setExecutable(boolean, boolean);Here File.setReadable(boolean, true) is the same as ...

26,645 0       JAVA FILE PERMISSION POSIX LEARNJAVA


  Java Cipher encryption/decryption example

In Java, Cipher is the API for doing data encryption/decryption. Many cryptographic algorithms such as AES, DES, RC4 etc can be specified when creating Cipher instance. The Cipher instance calls the underlying algorithm specific implementation to do the actual encryption/decryption. Before doing the encryption/decryption, a key needs to be created and it will be used to do the encryption/decryption. A sample program for performing all these is :import java.security.Key;import javax.crypto.Cipher;import javax.crypto.KeyGenerator;public class CipherSample { public static void main(String[] ...

30,138 1       JAVA EXAMPLE JAVA SECURITY CIPHER SAMPLE


  ByteBuffer in Java

ByteBuffer is introduced in java.nio since Java 1.4. It provides a way of representing raw structured data such as from a file or from network. It enables fast access of underlying data compared to traditional ways like byte[]Prior to Java 1.4, if you want to represent a structured raw data, you need to create a byte[] and then having a set of checks to delimit the byte array to get the expected tokens.There are three ways to create a ByteBuffer:Wrapping an exiting array by calling ByteBuffer.wrap();Creating an empty buffer with capacity by calling ByteBuffer.allocate();Creating an direct empt...

3,112 0       JAVA BYTEBUFFER ALLOCATION


  Generate certificate from cert file in Java

A certificate is often used to prove the identity of a server. The certificate will contain information such as the subject and issuer of the certificate. It will also contain the validation date of the certificate. A certificate is often exported to an external cert file which is transferred over the internet. We will often see its use in SSL communication which provides secure communication between two entities.In this post, we will show how to read the data from an external certificate file and generate a X509 certificate object with the data. This object can then be used to conduct other o...

22,725 2       JAVA EXAMPLE X509 PKCS12 CERTIFICATEFACTORY


  Arrays.equals() vs MessageDigest.isEqual()

Both Arrays.equals() and MessageDigest.isEqual() are used to compare the equality of two arrays. They can be interchangeably in many cases. However, they do have some differences which lead to different use cases in real applications.One difference is that the arrays passed to MessageDigest.isEqual() cannot be null while it's ok for Arrays.equals().The one major difference between these two methods is that Arrays.equals() is not time-constant while MessageDigest.isEqual() is time-constant. This means that when comparing two arrays, the arrays are compared byte by byte, Arrays.equals() will ret...

19,052 0       JAVA SECURITY ARRAYS.EQUAL() MESSAGEDIGEST.ISEQUAL()


  Recursive class initialization in Java

When a Java class is referenced and initialized, it has to go through the loading and linking first. Once the loading and linking complete successfully. The class will be initialized. The static variables and constant variables will be initialized during this process. Once the class is initialized, it is ready for use.If when class A is initialized and it is referencing a class B, the class B will also get initialized. But what will happen if class B is referencing class A as well? This is called recursive class initialization. Will there be a deadlock if this case happens? No, JVM will take c...

12,695 0       JAVA JVM CLASS INITIALIZATION STATIC FINAL


  String.length() vs String.getBytes().length in Java

In Java, String.length() is to return the number of characters in the string, while String.getBytes().length is to return the number of bytes to represent the string with the specified encoding. By default, the encoding will be the value of system property file.encoding, the encoding name can be set manually as well by calling System.setProperty("file.encoding", "XXX"). For example, UTF-8, Cp1252. In many cases, String.length() will return the same value as String.getBytes().length, but in some cases it's not the same.String.length() is the number of UTF-16 code units needed to represent the s...

111,045 0       JAVA STRING ENCODING SAMPLE UTF8