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

 ALL


  NIO vs IO in Java

Java 1.4 provides a new API for handling IO -- NIO. This is a non-blocking and buffer oriented IO API.Below are main differences between the NIO and IO in Java.IONIOStream orientedBuffer orientedBlocking IONon-blocking ION/AUsing selectorStream oriented vs Buffer orientedThe main difference is that IO is stream oriented where the data is read byte by byte and the data will not be buffered normally.This means there is no pointer to move forward and backward in the stream. If there is a need to move forward and backward of the stream, the data needs to be explicitly copied to a buffer. While Jav...

5,298 0       JAVA IO NIO


  Using Java keytool programmatically

Java provides a command line tool to access and operate different keystore which store keys and certificates. This tool is named keytool and is located at \bin. On command line, you can issue below command to generate a keystore named mytest.jks which contains a private key and certificate chain.keytool -genkeypair -alias mykey -keyalg RSA -sigalg SHA256withRSA -dname CN=Java -storetype JKS -keypass password -keystore mytest.jks -storepass passwordSometimes, in testing purpose, we may want to issue these command in our applications instead of start a command line terminal. This is...

23,768 14       JAVA KEYTOOL


  Different types of keystore in Java -- Windows-MY

Windows-MY is a type of keystore on Windows which is managed by the Windows operating system. It stores the user keys and certificates which can be used to perform cryptographic operations such as signature verification, data encryption etc. Since it's a kind of native keystore, Java doesn't have a general API to access it.To help Java applications access the keys and certificates stored in Windows-MY keystore, Java provides a separate API -- SunMSCAPI. The SunMSCAPI provider is layered on top of CAPI and helps Java platform applications access CAPI cryptographic services using ...

35,056 6       JAVA KEYSTORE WINDOWS-MY SUNMSCAPI


  Using keytool to create certificate chain

JDK provides a command line tool -- keytool to handle key and certificate generation. This tool has a set of options which can be used to generate keys, create certificates, import keys, install certificate and export certificates etc. In this tutorial, we will show how to create certificate chain using keytool. If you want to understand how to create certificate chain programmably, please refer to Generate certificate in Java -- Certificate chain.To begin, we first generate a key pair which will be used as the CA, ts private key will be used to sign the certificate it issues.keytool -gen...

48,151 1       JAVA CERTIFICATE CERTIFICATE CHAIN KEYTOOL


  Signature sign/verification demo in Java

Digital signature is commonly used in areas where data authentication and integrity are required. It is extremely important to have signature while transferring sensitive data from one peer to other peers through network since there might be malicious applications or man-in-the-middle attacks which may alter the data along the way.Java provides some APIs to generate and verify digital signature. One important class is Signature. When generating the signature, a private key needs to be passed to initSign()When verifying the signature, a public key needs to be passed to initVerify()Java use...

10,866 0       JAVA SECURITY SIGNATURE


  Using MemoryMappedBuffer to handle large file in Java

When handling large files, it will largely affect the process speed while using traditional FileInputStream, FileOutputStream or RandomAccessFile since they trigger lots of read and write operations. In Java NIO, a new way of handling large file is introduced which is to use MmeoryMappedBuffer to create memory mapped file.Memory-mapped I/O uses the filesystem to establish a virtual memory mapping from user space directly to the applicable filesystem pages. With a memory-mapped file, you can pretend that the entire file is in memory and that you can access it by simply treating it as a very lar...

8,503 0       JAVA IO NIO


  Use Java ThreadLocal with caution

According to Oracle documentation, ThreadLocal is a class provides thread-local variables. These variables differ from their normal counterparts in that each thread that accesses one (via its get or set method) has its own, independently initialized copy of the variable. ThreadLocal instances are typically private static fields in classes that wish to associate state with a thread. In short, ThreadLocal variables are variables belong to a thread, not a class or an instance of a class.One common use of ThreadLocal is when you want to access some non thread-safe objects in threads without using ...

22,915 0       JAVA MEMORY LEAK THREADLOCAL


  A HTTPS client and HTTPS server demo in Java

In this post, I will create a HTTPS server and HTTPS client demo which can establish HTTPS communication between a server and a client using Java. This should be very useful when we want to test our understanding about SSL communication. We will use both a detailed SSL client and a simple HttpsURLConnection as the HTTPS client.Before creating the actual HTTPS server and HTTPS client, we first need to generate the keystore and truststore to be used by the server and client. To generate the keystore, we can follow instructions at Different types of keystore in Java -- JKS. In this demo, we ...

69,285 5       JAVA SSL DEMO HTTPS