Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Monday, May 12, 2014

Java Asynchronous NIO File Reading Example

If you want to read a file using Java AsynchronousFileChannel, following sample code might help you to get started.
package com.ruchira.samples;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousFileChannel;
import java.nio.channels.CompletionHandler;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

public class AsyncReader {
    public static void main(String[] args) throws
            IOException, InterruptedException, ExecutionException {
        String fileName = "/Users/ruchira/Downloads/README.md";
        completionHandler(fileName);
        future(fileName);
    }

    private static void completionHandler(String fileName)
            throws InterruptedException, IOException, ExecutionException {
        final Thread current = Thread.currentThread();
        final StringBuilder content = new StringBuilder();
        Path path = Paths.get(fileName);
        final AsynchronousFileChannel ch = AsynchronousFileChannel.open(path);
        final ByteBuffer buf = ByteBuffer.allocate(1024);
        ch.read(buf, 0, 0,
                new CompletionHandler() {
                    public void completed(Integer result, Integer length) {
                        if (result == -1) {
                            try {
                                ch.close();
                            } catch (IOException e) {
                                //ignored
                            }
                            current.interrupt();
                            return;
                        }
                        buf.flip();
                        content.append(new String(buf.array()));
                        /*while (buf.hasRemaining()) {
                            content.append((char) buf.get());
                        }*/
                        buf.clear();
                        ch.read(buf, length, length + result, this);
                    }
                    
                    public void failed(Throwable exc, Integer length) {
                        System.out.println("Failure: " + exc.toString());
                    }
                }
        );

        System.out.println("You can use this thread for another task");
        try {
            current.join();
        } catch (InterruptedException e) {
            //ignored
        }
        System.out.println(content);
    }

    private static void future(String fileName)
            throws InterruptedException, IOException, ExecutionException {
        StringBuilder content = new StringBuilder();

        int start = 0;
        Path path = Paths.get(fileName);
        AsynchronousFileChannel ch = AsynchronousFileChannel.open(path);
        ByteBuffer buf = ByteBuffer.allocate(1024);
        Future result = ch.read(buf, start);
        int length;
        while ((length = result.get()) != -1) {
            while (!result.isDone()) {
                System.out.println("You can use this thread for another task");
                Thread.sleep(100);
            }
            buf.flip();
            content.append(new String(buf.array()));
            /*while (buf.hasRemaining()) {
                content.append((char) buf.get());
            }*/
            buf.clear();
            result = ch.read(buf, start);
            start += length;
        }
        ch.close();
        System.out.println(content);
    }
}

Thursday, April 28, 2011

How to increase JVM Heap Size and Perm Gen space

If you are dealing with applications which have large code bases, then you might quickly run out of Java memory. Probably you might get either java.lang.OutOfMemoryError: PermGen or java.lang.OutOfMemoryError: Java heap space. In order to fix that, just having sufficient memory in your machine is not sufficient. So you need to increase the JVM Heap size and Permanent Generation space.

Setting the following environment variable before starting your application, you can increase them easily.

Unix
export JAVA_OPTS="-Xms512m -Xmx1024m -XX:MaxPermSize=1024m"

Windows
set JAVA_OPTS="-Xms512m -Xmx1024m -XX:MaxPermSize=1024m"

Thursday, July 8, 2010

How to Create Client/Server Keystores using Java Keytool

Here I have described how to create client/server keystores which can be used to secure Axsi2 webservices and invoke Axis2 secured webservices. i.e. It can be easily used with any WSO2 Product to experience security scenarios.More detailed explanation on creating client/server keystores using openssl including Certificate Authority(CA) Requests, can be found at http://wso2.org/library/174.

Java keytool stores the keys and certificates in a keystore, protected by a keystore password. Further, it protects private key again with another password. A Java keystore contains private-public key pair and multiple trusted certificate entries. All entries in a keystore are referred by aliases. Both private key and self signed public key is referred by one alias while any other trusted certificates are referred by different individual aliases.

