Archive

Archive for the ‘Core Java’ Category

Java Split String with Pipe (|) Parameter

March 31, 2010 1 comment

Problem: Try to split String which is having delimiter as pipe(|)

public class TestSplit
{
  public static void main( String[] args )
  {
     String DELIMETER = "@";
    // String DELIMETER = "|";
    

    String line = "field1" + DELIMETER + "field2";

    String[] tempList = line.split( DELIMETER );
    int sizeOfArray = tempList.length;

    System.out.println( "sizeOfArray==>" + sizeOfArray );

    for( int i = 0; i < tempList.length; i++ ) {
      System.out.println( i + "==>" + tempList[i] );
    }
  }
}

Why Pipe(|) wont work as delimeter?

Java Syntax
String[] split(String regex)
http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html
Logical operators
X|Y Either X or Y

-o-

The code for the static initializer is exceeding the 65535 bytes limit

March 10, 2010 Leave a comment

Come across some auto generated code. It is showing following error….. : )

The code for the static initializer is exceeding the 65535 bytes limit

How to fix it?

Solution: Make sure that it wont happen or split the code .. : )

Categories: Core Java

Phone Number Validation / Formatting

March 8, 2010 Leave a comment

Problem: How to format phone numbers for different countries?

We dont have any ready to use library at this time. We need to develop our own library.

International Country Codes for phone lines is provided by ITU.
http://www.itu.int/publ/T-SP-E.164D-2009/en

Reference: http://www.answers.com/topic/telephone-number-1
-o-

Inheritance abuse in Java

February 18, 2010 Leave a comment

Problem: I am going through the code, Written by somebody else.

public interface FileManipulator
public abstract class FileRepository implements FileManipulator
public class CommonFileRepository extends FileRepository
public class Project1FileRepository extends CommonFileRepository
Some where else they encapsulated Project1FileRepository class.

Given requirement: Program need to read license agreement for project only once. It is not going to change across the project.

Solution:

public class ProjectLicense
{
public static String getLicenseText()
{
// Read the license text from file and return.
// Better to put in static variable and avoid reading again.
}
}

-o-

Interesting facts about Inheritance depth in Java.

http://www.javaspecialists.eu/archive/Issue121.html
http://www.javaspecialists.eu/records/index.jsp

JMX Port issue

December 30, 2009 Leave a comment

Use following commands to check open/established ports. This is giving snapshot. in between connections can be opened and closed.

>netstat -an
>netstat -an |find /i “listening”

Options to pass to JVM
-Dcom.sun.management.jmxremote=true
-Dcom.sun.management.jmxremote.port=1616
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false
-o-

Categories: Core Java Tags:

java.lang.IllegalArgumentException: SSL

December 28, 2009 Leave a comment

Problem:
INFO: Interceptor has thrown exception, unwinding now
java.lang.IllegalArgumentException: SSL
at com.sun.net.ssl.internal.ssl.ProtocolVersion.valueOf(ProtocolVersion.java:133)
at com.sun.net.ssl.internal.ssl.ProtocolList.(ProtocolList.java:38)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.setEnabledProtocols(SSLSocketImpl.java:2075)

Fix:

Before: parameters.setSecureSocketProtocol(“SSL”);

After: parameters.setSecureSocketProtocol(“SSLv3″);

InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty

December 15, 2009 Leave a comment

Problem Statement: Getting exception while trying to connecting remote https system.

Caused by: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty
	at java.security.cert.PKIXParameters.setTrustAnchors(PKIXParameters.java:183)
	at java.security.cert.PKIXParameters.<init>(PKIXParameters.java:103)
	at java.security.cert.PKIXBuilderParameters.<init>(PKIXBuilderParameters.java:87)
	at sun.security.validator.PKIXValidator.<init>(PKIXValidator.java:57)
	... 28 common frames omitted

Solution: System is not getting keystore from relative path.
Provide absolutepath and keystore name. It should be resolved.

//System.setProperty("javax.net.ssl.trustStore", relativePath+key_name); //Not working

System.setProperty("javax.net.ssl.trustStore", absolutePath+key_name); //Working

-o-

HashMap deep copy Vs simple copy

November 2, 2009 Leave a comment

Question: Many times we ask same question again and again, year after year.
The best way to understand is run the following code.


import java.util.HashMap;
import java.util.Iterator;

public class HashMapTest
{
  public static void main( final String[] args )
  {
    testReference();
    testDeepCopy();
  }

