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");
            System.out.println("");
        } else {
            System.out.println("Creating data files with default size.");
            System.out.println("");
        }
        GenerateData gd = new GenerateData();
        gd.generate(DATA_SIZE);
    }

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/rawbytearray.data");
            byteFile.write(b);
            byteFile.close();
            System.out.println("/tmp/rawbytearray.data created");

            for (int x=0;x<nums.length;x++){
                intFile.writeInt(nums[x]);
            }
            intFile.close();
            System.out.println("/tmp/datastream.data created");

      } catch (Exception ex) {
         System.out.println(ex);
      }
    }
byteFile

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.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s