private static BitTorrentMessage decodeBitField (ByteBuffer buffer, int length) {
        byte[] bytes = new byte[length];
        buffer.get(length);

        int bitlen = length * 8;
        BitSet bits = new BitSet();
        for (int i = 0; i < bitlen; i++) {
            int bInd = i / 8;
            int bMask = 1 << (7 - (i % 8));
            if ((bytes[bInd] & bMask) != 0) {
                bits.set(i);
            }
        }

        return new BitFieldMessage(bits);
    }