As the first step, let's create a keystore for server. In order to do it, execute following command in a terminal. "server" in the following command corresponds to the private key/self signed public key certificate alias in the keystore while "server.jks" is the name of the creating keystore file.
keytool -genkey -alias server -keyalg RSA -keystore server.jks
when you execute the above command it will first prompt you to specify a password which is corresponded to the keystore password. Then it will prompt several questions. You can give answers that you wish. At the end it will ask for a password again, which will be used to secure the generated private key.
Enter keystore password:  
Re-enter new password: 
What is your first and last name?
[Unknown]:  Ruchira Wageesha
What is the name of your organizational unit?
[Unknown]:  Mashup Server
What is the name of your organization?
[Unknown]:  WSO2
What is the name of your City or Locality?
[Unknown]:  Ahangama
What is the name of your State or Province?
[Unknown]:  Southern
What is the two-letter country code for this unit?
[Unknown]:  LK
Is CN=Ruchira Wageesha, OU=Mashup Server, O=WSO2, L=Ahangama, ST=Southern, C=LK correct?
[no]: yes
Enter key password for 
(RETURN if same as keystore password):
Once you successfully completed this, java keytool will create a file named "server.jks". In the same way, you can create a client keystore named "client.jks" with the alias "client" using following command.
keytool -genkey -alias client -keyalg RSA -keystore client.jks
Now, you have two files named client.jks and server.jks. You can view the content of these keystore files using the following command. Replacess "ruchira" with the keystore password you entered while creating the keystore.
keytool -list -v -keystore server.jks -storepass ruchira
This will list something like this.
Keystore type: JKS
Keystore provider: SUN

Your keystore contains 1 entry

Alias name: server
Creation date: Jul 8, 2010
Entry type: PrivateKeyEntry
Certificate chain length: 1
Certificate[1]:
Owner: CN=Ruchira Wageesha, OU=Mashup Server, O=WSO2, L=Ahangama, ST=Southern, C=LK
Issuer: CN=Ruchira Wageesha, OU=Mashup Server, O=WSO2, L=Ahangama, ST=Southern, C=LK
Serial number: 4c356225
Valid from: Thu Jul 08 10:59:09 IST 2010 until: Wed Oct 06 10:59:09 IST 2010
Certificate fingerprints:
MD5:  60:0B:48:0D:DB:56:8C:68:8C:2D:94:4A:D6:DA:04:B8
SHA1: A7:CE:57:10:70:87:C1:2C:C0:9D:1D:90:8C:BB:69:B6:66:26:97:13
Signature algorithm name: SHA1withRSA
Version: 3


*******************************************
*******************************************
The next step is, getting server's self signed public key certificate and storing it in client's keystore. And getting and storing client's self signed public key certificate in server's keystore. In order to do that, first we need to export both server and client public key certificates into files. Using the following command, you can export server's public key certificate into server.cert file and client's public key certificate into client.cert file.
keytool -export -file server.cert -keystore server.jks -storepass ruchira -alias server
keytool -export -file client.cert -keystore client.jks -storepass ruchira -alias client
Now you have server.cert and client.cert. You can use following commands to view certificate contents.
keytool -printcert -v -file server.cert
keytool -printcert -v -file client.cert
As the last step, we need to import server.cert into client keystore and client.cert into server keystore. As I mentioned earlier, each entry of a Java Keystore is stored against an alias. So, we need to specify aliases here, which will be used to refer the certificates that we are going to store.
keytool -import -file client.cert -keystore server.jks -storepass ruchira -alias client
Above command will store client's self signed public key certificate(client.cert) in server.jks against the alias "client". So, using "client" alias on server.jks, we can refer client's certificate anytime. Likewise, following command will store server.cert within client.jks against the alias "server".
keytool -import -file server.cert -keystore client.jks -storepass ruchira -alias server
After all, please view the content of both keystore again using following commands.
keytool -list -v -keystore server.jks -storepass ruchira
keytool -list -v -keystore client.jks -storepass ruchira
It will give you something like bellow for server.jks
Keystore type: JKS
Keystore provider: SUN

Your keystore contains 2 entries

Alias name: server
Creation date: Jul 8, 2010
Entry type: PrivateKeyEntry
Certificate chain length: 1
Certificate[1]:
Owner: CN=Ruchira Wageesha, OU=Mashup Server, O=WSO2, L=Ahangama, ST=Southern, C=LK
Issuer: CN=Ruchira Wageesha, OU=Mashup Server, O=WSO2, L=Ahangama, ST=Southern, C=LK
Serial number: 4c3562a0
Valid from: Thu Jul 08 11:01:12 IST 2010 until: Wed Oct 06 11:01:12 IST 2010
Certificate fingerprints:
MD5:  AB:77:72:F1:0D:09:55:E3:B6:D3:DC:A6:4D:D4:39:36
SHA1: D7:C1:60:5C:7E:34:40:A9:0B:E4:2C:65:6C:E0:79:7C:EE:37:A7:19
Signature algorithm name: SHA1withRSA
Version: 3


*******************************************
*******************************************


Alias name: client
Creation date: Jul 8, 2010
Entry type: trustedCertEntry

