Alessandro's blog

Thoughts on software development

Sep 13, 2016 - Load a resource into a String in Java

Recently I needed a simple method to load a file from resources into a String. Even if it looks like a very simple task, it usually involves many awkward transformations and exceptions catching.
This solution uses a Scanner to read the entire InputStream into a String object.

private String loadFromResources(String resourcePath) throws Exception {
    try (InputStream is = getClass()
            .getClassLoader().getResourceAsStream(resourcePath)) {
        Scanner sc = new Scanner(is);

The code takes advantage of Java7 AutoCloseable interface and try-with-resources statement to reduce boilerplate code. In case of exceptions the underlying input stream is closed automatically.

It works by setting \A as a delimiter. This regexp Pattern looks for the “beginning of the input” token which of course can’t be reached and causes all the input to be picked up by next().
Using the actual class’ classloader works very well in OSGi environments too (e.g. Apache Karaf, JBoss Fuse).

Jul 20, 2016 - How to fold code regions with Netbeans

In C# language you can surround a block of code with #region / #endregion preprocessor directive.

#region lets you specify a block of code that you can expand or collapse when using the outlining feature of the Visual Studio Code Editor. In longer code files, it is convenient to be able to collapse or hide one or more regions so that you can focus on the part of the file that you are currently working on.

Java misses such functionality. While any IDE collapses methods or classes, there’s no unique way to collapse more methods together.
In Netbeans we can define a folding region using custom crafted comments in Java source code:

//<editor-fold desc="Add your description">
... your Java code here

The result is pretty much the same as Visual Studio.

Unfolded code: Unfolded code

Folded code: Folded Java code! Well done, Netbeans!

This way we can group togeter related methods, collapse getters/setters, collapse loops inside big methods and in general manage code readability.

However, a folding region defined as above is displayed as a comment in Eclipse…

There’s a shortcut to insert a code fold in the editor: just type fcom and press TAB key.

Apr 27, 2016 - 3 flows antipattern

While maintaining an old Java enterprise application I got lost in the classical spaghetti code you would expect. After some days of staring at weird walls of code, huge classes and methods with at least one side-effect, I finally understood the logical flow of the program.

It isn’t easy to explain what each method does and how it achieves the result. This confusion arises from the fact that many classes violate the single responsibility principle in the same way: by having exactly three responsibilities. I call each one of these responsibilities “code flow”.
The first code flow is the regular one, when the application functions as expected. The second code flow is the error handling flow: whan something goes wrong take the else branch. The third code flow is the test flow, when some flag is enabled return predefined data or do not call external services.

I named this situation “3 flows antipattern”. A clean design of code separates the three flows into multiple classes (each one having exactly a single flow) using the most appropriate language features (interfaces, exceptions).

More on code design topic coming soon in a longer post.

Mar 21, 2016 - MD5 for Windows command line

Windows command line and Powershell do not have a default “md5sum” utility. There are many third party tools or PS scripts to do such a simple task. I prefer a pure Microsoft utility called FCIV: File Checksum Integrity Verifier. It can be downloaded from Microsoft site:

The usage is quite simple:
fciv myfile.ext calculates the MD5 checksum of the file.
fciv -sha1 myfile.ext calculates the SHA1 checksum.
fciv -both myfile.ext calculates both hashes.

A note about the installation: fciv.exe is not added to the system path, so you must do it manually. Type in the command line: setx PATH "%path%;c:\fciv" (change C:\fciv with the path of your installation)

About me

My online presence can be proven by the following social accounts ;-)