Calculate binary flag and size when loading blob data
Calculate binary flag and size when loading blob data

--- a/include/git/blob/Blob.class.php
+++ b/include/git/blob/Blob.class.php
@@ -137,6 +137,12 @@
 
 		$this->dataEncoded = false;
 
+		if ($this->size === null)
+			$this->size = strlen($this->data);
+
+		if ($this->binary === null)
+			$this->binary = GitPHP_Blob::DataIsBinary($this->data);
+
 		foreach ($this->observers as $observer) {
 			$observer->ObjectChanged($this, GitPHP_Observer_Interface::CacheableDataChange);
 		}
@@ -178,11 +184,7 @@
 	public function IsBinary()
 	{
 		if ($this->binary === null) {
-			$data = $this->GetData();
-			if (strlen($data) > 8000)
-				$data = substr($data, 0, 8000);
-
-			$this->binary = (strpos($data, chr(0)) !== false);
+			$this->binary = GitPHP_Blob::DataIsBinary($this->GetData());
 
 			foreach ($this->observers as $observer) {
 				$observer->ObjectChanged($this, GitPHP_Observer_Interface::CacheableDataChange);
@@ -294,5 +296,22 @@
 		return 'project|' . $proj . '|blob|' . $hash;
 	}
 
+	/**
+	 * Determines whether a data buffer is binary
+	 *
+	 * @param string $data data buffer
+	 * @return boolean true if binary
+	 */
+	public static function DataIsBinary($data)
+	{
+		if (empty($data))
+			return false;
+
+		if (strlen($data) > 8000)
+			$data = substr($data, 0, 8000);
+
+		return strpos($data, chr(0)) !== false;
+	}
+
 }
 

comments