Owner: CN=Ruchira Wageesha, OU=Mashup Server, O=WSO2, L=Ahangama, ST=Southern, C=LK
Issuer: CN=Ruchira Wageesha, OU=Mashup Server, O=WSO2, L=Ahangama, ST=Southern, C=LK
Serial number: 4c356225
Valid from: Thu Jul 08 10:59:09 IST 2010 until: Wed Oct 06 10:59:09 IST 2010
Certificate fingerprints:
MD5:  60:0B:48:0D:DB:56:8C:68:8C:2D:94:4A:D6:DA:04:B8
SHA1: A7:CE:57:10:70:87:C1:2C:C0:9D:1D:90:8C:BB:69:B6:66:26:97:13
Signature algorithm name: SHA1withRSA
Version: 3


*******************************************
*******************************************

something like below for client.jks
Keystore type: JKS
Keystore provider: SUN

Your keystore contains 2 entries

Alias name: server
Creation date: Jul 8, 2010
Entry type: trustedCertEntry

Owner: CN=Ruchira Wageesha, OU=Mashup Server, O=WSO2, L=Ahangama, ST=Southern, C=LK
Issuer: CN=Ruchira Wageesha, OU=Mashup Server, O=WSO2, L=Ahangama, ST=Southern, C=LK
Serial number: 4c3562a0
Valid from: Thu Jul 08 11:01:12 IST 2010 until: Wed Oct 06 11:01:12 IST 2010
Certificate fingerprints:
MD5:  AB:77:72:F1:0D:09:55:E3:B6:D3:DC:A6:4D:D4:39:36
SHA1: D7:C1:60:5C:7E:34:40:A9:0B:E4:2C:65:6C:E0:79:7C:EE:37:A7:19
Signature algorithm name: SHA1withRSA
Version: 3


*******************************************
*******************************************


Alias name: client
Creation date: Jul 8, 2010
Entry type: PrivateKeyEntry
Certificate chain length: 1
Certificate[1]:
Owner: CN=Ruchira Wageesha, OU=Mashup Server, O=WSO2, L=Ahangama, ST=Southern, C=LK
Issuer: CN=Ruchira Wageesha, OU=Mashup Server, O=WSO2, L=Ahangama, ST=Southern, C=LK
Serial number: 4c356225
Valid from: Thu Jul 08 10:59:09 IST 2010 until: Wed Oct 06 10:59:09 IST 2010
Certificate fingerprints:
MD5:  60:0B:48:0D:DB:56:8C:68:8C:2D:94:4A:D6:DA:04:B8
SHA1: A7:CE:57:10:70:87:C1:2C:C0:9D:1D:90:8C:BB:69:B6:66:26:97:13
Signature algorithm name: SHA1withRSA
Version: 3


*******************************************
*******************************************
If everything went well, you might have successfully created server.jks and client.jks which can be used to secure Axis2 Services and access those secured services.

You can also find a summary of Java keytool commands at http://ruchirawageesha.blogspot.com/2010/07/java-keytool-keystore-commands.html

Wednesday, July 7, 2010

Java Keytool Keystore Commands

Create Keystore, Keys and Certificate Requests
  • Generate a Java keystore and key pair
    keytool -genkey -alias mydomain -keyalg RSA -keystore keystore.jks -storepass password
  • Generate a certificate signing request (CSR) for an existing Java keystore
    keytool -certreq -alias mydomain -keystore keystore.jks -storepass password -file mydomain.csr
  • Generate a keystore and self-signed certificate
    keytool -genkey -keyalg RSA -alias selfsigned -keystore keystore.jks -storepass password -validity 360
Import Certificates
  • Import a root or intermediate CA certificate to an existing Java keystore
    keytool -import -trustcacerts -alias root -file Thawte.crt -keystore keystore.jks -storepass password
  • Import a signed primary certificate to an existing Java keystore
    keytool -import -trustcacerts -alias mydomain -file mydomain.crt -keystore keystore.jks -storepass password
  • Import New CA into Trusted Certs
    keytool -import -trustcacerts -file /path/to/ca/ca.pem -alias CA_ALIAS -keystore $JAVA_HOME/jre/lib/security/cacerts
Export Certificates
  • Export a certificate from a keystore
    keytool -export -alias mydomain -file mydomain.crt -keystore keystore.jks -storepass password
Check/List/View
  • Check a stand-alone certificate
    keytool -printcert -v -file mydomain.crt
  • Check which certificates are in a Java keystore
    keytool -list -v -keystore keystore.jks -storepass password
  • Check a particular keystore entry using an alias
    keytool -list -v -keystore keystore.jks -storepass password -alias mydomain
  • List Trusted CA Certs
    keytool -list -v -keystore $JAVA_HOME/jre/lib/security/cacerts
