Java HTTP library

Being in need to issue POST requests from within a Java application, and wanting re-usable code, I created a very simple Java library for issuing POST and GET requests to a remote server.

POST source codeGET source code

Usage (POST example):

POST post = new POST();
post.postRequest("", "param1=foo&param2=bar", "Mozilla/5.0");

In other words, the arguments are:


The output can be manipulated as desired, depending on what you response you except you should use a corresponding element/object.


Java libary in Android project

If you have developed your Java library that you would like to use within your Android project, or you want to use an existing Java library, make sure that the library itself is compiled with Java 1.6 rather than 1.7. Have been always forgetting that and have wondering for a couple of days why am I getting “NoClassDefFoundError” error.

Java encrypt class

A simple implementation of an encryption class to be used in Java applications for either encrypting strings and calculating files’ fingerprint.

import java.math.BigInteger;

public class Encrypt {

   private FileInputStream fis;

   public Encrypt() { }

   public String encryptString(String input, String salt, String algorithm) {
      String output = null;
      String fullInput = null;

      if (!salt.equals(null)) {
         fullInput = salt + input;
      } else {
         fullInput = input;

      try {
         MessageDigest digest = MessageDigest.getInstance(algorithm);
         digest.update(fullInput.getBytes(), 0, fullInput.length());
         output = new BigInteger(1, digest.digest()).toString(16);
      } catch (NoSuchAlgorithmException ex) {
      return output;

   // Based on:
   public String calculateFingerprint(String fileName, String algorithm) {
      StringBuffer sb = new StringBuffer();
      try {
         MessageDigest md = MessageDigest.getInstance(algorithm);
         fis = new FileInputStream(fileName);
         byte[] dataBytes = new byte[1024];
         int nread = 0;
         while ((nread = != -1) { md.update(dataBytes, 0, nread); }
         byte[] mdbytes = md.digest();
         for (int i = 0; i < mdbytes.length; i++) {
            sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
      } catch (Exception ex) {
      return sb.toString();

Link to GiHub repository. The class is intended to be used as a library within Java applications.

Example for encrypting a string:

Encrypt en = new Encrypt();
String encryptedString = en.encryptString(textField.getText().toString(), \
"salt goes here if you want", "md5")));

Example for calculating the fingerprint of a file:

Encrypt en = new Encrypt();
String fingerPrint = en.calculateFingerprint("/path/to/file", sha1);

Android SQLite Adapter class

Working with an SQLite database on Android can require some times more effort than what you should really give. For instance, if you ship a pre-configured and populated database with an application, you would need first to copy over the database to the specific database directory and then open it for reading and/or writing data to it. The SQLiteOpenHelper class provides a few methods that make life a bit easier but it does not provide a copy method for handling this issue. In addition to this, a check method would be required before copying the database and so on. Following on this, I implemented a SQLite adapter class to cover my needs as bellow:

– Copy database to the correct directory.
– Open and close the database.
– Execute raw SQL query.
– Execute SQL query for string, int, long, double.
– Drop table.
– Return count of a table.
– Download a db copy from Internet and replace local one.

Source code at GitHub.


Last week I was in Athens for the 4th International Conference from Scientific Computing to Computational Engineering. Many interesting talks from a wide range of areas. My talk was about “HPC Applications Performance on Virtual Clusters“. The main outcome from my perspective, we need to investigate GPU virtualisation. There are more and more scientists/researchers that want to exploit such systems and in the near future we’ll need to deploy virtualised GPU systems in the same way we do with CPUs.

My paper
My presentation

Basic HTTP authentication with Java

Opening a URL in Java is pretty much straightforward by using the class. If the URL trying to be accessed is password protected (by Apache’s AuthType for example), you will need to authenticate before trying to open the URL. The easiest way to authenticate is by using the Authenticator ( class which is available on Java 1.2 and higher:

final String theUsername = username;
final String thePassword = password;

Authenticator.setDefault(new Authenticator() {
     protected PasswordAuthentication getPasswordAuthentication() {
           return new PasswordAuthentication(theUsername, thePassword.toCharArray());

Fetching and saving my Twitter tweets

I have heard quite a few people complaining that tweets on Twitter are erased after some period and they can’t be saved without hassling. A quick googling revealed some options but I can’t say that there was anything satisfying, at least for what I was looking for. The way I’d like to save my tweets would be directly to my system, in a simple text file which I’ll be able to access from any other system if I need so.

To meet this “personal requirements” I implemented a short application in Java. It will fetch the specified feed and save your entries in a specified file. On the next fetch operation, if the user specifies the same destination file, it will just update the file with the new tweets from the feed. The most effective way to get that automatically done is to use it as a daily cronjob.

I have the following entry in my crontab:

$ crontab -l | grep fetch
41 23 * * * ~/xNIX/scripts/backups/

and the scripts all it does is:

$ cat
cd ~/xNIX/scripts/backups
java twitterFetcher ~/myTweets

I wouldn’t consider this as a proper Twitter backup application but more like a hack to get the work done without touching the Twitter API or using an external service that would require more manual work to get the tweets on my system. Also, as it uses the RSS Feed, it’s not suitable for those who tweet massively throughout the day.


Generating dump Java data files

Recently I have been running some disk I/O benchmarks, among others, with Java. I needed to check I/O operations performance with plain data files. I had to implement the following to generate dump data files to be used by the benchmark. I’m not going to comment a lot on that as it is pretty much straight forward.

Some variables declarations at first place:

    private int value = 0;
    public static int DATA_SIZE = 10000000; // 9.5MB
    private static byte[] b;
    private static int[] nums;
    private FileOutputStream byteFile;
    private DataOutputStream intFile;

The main method follows. If the user doesn’t provide any input then the default data size will be assumed, otherwise the one specified. There should be an exception handler there for checking the input format but with such a simple program it can be avoided:

    public static void main(String args[]){
        if (args.length != 0){
            DATA_SIZE = Integer.parseInt(args[0]);
            System.out.println("Creating data files with size: " + DATA_SIZE + " bytes");
        } else {
            System.out.println("Creating data files with default size.");
        GenerateData gd = new GenerateData();

Followed by the GenerateData method that actually creates the data:

    public void generate(int size){
        b = new byte[size];
        nums = new int[size/4]; // Each int is 4 bytes

        for (int i=0;i<size;i++){
           int offset = (b.length - 1 - i) * 8;
           b[i] = (byte) ((value >>> offset) & 0xFF);

        try {
            byteFile = new FileOutputStream("/tmp/");
            System.out.println("/tmp/ created");

            for (int x=0;x<nums.length;x++){
            System.out.println("/tmp/ created");

      } catch (Exception ex) {

Both files can be found under /tmp. Obviously that wouldn’t work on Windows, but there was not intention from my part in running this in any non *nix system :). If you need to do so, just replace the path.