  private static void testDeepCopy()
  {
    System.out.println( "Deep Copy Test" );
    final HashMap<Integer, String> data1 = new HashMap<Integer, String>();
    data1.put( new Integer( "1" ), "one" );
    data1.put( new Integer( "2" ), "two" );

    final HashMap<Integer, String> data2 = new HashMap<Integer, String>( data1 );
    printHashMap( data1, "data1" );
    printHashMap( data2, "data2" );

    System.out.println( "Remove data in data2" );

    data1.remove( new Integer( 2 ) );
    printHashMap( data1, "data1" );
    printHashMap( data2, "data2" );
  }

  private static void testReference()
  {
    System.out.println( "Simple Copy Test" );
    final HashMap<Integer, String> data1 = new HashMap<Integer, String>();
    data1.put( new Integer( "1" ), "one" );
    data1.put( new Integer( "2" ), "two" );

    final HashMap<Integer, String> data2 = data1;
    printHashMap( data1, "data1" );
    printHashMap( data2, "data2" );

    System.out.println( "Remove data in data2" );

    data1.remove( new Integer( 2 ) );
    printHashMap( data1, "data1" );
    printHashMap( data2, "data2" );
  }

  private static void printHashMap( final HashMap<Integer, String> map,
    final String message )
  {
    System.out.println( "---- " + message + " Begin ----" );
    final Iterator iterator = map.keySet().iterator();

    while ( iterator.hasNext() ) {
      final Integer key = (Integer)iterator.next();
      final String value = map.get( key );

      System.out.println( key + " " + value );
    }
    System.out.println( "---- " + message + " End ----" );
  }

}

Result:
Simple Copy Test
—- data1 Begin —-
1 one
2 two
—- data1 End —-
—- data2 Begin —-
1 one
2 two
—- data2 End —-
Remove data in data2
—- data1 Begin —-
1 one
—- data1 End —-
—- data2 Begin —-
1 one
—- data2 End —-
Deep Copy Test
—- data1 Begin —-
1 one
2 two
—- data1 End —-
—- data2 Begin —-
1 one
2 two
—- data2 End —-
Remove data in data2
—- data1 Begin —-
1 one
—- data1 End —-
—- data2 Begin —-
1 one
2 two
—- data2 End —-

Categories: Core Java

Java Swing – Telugu

July 21, 2009 3 comments

Just thought of writing small application in Java to display Telugu fonts. Here is the code for your reference.

Java Telugu Example

Java Telugu Example


import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JTextArea;

/**
* Copy gautami.ttf from windows\fonts to \java_160_10\jre\lib\fonts\
*
* References:
*
* http://unicode.org/charts/PDF/U0C00.pdf
* http://java.sun.com/javase/technologies/core/basic/intl/faq.jsp
* http://java.sun.com/j2se/1.4.2/docs/guide/intl/fontprop.html
*
* Load font from file
* http://www.java2s.com/Code/Java/2D-Graphics-GUI/Loadfontfromttffile.htm
*
* http://www.roseindia.net/java/example/java/swing/graphics2D/font-paint.shtml
*
* How to get unicode for telugu from lekhini? type in lekhini. copy text to
* following location. Get unicode.
*
* http://rishida.net/tools/conversion/
*
* When you type in text area, we will get squares. Use Baraha IME. Then you can
* type in telugu.
*/
public class JavaTeluguDemo extends JFrame {

private static final long serialVersionUID = 123123123L;
private static final Font fontTeluguGautami32 = new Font("gautami",
Font.PLAIN, 32);
private static final Font fontTeluguGautami10 = new Font("gautami",
Font.PLAIN, 22);
private static String telugu = "\u0C05\u0C2E\u0C4D\u0C2E";
private static String teluguHeading = "\u0C1C\u0C3E\u0C35\u0C3E \u0C24\u0C46\u0C32\u0C41\u0C17\u0C41";

public JavaTeluguDemo() {
JTextArea taDisplay = new JTextArea(telugu);
taDisplay.setFont(fontTeluguGautami32);
this.getContentPane().add(taDisplay);
}

public static void main(String[] args) {
// We can't change title font in default system look and feel.
JFrame.setDefaultLookAndFeelDecorated(true);

JFrame frame1 = new JavaTeluguDemo();
frame1.setSize(400, 150);
frame1.setVisible(true);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame1.getLayeredPane().getComponent(1).setFont(fontTeluguGautami10);
frame1.setTitle(teluguHeading);

}

}

Difference betwen two object arrays

July 10, 2009 Leave a comment

Problem Statement: We have two different sets of objects. We would like to find the objects which are different in given two sets.

Solution: Use Java-Diff from http://www.incava.org/ . I am suspecting issues with its accuracy. Make sure that comparators are working for objects.

-o-

Categories: Core Java Tags:
Follow

Get every new post delivered to your Inbox.