Tuesday, 17 January 2017

UTF-8 with and without BOM issue



What Client want ?
             We have client which generate tones of report from project data in csv format for their BI(Business Intell) group.
File downloaded form server is encoded with UTF-8 without BOM (Byte Order Marker), that is fine and also stranded way. A standard saying is do not add BOM for UTF-8.
        But when same report file(.csv) is opened with MS Excel, it is not able to detect encoding of file because absence of BOM for UTF-8, and French character in file shows some random symbol. 

What we did ? 
We told them to open same file notepad++, because it detect the encoding and mark encode to UTF-8-BOM so it can open in Excel with proper encoding for special character. That is working fine and also good solution. 

What Client Replied ?
Client replied that they dont want open same file notepad++, instead it should open in Excel with correct encoding.

This is the story to dig up little into BOM (Byte Order Marker)
and  JVM s UTF-8 behavior 

What JVM Expect for UTF-8 Encoding ?
Whenever you read file using java with UTF-8, JVM is not excepting BOM character at starting of InputStream. If their is any BOM character present, JVM consider it as data of file. 
JVM not expecting BOM for UTF-8, which standard from UNICODE    

        Finally we have added BOM for UTF-8 encoding for file. And removing BOM character while reading the file. This is fix we have given to client.
        But may be in future client will come to us saying that they have one tools which consumes csv format data and while reading data,tool getting weird character at staring of file. Which is because client tool expecting file with UTF-8 encoding without BOM and tool considering BOM Character as data of input file.

But client is client, nobody beat them. 

Lazy Intialization (From Effective Java)

Most common said is to avoid lazy initialization if not required.
Lazy initialization play an important while performance is key.
As mentioned in effective java, there are two ways to do better lazy initialization.
  • Double checked Lock (For Instance Field)
  • Lazy Initialization holder class Idiom (For static field)
 Double checked Lock (For Instance Field)
                   
        In simple word, avoid synchronization on whole getter method, instead of that first check for null or default value(In case of primitives) and if required initialization then only use synchronization.
Following example depict simple use of double checked lock

private volatile FiledType filed;
FiledType getFiledType() {
    FiledType result = filed; //Use at least once pre Initialized 
    if (result == null) { // First check (No locking)
        synchronized(this) {
            if (result == null) { //Second checking (With locking)
                result = filed = computeFieldValue();
            }
        }
    }
    return result;
} 


Lazy Initialization holder class Idiom (For static field)
           It is also called as initialization on demand holder class idiom.
It guarantee that a class will not be initialized until it used(JLS 12.4.1).
Following code depict the same:-

//Lazy initialization holder class idiom for static filed initialization
private static class FiledHolder {
    static final FiledType filed =  computeFieldValue();
}
FiledType getFiledType() {
    return FiledHolder.filed; //Here class is initialized
}