Oct 23

Arrays in Java are actually Objects and sometimes we need to get the Class instance for an array object. An example would be when using reflection to get a method that takes an array as an argument.

Here’s an example for getting the Class instance for a string array.

Class arrayClass = String[].class;

written by objects \\ tags: , ,

Oct 17

You can use the getNetworkPrefixLength() method in the InterfaceAddress class to get the size of the mask.

InetAddress localHost = Inet4Address.getLocalHost();
NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localHost);

for (InterfaceAddress address : networkInterface.getInterfaceAddresses()) {
    System.out.println(address.getNetworkPrefixLength());
}

written by objects \\ tags: , , ,

Oct 15

We often want to quickly identify the type of a file. The following two libraries help us achieve that goal.

jMimeMagic

jMimeMagic is a Java library for determining the MIME type of files or streams.

Mime Type Detection Utility

Enable Java programs to detect MIME types based on file extensions, magic data and content sniffing. Supports detection from java.io.File, java.io.InputStream, java.net.URL and byte arrays.

written by objects \\ tags: , , ,