Friday, February 20, 2009

Java Serialization Snippets

Object serialisation is the process of saving an object's state to a sequence of bytes. De-serialisation is the process of rebuilding the object from those bytes later.

The object to be serialised must implement the Serializable interface or inherit it. If you don't want fields in the object serialised mark them as transient.

Serializing an Object

//the object to serialize
Date date=new Date() ;

//the file to serialize to
String filename="date.ser";

//write the object to file
FileOutputStream fos=new FileOutputStream(filename);
BufferedOutputStream bos=new BufferedOutputStream(fos);
ObjectOutputStream outputStream=new ObjectOutputStream(bos);
outputStream.writeObject(date);
outputStream.flush();
outputStream.close();
De-Serializing an Object

//the file containing the serialized object
String filename="date.ser";

//read the object from the file
FileInputStream fis=new FileInputStream(filename);
BufferedInputStream bis=new BufferedInputStream(fis);
ObjectInputStream inputStream=new ObjectInputStream(bis);
Date date=(Date)inputStream.readObject();
inputStream.close();

//print the object
System.out.println(date);
Determining the Size of an Object
//the object to measure
Date date=new Date() ;

//write it out to a byte array
ByteArrayOutputStream baos=new ByteArrayOutputStream();
ObjectOutputStream oos=new ObjectOutputStream(baos);
oos.writeObject(date);
oos.close();
byte[] ba=baos.toByteArray();
baos.close();

//print size
System.out.println(ba.length);
or you can serialize the object to a file and measure the size of the file.

Friday, February 13, 2009

Happy 1234567890!

The epoch timestamp just passed 1234567890 and I saw it change!

Epoch Time

The time has come.

Tonight, at exactly 23:31:30 UTC, Epoch Time will reach 1234567890! You can watch the countdown here: http://coolepochcountdown.com/

Epoch Time (or Unix Time) can be defined as the number of seconds elapsed since 1 January 1970 00:00:00, not counting leap seconds. It is widely used on many computing systems.

We had an interesting time at work today, coming up with programs in different languages to watch Epoch Time tick by. Here are some of them:

Shell

 while true; do date +%s; sleep 1; done
Perl
 perl -le 'while(true){print time();sleep 1;}'
Haskell
import System.Time
import System.Posix

main :: IO ()
main = do { TOD epoch _ <- getClockTime
       ; putStrLn $ show epoch
       ; threadDelay 1000000
       ; main }
Groovy
groovy -e "while(true){println(new Date().time);sleep(1000)}"
Python
python -c \
  "while True: import time;print time.time();time.sleep(1)"
Java
import java.util.Date;

public class EpochTime {
  public static void main(String[] args) {
    while (true) {
      System.out.println(new Date().getTime() / 1000);
      try {
        Thread.sleep(1000);
      }
      catch (InterruptedException ignore) {
      }
    }
  }
}
HTML
<HTML><body>
<script language="javascript">
if(document.all){
  setInterval(function(){
  time=parseInt(new Date().getTime()/1000);
  document.body.innerText=time;
  },1000);
} else{
  setInterval(function(){
  time=parseInt(new Date().getTime()/1000);
  document.body.textContent=time;
  },1000);
}
</script></body></HTML>

Can you think of any more?

Happy 1234567890 day!

Monday, January 12, 2009

Set Google's new icon in Firefox's Search Box

This is how you can update your search box in Firefox to display Google's brand new favicon:

1. Go to the searchplugins directory, where you installed Firefox. In Windows XP, this will normally be: C:\program files\Mozilla Firefox\searchplugins

2. Open google.xml in your favourite text editor.

3. Change the Image tag to the following and save:

<Image width="16" height="16">data:image/x-icon;base64,AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA7PT7/3zF6/9Ptu//RbHx/0227/+Tzvb/9vv5/97h0f9JeBz/NHoA/z98Av9AfAD/PHsA/0F6AP8AAAAA/vz7/1+33/8Mp+z/FrHw/xWy8f8bs/T/Hqrx/3zE7v////7/t8qp/zF2A/87gwH/P4ID/z59AP8+egD/Q3kA/97s8v8botj/ELn3/wy58f8PtfL/D7Lw/xuz9P8vq+f/8/n///779v9KhR3/OYYA/0GFAv88hgD/QIAC/z17AP/0+/j/N6bM/wC07/8Cxf7/CsP7/wm+9v8Aqur/SrDb//7+/v///P7/VZEl/zSJAP87jQD/PYYA/0OBBf8+fQH///3//9Dp8/84sM7/CrDf/wC14/8CruL/KqnW/9ns8f/8/v//4OjX/z+GDf85kAD/PIwD/z2JAv8+hQD/PoEA/9C7pv/97uv////+/9Xw+v+w3ej/ls/e/+rz9///////+/z6/22mSf8qjQH/OJMA/zuQAP85iwL/PIgA/zyFAP+OSSL/nV44/7J+Vv/AkG7/7trP//7//f/9//7/6/Lr/2uoRv8tjQH/PJYA/zuTAP87kwD/PY8A/z2KAP89hAD/olkn/6RVHP+eSgj/mEgR//Ho3//+/v7/5Ozh/1GaJv8tlAD/OZcC/zuXAv84lAD/O5IC/z2PAf89iwL/OIkA/6hWFf+cTxD/pm9C/76ihP/8/v//+////8nav/8fdwL/NZsA/zeZAP83mgD/PJQB/zyUAf84jwD/PYsB/z6HAf+fXif/1r6s//79///58u//3r+g/+3i2v/+//3/mbiF/yyCAP87mgP/OpgD/zeWAP85lgD/OpEB/z+TAP9ChwH/7eHb/////v/28ej/tWwo/7tUAP+5XQ7/5M+5/////v+bsZn/IHAd/zeVAP89lgP/O5MA/zaJCf8tZTr/DyuK//3////9////0qmC/7lTAP/KZAT/vVgC/8iQWf/+//3///j//ygpx/8GGcL/ESax/xEgtv8FEMz/AALh/wAB1f///f7///z//758O//GXQL/yGYC/8RaAv/Ojlf/+/////////9QU93/BAD0/wAB//8DAP3/AAHz/wAA5f8DAtr///////v7+/+2bCT/yGMA/89mAP/BWQD/0q+D///+/////P7/Rkbg/wEA+f8AA/z/AQH5/wMA8P8AAev/AADf///7/P////7/uINQ/7lXAP/MYwL/vGIO//Lm3P/8/v//1dT2/woM5/8AAP3/AwH+/wAB/f8AAfb/BADs/wAC4P8AAAAA//z7/+LbzP+mXyD/oUwE/9Gshv/8//3/7/H5/zo/w/8AAdX/AgL6/wAA/f8CAP3/AAH2/wAA7v8AAAAAgAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAEAAA%3D%3D</Image>
4. Restart Firefox to see the new icon in your search box!

Here is a useful page you can use to convert files into base64 encoded strings: http://www.greywyvern.com/code/php/binary2base64

Related Posts:
How-to write your own Search Plugin for Firefox

Friday, January 02, 2009

fahd.blog in 2008

Happy 2009!
I'd like to wish everyone a great start to an even greater new year!

During 2008, I posted 44 new entries on fahd.blog, which is 52% more than in 2007. In addition to more posts, I am thrilled that I have more readers from all over the world too! Thanks for reading!

Top 5 posts of 2008:

I'm going to be writing a lot more this year, so stay tuned for more great techie tips, tricks and hacks! :)