Recent Posts

Tabular data in a Java Console application

Displaying tabular data is a very common use-case, but working a console application and displaying the same becomes a tedious task. Why? First, you have to take care of formatting the entire data. Second, you need to make sure that the data does not spill-over the boundary of the cell. Third, it takes time and precision for such a code, and spending time on this boiler-plate code is total-waste.

Thus, I created ConsoleTable, a new helper class in Jerry-Core framework. This class is pretty useful to create such console outputs.


  • Supports automatic sizing of columns in full-width layout
  • Supports multi-line layout where column text is broken at word boundaries to spill over multi-line
  • Support export to CSV, XML and JSON formats


For example, to create a layout like:

 | ------------ | --------- | --------------- | 
 | Stock Ticker | Company   | Any one product | 
 | ------------ | --------- | --------------- | 
 | APPL         | Apple     | iPhone          | 
 | GOOG         | Google    | GMail           | 
 | MSFT         | Microsoft | Windows         | 
 | ------------ | --------- | --------------- | 

you just need to code something like:

ConsoleTable table = new ConsoleTable();

table.addHeaderRow("Stock Ticker", "Company", "Any one product");

table.addRow("APPL", "Apple", "iPhone");
table.addRow("GOOG", "Google", "GMail");
table.addRow("MSFT", "Microsoft", "Windows");


The code itself takes care of proper alignment and spacing of each element. This is not the end.

The ConsoleTable also supports multi-line output by breaking the sentence at word boundaries to make sure that the text fits in the cell.

For example:

ConsoleTable table = new ConsoleTable(ConsoleTableLayout.MULTI_LINE);

table.addHeaderRow("Stock Ticker", "Company", "Products");
table.addRow("APPL", "Apple", "iPhone, iPad, iPod, Mac, OSX, Mac Pro");
table.addRow("GOOG", "Google", "GMail, Blogger, AdSense, Analytics, Search");
table.addRow("MSFT", "Microsoft", "Windows, Office, Remote Desktop");

table.setColumnSize(2, 20);


produces output as:

 | ------------ | --------- | -------------------- | 
 | Stock Ticker | Company   | Products             | 
 | ------------ | --------- | -------------------- | 
 | APPL         | Apple     | iPhone, iPad, iPod,  | 
 |              |           | Mac, OSX, Mac Pro    | 
 | GOOG         | Google    | GMail, Blogger,      | 
 |              |           | AdSense,             | 
 |              |           | Analytics, Search    | 
 | MSFT         | Microsoft | Windows, Office,     | 
 |              |           | Remote Desktop       | 
 | ------------ | --------- | -------------------- | 

The table also supports export to various formats:

Console table = getMyTable(); // some table data

// create a CSV
ConsoleTableWriter.writeCsv(table, System.out);

// create JSON output
ConsoleTableWriter.writeJson(table, System.out);

// create an XML
// wrap everthing inside a <data> ... </data> tag
// each row be wrappped inside a <row> ... </row> tag
ConsoleTableWriter.writeXml(table, System.out, "data", "row"); 

Hope this helps.

Various Bit-Array Implementation in Java

Bit-Arrays are much-useful and quite-fast data-structure that have a variety of uses. One of the most important ones in context of Big-Data is the use in Bloom filters. To store the bloom we need a very fast bit-array and the ones that can be persisted as well. Java has only an in-memory bit-array implementation. I needed a file-persisted one to be used in conjunction with the bloomfilter filter project. Thus, I added the following implementations to the jerry-core project.

  • FastBitArray - an implementation that is much faster than the standard Java implementation
  • FileBackedBitArray - backed by file persistence, and all operations are flushed back automatically
  • MMapFileBackedBitArray - one that is file-persisted, but uses memory-mapped files for nuch faster performance
  • JavaBitSetArray - one that uses internal Java implementation underneath

Usage is pretty simple as,

final int maxElements = 1000 * 1000; // 1 million
BitArray ba = new FileBackedBitArray(new File(""), maxElements);

boolean updated = ba.setBit(13); // returns true
updated = ba.setBit(13); // returns false
udpated = ba.setBit(13); // returns true

boolean isSet = ba.getBit(13); // returns true

// using the memory-mapped version is similar
ba = new MMapFileBackedBitArray(new File("my-bit-array"), maxElements);

// all other operations are the same

I have used MMapFileBackedBitArray in production for the last few years and has been quite useful and fast.

Hope this helps.

Iterating over Strings inside a String object

When reading file contents, or working with REST requests, many a times we want to read a String object line-by-line, that is, read lines within a single String object. Java does not offer a simple solution to the same - either you convert the String object to a byte[] and then use ByteArrayInputStream or use a StringReader and then push this into another line-by-line reader.

For the same, I wrote a simple utility class, StringLineIterator (available in jerry-core project) simplifying reading of lines to the following code snippet:

String contents = '...'; // some contents that contains new-lines, and form-feed characters

StringLineIterator iterator = new StringLineIterator(contents);
while(iterator.hasNext()) {
    String line =;

    // do something with this extracted line

This helps us reading sub-string lines from contents and reduces boiler-plate code. Note that this implementation would use extra memory to the extent of each line, as it creates a new String object for each line that is read via the iterator.

Hope this helps.