Locking a file is quite simple in Java. The FileChannel (Java Doc) provides all methods we need to lock files. The tryLock() (Java Doc) method will try to obtain the lock on the file without waiting. If the lock is acquired an instance of FileLock (Java Doc) is returned, otherwise this method returns null.

Albert Attard
Author: 1 month ago

Selenium () is a comprehensive testing framework for web applications which allows us to take screenshots of web pages programmatically amongst many other things. It makes use of web browsers, such as FireFox (), to open the web page and then takes the screenshot. This guarantees a genuine representation of the web page.

Albert Attard
Author: 1 month ago

The Windows operating system (Homepage) provides useful information about video files playable by Windows Media Player (Homepage). This information includes the video length, frame width and height and other similar properties. Unfortunately there is no Java API which can easily retrieve such information. Instead we need to rely on third party native libraries such as MediaInfo (Homepage) and create wrappers around it. In this article we will see how to use the aforementioned third party native library to retrieve the video properties from a Java application.

Albert Attard
Author: 2 months ago

Cryptographic Hash algorithms (Wiki), such as SHA-1 (Wiki), are known for digesting inputs of any size into a fixed length, nondeterministic, output. Irrespective of the input size, the output is always of a fixed length, 160 bits (or 20 bytes) in case of SHA-1. Different hash algorithms produce different lengths. When processing large inputs, such as large files, it is convenient to work with streams, rather than loading all input to memory first.

Albert Attard
Author: 2 months ago

Databases are an important part of most applications irrespective of their size and nature. Data needs to be persisted and this is usually done through a database. OSGi (Home Page) is an emerging platform providing high degree of modularity and also supports the hot deployment and replacement of modules. This article describes how to use Flyway (Home Page), a database migration library, with Eclipse Virgo (Home Page) OSGi implementation (previously known as Spring Dynamic Modules (Reference Guide)).

Albert Attard
Author: 4 months ago

The Singleton (Wiki) pattern is quite an old and well known pattern. This pattern was discussed over and over again in many books and articles on the web. So why are we discussing it again here? This article is not about the Singleton pattern per se, but more about the Java Memory Model (Paper) and the concurrent access of shared objects.

Albert Attard
Author: 4 months ago

Java 7 introduced a new type of ExecutorService (Java Doc) called Fork/Join Framework (Tutorial), which excels in handling recursive algorithms. Different from other implementations of the ExecutorService, the Fork/Join Framework uses a work-stealing algorithm (Paper), which maximise the threads utilisation, and provides a simpler way to deal with tasks which spawn other tasks (referred to as subtasks).

Albert Attard
Author: 4 months ago

Say that you are eating some sweets while watching a movie. You had a look at your pack or sweets, counted five and continued watching the movie. Took the first one and deducted this in your mind (without looking at the sweets pack). In the mean time, and without you knowing, your daughter takes one. While you think that there are four sweets left, in reality there are only three. You will discover this at the end when you are looking for the last one. This is a Race Condition and in this article we will see how this occurs in programming and how we can avoid it.

Albert Attard
Author: 5 months ago