Archive for December, 2007

Java tips

How to compress/decompress a bytes array? Or even better with an object?

Google: java + compress + object
Compressing and Decompressing Data using Java
Search for Compressing Object header.

ByteArrayOutputStream baos = new
ByteArrayOutputStream();
GZIPOutputStream gz = new GZIPOutputStream(baos);
ObjectOutputStream oos = new
ObjectOutputStream(gz);
oos.writeObject(obj1);
oos.writeObject(obj2);
oos.close();
baos.toByteArray(); // this is the compressed data of your objects
fos.close();

Google: java + compress + bytes array
-> Compressing a Byte Array (Java Developers Almanac Example)
-> Decompressing a Byte Array (Java Developers Almanac Example)


How to convert an object to bytes array and convert back from bytes array to object?
(so as to write/read an object stored in a binary file)

To convert an object to bytes array, see the code below from How can I convert any Java Object into byte array? And byte array to file object

public static byte[] getBytes(Object obj) throws java.io.IOException{

      ByteArrayOutputStream bos = new ByteArrayOutputStream();


      ObjectOutputStream oos = new ObjectOutputStream(bos);


      oos.writeObject(obj);


      oos.flush();


      oos.close();


      bos.close();


      byte [] data = bos.toByteArray();


      return data;


  }

To convert a byte array back to an object see the code in here Java Programming [Archive] – convert byte array to object

// .. get data into an array named "byteArray"
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(byteArray));
// .. let's say you have a class named "Person"
Person person = (Person) ois.readObject();
// close the input stream (probably not necessary in this case)
ois.close();


Java – binary file – random access?
The tittle is the google search keyword
Source I found:

  1. Physics Simulation and Java – Lecture 9A: Binary File I/O Example
  2. Java I/O skip to the "RandomAccess Files" section.

What u may need as I did:

  • FileInputStream – DataInputStream
    • readInt – writeInt
    • readLong – writeLong
  • RandomAccessFile f
    • seek(0)
    • seek(f.length)
    • getFilePointer //current position of file pointer


How to use an external jar file jarExt in a jar file myJar?
Very easy, you just pay attention when jar-build the myJar file, you add the Class-Path attribute to you manifest file to the location of jarExt as below:
    Class-Path: path/to/your/external/jar the/2nd/jar/space/separated
That’s all. Don’t know why Sun mention this in their tutorial not so bold for such a big need like this.


How to compile java source file?
Create your source files, putting them into the folders according to their package (e.g class packageName.any.className must be put in packageName/any/className.java).
From your current folder somewhere, compile your code by calling :
    javac p2ur.source.files -classpath p2ur.including_packageName.folder -d p2ur.storing.compiledOutput.folder
, note:
  p2ur is shorthand of path.to.your
  if your code do not using any class other than the ones from the JDK, then forget the classpath argument, which means:

    javac
p2ur.source.files 
-d p2ur.storing.compiledOutput.folder
  if you skip also the -d argument, i.e javac p2ur.source.files , then the output will have the same folder with the source.
, special note:
  if your code use other class not in JDK, then without providing classpath
the compiler can’t say what the classes you used are! So, you have to
tell JDK where those used classes are stored. Remember to give your
storing path as the classpath-rule below.
  JDK actually try to find those classes in your current folder when no classpath is
given. This explains why some other refered-by-your-code classes stored
in the same folder with your code will be auto compiled when you
compile your code without giving the classpath.
  Classpath may contain many paths separated by colon in Linux, semi-colon in Windows.

How to run java source file?
When you have a .class bytecode file with the main method, you wanna run it? Just call:
    java p2ur.bytecode.file
Well, it’s just that simple when your code just used the
JDK classes. What if you use the other classes (you wrote yourself, use
from others, v.v..) ? Then, JDK will look in the classpath, which default (when not provided) is the current folder where you call java. You add classpath when call java as below.
    java -classpath path.to.your.referred.classes p2ur.bytecode.file
Must notice to the way JDK works with the classpath. If some any.package.class.name is need to find, then it will iterate every path, called cpEntry, in classpath and see if cpEntry/any/package/class/name exists. Note that if you class has package, full package of the class must be called when call java. i.e the whole any.package.class.name not only the class.name.
Also notice that classpath must be preceding of the bytecode or it won’t work.

Having the compiled classes, now how to easily distribute?
The answer is to use jar files. The target is to combine a set of classes to a single file. To do this we call :
    jar cfm jarOutputFile manifestFile -C classesFolder .
