Kotlin Language Features Related to Null Handling

Any software engineer with a Java background would find the null handling features in the Kotlin language interesting. Let's summarize this topic with some examples. Nullable types: In Kotlin, types are non-nullable by default. If you want a variable to be able to hold a null value, you need to explicitly declare its type as nullable using the Type? syntax. For example, String? denotes a nullable string, while String represents a non-nullable string. Safe calls (?.): Kotlin introduces the safe call operator (?.) for handling nullable types. It allows you to safely invoke a method or access a property on a nullable object. If the object is null, the expression returns null instead of throwing a NullPointerException. Example: data class Person(val name: String, val age: Int, val address: String?) fun main() {     // Create a person with a nullable address     val person1 = Person("John Doe", 25, "123 Main Street")     val person2 = Person("Jane Doe", 30,...

UTF-8 Validation in Java


A character in UTF8 can be from 1 to 4 bytes long, subjected to the following rules:
  • For 1-byte character, the first bit is a 0, followed by its unicode code.
  • For n-bytes character, the first n-bits are all one's, the n+1 bit is 0, followed by n-1 bytes with most significant 2 bits being 10.

Given an array of integers representing the data, lets calculate whether it is a valid utf-8 encoding.

Example:

data = [235, 140, 4], which represented the octet sequence: 11101011 10001100 00000100. 
Return false. 

The first 3 bits are all one's and the 4th bit is 0 means it is a 3-bytes character. The next byte is a continuation byte which starts with 10 and that's correct. But the second continuation byte does not start with 10, so it is invalid.

Source Code:

public static void main(String[] args) {

        // should be true
        System.out.println(validUtf8(new int[]{228, 189, 160, 229, 165, 189, 13, 10}));

        //should be false
         System.out.println(validUtf8(new int[]{240, 162, 138, 147, 145}));

        //should be true
        System.out.println(validUtf8(new int[]{197, 130, 1}));

        //should be false
        System.out.println(validUtf8(new int[]{235, 140, 4}));

        //should be false
        System.out.println(validUtf8(new int[]{255}));

        //should be false
        System.out.println(validUtf8(new int[]{250, 145, 145, 145, 145}));

    }


public static boolean validUtf8(int[] data) {

        int nOctetLeft = 0;

        for (int i = 0; i < data.length; ++i) {

            int octet = data[i];

            // n-byte character
            if ((octet & (1 << 7)) != 0) {

                int n = getCount(octet);

                if (n > 4) {
                    return false;
                }

                if (n > 1) {
                    if (nOctetLeft > 0) {
                        return false;
                    }

                    nOctetLeft += n;
                    --nOctetLeft;
                } else if (n == 1) {

                    --nOctetLeft;
                }
            } else {
                // one-byte character

                if (nOctetLeft != 0) {
                    return false;
                }
            }
        }

        // unsatisfied header
        if (nOctetLeft != 0) {
            return false;
        }

        return true;
    }

    private static int getCount(int octet) {
        int count = 0;
        int i = 7;
        while (i >= 0) {
            if ((octet & (1 << i)) == 0) {
                break;
            } else {
                ++count;
            }

            --i;
        }
        return count;
    }

Comments

Popular posts from this blog

Trie Data Structure and Finding Patterns in a Collection of Words

Virtual Memory

NOTES ON COMPUTER ARCHITECTURE: Some important concepts in computer architecture