Friday, January 11, 2013

What is base64 encoding ?



Ref : http://stackoverflow.com/questions/201479/what-is-the-use-of-base-64-encoding

It's a textual encoding of binary data where the resultant text has nothing but letters, numbers and the symbols "+", "/" and "=". It's a convenient way to store/transmit binary data over media that is specifically used for textual data.
But why Base-64? The two alternatives for converting binary data into text that immediately spring to mind are:
  1. Decimal: store the decimal value of each byte as three numbers: 045 112 101 037 etc. where each byte is represented by 3 bytes. The data bloats three-fold.
  2. Hexadecimal: store the bytes as hex pairs: AC 47 0D 1A etc. where each byte is represented by 2 bytes. The data bloats two-fold.
Base-64 maps 3 bytes (8 x 3 = 24 bits) in 4 characters that span 6-bits (6 x 4 = 24 bits). The result looks something like "TWFuIGlzIGRpc3Rpb...". Therefore the bloating is only a mere 4/3 = 1.3333333 times the original.

Thursday, December 30, 2010

sonar 2.4 and 2.5

Excited about Sonar's new features to set Architectural rules in version 2.4, and upcoming Track violations over time, differential views, manual code review in 2.5.

This surely will make my life little easier when doing daily code reviews.

http://www.sonarsource.org/roadmap/
http://www.infoq.com/news/2010/12/sonar-2.4

Thank you sonar source

Wednesday, December 22, 2010

Java Autoboxing - Auto-unboxing

Java 1.5 onward auto-boxing and auto-unboxing were added. This allows you to covert primitives and boxed types primitives back and forth with simple assignment vs calling special functions or constructors.

Lets look at some examples.
int primitiveInt = 1000;                   //primitive int
Integer wrapperInt = new Integer(1000);    // wrapper Integer, boxed type primitive.
Integer autoBoxedWrapperInteger = 1000;    // Autoboxing, primitive is autoboxed to Integer, an
//Object is bing created
int autoUnboxedPrimitiveInt = wrapperInt;  //Auto-unboxing, primitive is being assigned a value by auto-unoxing

//Comparing a primitive to boxed type, boxed type is autoboxed to primitive so the results is
// two primitives are being compared using === as opposed to 2 Objects are being ref compared
// using ==
System.out.println("primitiveInt == wrapperInt  :" +
(primitiveInt == wrapperInt ? true : false));

//Two object references are being compared using ==, therefore the result is false
System.out.println("wrapperInt == autoBoxedWrapperInteger  :" +
(wrapperInt == autoBoxedWrapperInteger ? true : false));

System.out.println("primitiveInt == autoBoxedWrapperInteger  :" +
(primitiveInt == autoBoxedWrapperInteger ? true : false));

System.out.println("primitiveInt == autoUnboxedPrimitiveInt  :" +
(primitiveInt == autoUnboxedPrimitiveInt ? true : false));


Output

primitiveInt == wrapperInt  :true

wrapperInt == autoBoxedWrapperInteger  :false
primitiveInt == autoBoxedWrapperInteger  :true
primitiveInt == autoUnboxedPrimitiveInt  :true

Here are some tips based on reading from Effective Java 2nd edition.

  • Prefer primitives over boxed primitives.

  • When you mix primitives and boxed primitive in single opeartion - boxed-primitive is auto-unboxed.

  • When mixing primitives and wrapper types in expression watch out for "==" comparision.

  • Performance hit due to autoboxing and unboxing, a new object is created when autoboxing is performed.

  • Unboxing can throw NullPointerException

Wednesday, December 15, 2010

HTTP Notes

Keep alive/Persistent Connections : HTTP 1.1 introduced keep alive to keep the connection between web client and server alive and reuse it to serve subsequent requests. In HTTP 1.0 a new connection had to be created for serving each request.

Servlet 3.0 Asynchronous Processing

Came across a great post on Servlet 3.0 asynchronous processing at http://www.javaworld.com/javaworld/jw-02-2009/jw-02-servlet3.html?page=1

Following are some notes based on the post.

Important thing to note is that it is not a fire and forget mechanism, as in put a request on a queue and return the response right away. The web client  has to wait however long the request processing take place.

It is a way to free up request handling server threads from long running code. Instead the work in offloaded to be handled by manually created thread(s) by application code.

Client response stream can be open for extended period of time, and server can write to response to implement "push" model.

 

 

 

 



Following are some of the important methods from ServletRequest API from Servlet 3.0 .

AsyncContext getAsyncContext()
Gets the AsyncContext that was created or reinitialized by the most recent invocation of startAsync() or startAsync(ServletRequest,ServletResponse) on this request.

boolean     isAsyncStarted() Checks if this request has been put into asynchronous mode.
boolean  isAsyncSupported() Checks if this request supports asynchronous operation.
AsyncContext     startAsync() Puts this request into asynchronous mode, and initializes its AsyncContext with the original (unwrapped) ServletRequest and ServletResponse objects.
AsyncContext     startAsync(ServletRequest servletRequest, ServletResponse servletResponse)
Puts this request into asynchronous mode, and initializes its AsyncContext with the given request and response objects.











Wednesday, November 17, 2010

HashMap Implementation notes

Implements Map interface

Permits null values and null key

None of the methods of HashMap are synchronized, so if you are adding or deleting keys - you must synchronize in multi threaded environment. Synchronize on some object, if  external object is not available use Collections.synchronizedMap(new HashMap())

Collections.synchronizedMap(new HashMap()) synchronizes all map operations using a internal mutex. However synchronization every single method is not enough, some client side locking is also required.

Iterators of HashMap are fail-fast when underlying map is modified (add/delete key) - ConcurrentModificationException is thrown.

Ordering of keys is not guaranteed to be same over time.

Access for get and put is time constant.

Max Capacity ~ 1.07 billiion

Default Initial capacity = 16

Uses hash function to disperse keys around.--

Uses Array to store elements.

Try and provide capacity  as part of initialization.

 

 

Monday, March 29, 2010

Executing Asynchronous Actions in a Web Application

Web applications need to provide instant response to end users, however not all requests can be served instantly due to a variety of constraints on infrastructure the application must live with. In an enterprise web applications there could a variety of long running tasks initiated by user request. To handle such task, the request could be queued for later processing and user is presented with a message to check status at a later time.  For some requests a part of necessary processing could be done instantly and remaining could be done at a later time.

Queuing a request requires adding extra infrastructure to web applications. Request could be queued in a single instance of share data structure across the application. Application threads running in background can read the queue data structure and act on queue messages. Rather than writing this framework/infrastructure enterprise class open source libraries could be utilized.  ActiveMQ provides a robust queuing solution and Apache Camel provides processing queued actions. Both libraries can be used in embedded or standalone fashion.

Architecture:

ServletContextListener initialize the ActiveMQ Broker and CamelContext, queues and routes are setup.

Have web action/controller write to ActiveMQ queue.

Write classes/camel routes to read from queue .