private static int calculateChecksum (String tagString, ByteBuffer data) {
        int sum = 0;

        data.mark ();

        // special adjustment for head table: always treat the 4-bytes
        // starting at byte 8 as 0x0000. This the checkSumAdjustment so
        // must be ignored here (see the TTF spec)
        if (tagString.equals ("head")) {
            sum += data.getInt();
            sum += data.getInt();
            // consume the uncounted checkSumAdjustment int
            data.getInt();
        }

        int nlongs = (data.remaining () + 3) / 4;

        while (nlongs-- > 0) {
            if (data.remaining () > 3) {
                sum += data.getInt ();
            } else {
                byte b0 = (data.remaining () > 0) ? data.get () : 0;
                byte b1 = (data.remaining () > 0) ? data.get () : 0;
                byte b2 = (data.remaining () > 0) ? data.get () : 0;

                sum += ((0xff & b0) << 24) | ((0xff & b1) << 16) |
                        ((0xff & b2) << 8);
            }
        }

        data.reset ();

        return sum;
    }