Using Auto expanding and shrinking IoBuffer in Apache MINA

Networking applications always need ByteBuffer that can expand on the need basis. Java ByteBuffer doesn’t provide this feature. Fortunately, MINA’s IoBuffer class has implemented this much needed feature.

Creating Auto Expanding Buffer

IoBuffer class in MINA has a property autoExpand, which when set makes the IoBuffer expand on the need basis, very much similar to StringBuffer class.
Let’s see, how we can achieve this

  2. IoBuffer buffer = IoBuffer.allocate(8);
  3. buffer.setAutoExpand(true);
  5. buffer.putString("12345678", encoder);
  6. // Add more to this buffer
  7. buffer.put((byte)10);

When we set this property, the buffer will expand as and when needed. Under the hood, it essentially allocated a new IoBuffer and returns the new instance with the copied data.

So, can we shrink the buffer as well. Yes, if we set the autoshrink property to true.

  2. IoBuffer buffer = IoBuffer.allocate(8);
  3. buffer.setAutoShrink(true);
  4. buffer.put((byte)7);
  5. System.out.println("Buffer size = "+buffer.capacity());
  6. buffer.shrink();
  7. buffer.capacity(16);
  8. System.out.println("Buffer size = "+buffer.capacity());
  9. buffer.shrink();
  10. System.out.println("Buffer size = "+buffer.capacity());

Here we set autoShrink property to true. To manually shrink the capacity, we use shrink() method. Caution, shrink(), won’t reduce capacity below the minimum capacity (the capacity with which the buffer was created). The second call to shrink(), will reduce the capacity to 8.

Has updated the MINA’s documentation page on IoBuffer today. So please feel free to find more details here

How I learned Apache MINA internal?

This is a very short post. The idea was to share how I learned some basic things about Apache MINA. LoggingFilter provides a very good way of how MINA process a incoming requests. Add the LoggingFilter in the chain and enable logging. As soon as it receives a packet, watch out the logs, it provides enough details on what is going around inside the framework. Believe it or not, I learned a lot of internals using this filter.

Apache MINA – Blacklist Filter Explained

Blacklist filter blocks connections from the blacklisted remote Addresses. The filter is very useful, for dropping the connection originating from addresses, not of interest.

Useful API’s

setBlacklist(InetAddress[] addresses) – Sets a list of IP Addresses that needs to be blocked. This API call clears all the previously added Addresses

setSubnetBlacklist(Subnet[] subnets) – Sets a List of Subnets to be blocked. This API call clears all the previously added Addresses

setBlacklist(Iterable<InetAddress> addresses) – Sets a list of IP Addresses that needs to be blocked. This API call clears all the previously added Addresses

setSubnetBlacklist(Iterable<Subnet> subnets) - Sets a List of Subnets to be blocked. This API call clears all the previously added Addresses

block(InetAddress address) - Adds a remote address to the blacklist

block(Subnet subnet) – Adds a Subnet to be blocked

unblock(InetAddress address) – Removes remote address from the blacklist

unblock(Subnet subnet) – Removes a Subnet from the blacklist

Under the Hood

For each of the events, the Filter checks for the remote address, in the blacklisted Address. If found, the session is closed. The isBlocked() API, checks for the remote address in the configured list and returns true if found.


Blacklist Filter

Blacklist Filter

How to Use Blacklist Filter

To use a blacklist filter, we need to follow some simple steps listed below

  • Create an instance of Blacklist filter
  • Add IP Addresses/Subnets to be blacklisted
  • Add the Backlist filter to the Filter chain

Code Snippet

  1. public BlacklistFilter createBlacklistFilter() throws UnknownHostException {
  2. BlacklistFilter filter = new BlacklistFilter();
  4. // Get the IP Addresses to be blocked
  5. // from a config file or from DB, whatever
  6. InetAddress[] blockedAddresses = null;
  8. // populate the list
  9. // Ensure that the list if populated correctly as per requirement
  10. blockedAddresses = new InetAddress[1];
  12. // block all connection from Localhost
  13. blockedAddresses[0] = InetAddress.getByName("");
  14. filter.setBlacklist(blockedAddresses);
  16. return filter;
  17. }

The code is fairly straight forward. We create an instance of Blacklist Filter and add the list of Addresses to be blocked. Then we need to add the Filter instance in the Filter chain of our implementation.

Usage Scenario’s

  • Conditionally allow traffic from only selected sources

Think Differently :-)

  • The default MINA implementation stores the blacklisted Addresses in a List, Custom data structure can be used to speed up search
  • Can extend the Filter’s functionality to allow traffic only from selected sources.