Delete Certificates
  • Delete a certificate from a Java Keytool keystore
    keytool -delete -alias mydomain -keystore keystore.jks -storepass password
Change Passwords
  • Change a Java keystore password
    keytool -storepasswd -new new_storepass -keystore keystore.jks -storepass password
  • Change a private key password
    keytool -keypasswd -alias client -keypass old_password -new new_password -keystore client.jks -storepass password

Thursday, June 25, 2009

How to Increase JVM(Java) Heap Size

If you're building a big project using ant/maven then your jvm heap size may not sufficient and give you a java.lang.OutOfMemoryError: PermGen space exception. Increasing of maven/ant heap size might not sufficient enough for that and an increase of jvm heap size might also needed. When you're starting an application through command line, you can do it by specifying
java -Xms32m -Xmx128m your_application
it will be a temporary increase of heap size and if you want to increase it permanently set the following environment variable as needed.
export _JAVA_OPTIONS="-Xms64m -Xmx128m"
-Xms64m is the startup size(64MB) and -Xmx128m is the maximum size(128MB)

Saturday, January 10, 2009

Add, Multiply, Divide Numbers in Fractional Format

Here is a way to do fractional calculations in Java. This might be useful when you want to get the exact values for some arithmetic operations like division. You can keep the result as a fractional number and to the operations what ever you like. i.e. add, subtract, multiply and divide.

Here are two classes named Fraction.java and Util.java. As an example you can create the fractional number 7/8 using the constructor Fraction("8", "7"). There are many other constructors too in the Fraction class. If you want to get 10 as a fractional number, i.e. 10/1, you can use the contructor new Fraction("10").

In order to add two fractional numbers a and b, you can use Util.add(a, b) method. Likewise you can to subtraction, multiplication and division. Please refer the code, it might be useful to understand the usage.

Fraction.java
public class Fraction {
   
    private Long denominator;
    private Long numerator;
    /** Creates a new instance of Fraction */
    public Fraction(String denominator, String numerator) {
        this(new Long(denominator), new Long(numerator));
    }
   
    public Fraction(Long denominator, Long numerator) {
        long gcd = Util.gcd(denominator, numerator);       
        this.denominator = denominator / gcd;
        this.numerator = numerator /gcd;
    }
   
    public Fraction(String number) {
        this.denominator = new Long("1");
        this.numerator = new Long(number);
    }
   
    public Fraction(long number) {
        this.denominator = new Long("1");
        this.numerator = new Long(number);
    }

    public Long getDenominator() {
        return denominator;
    }

    public Long getNumerator() {
        return numerator;
    }
}
Util.java
public class Util {
   
    /** Creates a new instance of Util */
    public Util() {
    }
  
    public static long gcd(Long a, Long b) {      
        if (b==0)
            return a;
        else
            return gcd(b, a % b);
    }
   
    public static long lcm(Long a, Long b) {
        return (a/gcd(a, b))*b;
    }
   
    // (a + b)
    public static Fraction add(Fraction a, Fraction b) {
       
        Long denominator = lcm(a.getDenominator(), b.getDenominator());
        Long numerator = a.getNumerator()*(denominator/a.getDenominator()) +
                b.getNumerator()*(denominator/b.getDenominator());
        return new Fraction(denominator, numerator);
    }
   
    // (a - b)
    public static Fraction substract(Fraction a, Fraction b) {
       
        Long denominator = lcm(a.getDenominator(), b.getDenominator());
        Long numerator = a.getNumerator()*(denominator/a.getDenominator()) -
                b.getNumerator()*(denominator/b.getDenominator());
        return new Fraction(denominator, numerator);
    }
   
    // (a * b)
    public static Fraction multiply(Fraction a, Fraction b) {
        return new Fraction(a.getDenominator() * b.getDenominator(), a.getNumerator() * b.getNumerator());
    }
   
    // (a / b)
    public static Fraction divide(Fraction a, Fraction b) {
        return new Fraction(a.getDenominator() * b.getNumerator(), a.getNumerator() * b.getDenominator());
    }
   
}

How to find gcd and lcm in Java

Here is the easiest and efficient way to get the Greatest Common Divisor(gcd) and Least Common Multiple(lcm) in Java. This algorithm could be used for any language. Here I have used Long data type. You can use integer as needed.
public static long gcd(long a, long b) {      
    if (b==0)
        return a;
    else
        return gcd(b, a % b);
}
public static long lcm(long a, long b) {
    return (a / gcd(a, b)) * b;
}