We need to add context for the above command to understand it. Let’s say we have :

  • 2 classes : pack.age1.class1 and pack.age1.class2
    accordingly stored in src/pack/age1/class1.java and src/pack/age2/class2.java
    then compiled into bin/pack/age1/class1.class and bin/pack/age2/class2.class
    You may need to look back if you don’t know how to do this.

The above command will create the jar file stored at jarOutputFile which include all the files recuresively stored in the classesFolder. Here, the working folder must be the bin/ folder. You can have this by adding -C path/to/bin <theChosenInBin> , where theChosenInBinn is the files in bin/ folder to be chosen to add to the jar file. Cause we here add all files, the command should be -C path/to/bin .
(note the dot, means the bin/ folder itself, making JDK auto look for the files inside recursively)

Having the jar file, we can call to run it by :
    javar -jar jarOutputFile
but this require a starting point, i.e the main method elsewhere among the classes should be chosen
The main method chosen to be executed is the one indicated in the manifest file as:
  Main-class: pack.ageX.classX (enter here)
  (end of file here)

, note :
  again require the last new-line character ending for the entry, without this the entry won’t be realized by JDK
  and that the classname listed as main class, not the .class or .jave.


How to print a percentage?

Double perc = Double.valueOf(<ur percentage computation here>);
System.out.print(String.format("%1$.2f", new Object[]{perc})


About classpath
How JDK find your referent classes – the classpath-rule

Both the compiler and the JVM construct the path to your .class files by adding the package name
to the class path. For example, if the package name is

com.example.graphics

and your class path is

<path_two>\classes 

then the compiler and JVM look for .class files in

<path_two>\classes\com\example\graphics.

A class path may include several paths, separated by a semicolon (Windows) or colon (Unix). By default,
the compiler and the JVM search the current directory.

How to write a letter of motivation (cover letter – statement of purpose)

source: I lost it. Will find it later.

How to write a "letter of motivation", you can also write a cover letter
for employment. Make sure to include relevant details of job post that you are applying for.

"Letter of motivation" seems to be a literal translation
of the French "lettre de motivation". In the English-speaking world,
the document is called a cover letter. Remember that it is a letter, so
the layout must be correct:
Here are some tips to keep in mind when writing a cover letter.

• A "letter of motivation" or cover letter (CL) is your
opportunity to "sell" yourself, i.e. convince the institution or
company that they need to have you. Do not be modest: when applying for
a job, explain clearly why you think you are suited for the position.

• The goal of a CL is to provide a picture of your background and
goals that will persuade the admission committee to accept you.

Recruiters
expect you to show that what you have matches what they want. Remember,
this is a question of interpretation, and the secret of writing a good
CL lies in using your qualifications and experience to show that you
are the best person for the position. (This means, of course, that you need to modify your CL for each position you are applying for. The same goes for your CV, of course.)

• Beyond the particular skills required by each program, a CL
should show you to be a clear-headed person, capable of thinking
clearly, and a motivated, active learner. Write in a clear and logical
manner: remember, the way you write and present yourself says a lot
about you.

• Before writing the first draft, take some time to think about
yourself, your goals and your skills. Think about your background, what
and where you have studied, and how all that makes you suitable for the
program/position. Put this in to the first part of the CL.

• Continue in the second section with your professional goals, explaining the connection between them and your studies.
Present your long-term plans, and say how the program you are applying
for will help you achieve those goals. Make clear why your background
makes you suitable for the program, and how the program will help you
achieve your long-term goals. In this section also explain why
precisely that university
etc. is your choice – the courses, faculty, research interests are
possible reasons. In conclusion, state how you can contribute to the
program.

• You need to take time. Expect to revise your letter up to 10
times (no exaggeration!) before you come up with a good CL. One
strategy is, let it rest after you have written a new draft for a few
days, and read it again, in order to get a more objective view. And
have it checked by as many other people as possible! To do this, of
course, means you must start the process early.

• And don’t just copy what you have said in the other documents
that you submit, but try to add to them. Answer the essay/statement
question, if you have one – it usually will be something like "Why are
you the right person for this program?".

Chúc mừng giáng sinh

Giáng sinh sắp đến rùi ^^.Chúc chị em: Trẻ trung như heo sữa, Bốc lửa
như heo hơi, Chịu chơi như heo nái, Hăng hái như heo con, Sắc son như
heo đất, Đủ chất như ..heo thịt! Chúc anh em: Tươi vui như heo cắn, đỏ
đắn như heo lai, sức dai như… heo nọc. Chuc moi nguoi giang sinh vui ve
nhe’

Study Lemur

To try the sample data

Go to the data directory, run "test_key_index.sh" which is a self-explanatory shell script that will build an index, run several retrieval algorithms with some sample parameter files, and then evaluate the retrieval performance.

 

blog it


clipped from: www.lemurproject.org   

1. Browsing an index

This example prints out all entries in the term-to-document index and
document-to-term index of a collection with a table of contents (toc)
file named "index-file" built using the basic indexer.

Index *ind;  

ind = IndexManager::openIndex("index-toc-file.key");
// open the index specified by the table-of-content (toc) file "index-toc-file.key"
// IndexManager recognizes the suffix of the toc file (in this case ".key") and uses it to
// infer the actual type of the index to be opened (KeyfileIncIndex).


// first, browse through the term->document index (i.e., the inverted index)

int termID;

// iterate over all possible termID's, the termCountUnique() function
// gives the total count of unique terms, i.e., the vocabulary size.
// Note that the term index 0 is reserved for out-of-vocabulary
// terms, so we start from 1.

for (termID = 1; termID <= ind->termCountUnique(); termID++) {

cout << "term->document index entries for term : "
<< ind->term(termID) << endl;
// The function call term(termID) returns the string form of the term.


// now fetch doc info list for each term, this creates an
// instance of DocInfoList, which needs to be deleted later!
DocInfoList *docList = ind->docInfoList(termID);

// iterate over entries in docList
docList->startIteration();
DocInfo *dEntry;
while (docList->hasMore()) {
dEntry = docList->nextEntry();
// note that nextEntry() does *not* return an instance,
// instead, it passes out a pointer to a local static variable.
// so no "delete" is needed.

// print out this entry
cout << "-> " << dEntry->termCount() << " times in doc "
<< ind->document(dEntry->docID()) << endl;
}
delete docList; // note that you MUST delete docList!
}

U can browse the index/document sets by other prog. lang. See below links :
http://www.lemurproject.org/docs/index.php/Main_Page#Programming_with_the_Toolkit

Perl tips

How to remove leading & trailing white space of a string?

-> use sub chomp
E.g:
  my $s=" abc ";
  chomp($s);       #$s will be ‘abc’ without any spaces

Một lời kêu gọi biểu tình not verified yet (I doubt it !)

Nhan Nguyen Dang:

Le Quy Loc: Tuân theo chỉ thị của đồng chí Nguyễn Thành Tài, phó Chủ tịch UBND TP. Hồ Chí Minh, Thành Đoàn TP. Hồ Chí Minh kêu gọi các bạn trẻ và đồng bào, không phân biệt tuổi tác, giới tính, nơi cư trú, đúng 9h ngày 16/12/2007 đến trước tập hợp trước cổng NVH Thanh niên để mít ting tuần hành phản đối nhà cầm quyền Trung Quốc âm mưu lấn chiếm HS-TS của Việt Nam, và yêu cầu chấm dứt các hành động bắn giết ngư dân Việt Nam vô tội của quân đội Trung quốc trên vùng biển Hoàng Sa như thời gian vừa qua. Rất mong các bạn tham gia giúp đỡ tự in ấn các băng rôn này: “ Phản đối chính quyền Trung Quốc chiếm đoạt lãnh thổ Việt Nam”, “Hoàng Sa-Trường Sa mãi mãi là máu thịt của Việt nam”
GiangNam Vu: 2 ba.n
GiangNam Vu: cai thong tin ban vua truyen di
GiangNam Vu: noi la theo chi thi cua Ng.Th.Tai
GiangNam Vu: vay de nghi ban cung cap thogn tin
GiangNam Vu: de chu’ng minh ?
GiangNam Vu: (toi fai ycau nhu vay vi du yeu nc, chung ta fai yeu nc voi 1 tu duy su sang suot!)

Mot loi keu goi tin tuong Dang… (I don’t agree)

Người ta kêu gọi người VN biểu tình và phê phán chính phủ không mạnh dạn, nhưng mạnh dạn như thế nào đây, đánh nhau? TQ chỉ cần cái cớ như thế để đánh hết luôn những hòn đảo còn lại của ta, mà hải quân ta không đủ sức để giữ. Trừng phạt kinh tế: chỉ có TQ trừng phạt ta chứ ta chưa đủ sức làm thế. Chỉ còn ngoại giao. Về mặt ngoại giao, nhà nước đã lên tiếng đúng với tư cách của mình, và không ngừng vận động, tranh cải trên trường Quốc tế để bảo vệ chủ quyền. Chúng ta hãy tin tưởng Đảng, Chính phủ, vì lịch sử đã cho thấy họ xứng đáng với niềm tin đó. Gởi tin này đến bạn bè của bạn nếu bạn thấy đúng (Sent by VCNam)

GiangNam Vu: Rat tiec khi anh lai nghi den 2 chu~ d’anh nhau
GiangNam Vu: A noi chphu ko ngung dau tranh ve ngoai giao ?
GiangNam Vu: Vay xin loi anh, cho em hoi, chinh phu minh da lam gi ?
GiangNam Vu: Sao ngay ca tang lop tri thuc nhu em, cac ban cung khoa, va rat nhieu anh em tri thuc khac
GiangNam Vu: biet rat it &phai phan u’ng ?
GiangNam Vu: ne’u nguoi` dan biet chphu da lam gi` de bve bo` coi~
GiangNam Vu: tren truo`ng ngoai giao
GiangNam Vu: xin thua voi anh, ho san sang tin tuong !
GiangNam Vu: ko can de’n anh keu goi la hay~ tin tuong !
GiangNam Vu: vde`: ho. it duoc nghe & duoc biet chphu nay dang lam gi`
GiangNam Vu: ho. len tieng btinh de do`i hoi hanh dong cua chphu
GiangNam Vu: anh du`ng la`m tuong la` doi` chie’n tranh
GiangNam Vu: theo su tim hieu & theo doi cua ca’ nhan em
GiangNam Vu: ho. doi` dc nghe, dc biet & dc lam chu nhu cau sa’o rong~ ma chinh  phu luon de` cao : chphu do dan, vi` dan
GiangNam Vu: hay nhu: dan biet, dan ba`n, dan la`m, dan kiem tra
GiangNam Vu: em thu hoi a suy nghi gi` ma keu goi tin tg chphu khi ma`
GiangNam Vu: ca’i do dan, vi dan da bi ba’c boi 1 co che’ ha`nh chi’ ha`nh da^n ?
GiangNam Vu: dang con cai cach cai chi’nh ra kia ?
GiangNam Vu: gay bao nhieu kho’ khan cho nguoi` dan, vay vi` dan & do dan o dau ra ?
GiangNam Vu: tin tuong gi` ha anh ?
GiangNam Vu: ca’i dan biet, ba`n, la`m, kiem tra thu hoi anh ve vde` HSa TrSa
GiangNam Vu: dan biet nhung gi` ?
GiangNam Vu: co biet TQ da la’n chu’ng ta nhung noi nao ?
GiangNam Vu: dan duoc ba`n gi` vay anh? nothing !
GiangNam Vu: ba`n co`n chua tha’y gi`, thi` du`ng noi la`m & ktra
GiangNam Vu: vay khi van dang ton ta.i nhung hie.n tuo.ng nay
GiangNam Vu: vay ko biet anh nghi gi de keu goi su. tin tuong ?
GiangNam Vu: Em tom lai, neu nguoi dan duoc biet thong tin ve HSa TrSa,
GiangNam Vu: ho dc tham gia dong gop trao doi y kien voi ChPhu ve vde` nay
GiangNam Vu: ho biet dvoi Vietnam & TQ, chugn ta dang o tra.ng tha’i nhu the’ nao & mdic’h hdong tuong lai ra sao
GiangNam Vu: thi` ho. tu. nhien tin tuong & ko phai bieu ti`nh gi` ca
GiangNam Vu: ko phai ho btinh la doi da’nh nhau
GiangNam Vu: Anh noi lsu da cho thay ho xu’ng da’ng voi niem tin do’ ?
GiangNam Vu: Lsu thi` em co’ 1 su. viec day :
GiangNam Vu: su kie.n La’ng Hoa` La.c – giet nguoi` & cha.y toi. cua 1 bo phan Cong An Ha noi
GiangNam Vu: da duoc chinh phu pho bien & xu ly ra sao ?
GiangNam Vu: cac phan tich ve va’n de` nay ?
GiangNam Vu: Do’ chi la 1 vi’ du de em muon noi voi anh rang
GiangNam Vu: su tin tuong phai de’n tu` lich su 2 ma.t
GiangNam Vu: tot & chua tot
GiangNam Vu: ne’u ma.t chua tot co`n qua’ nhie`u
GiangNam Vu: xin loi anh, chu’ng toi ko the tin tuong!