Don't retain large blob data in memory
[gitphp.git] / include / git / blob / Blob.class.php
blob:a/include/git/blob/Blob.class.php -> blob:b/include/git/blob/Blob.class.php
--- a/include/git/blob/Blob.class.php
+++ b/include/git/blob/Blob.class.php
@@ -137,59 +137,74 @@
 
 		$this->dataEncoded = false;
 
-		if ($this->size === null)
+		$datachanged = false;
+
+		if ($this->size === null) {
 			$this->size = strlen($this->data);
-
-		if ($this->binary === null)
+			$datachanged = true;
+		}
+
+		if ($this->binary === null) {
 			$this->binary = GitPHP_Blob::DataIsBinary($this->data);
-
-		foreach ($this->observers as $observer) {
-			$observer->ObjectChanged($this, GitPHP_Observer_Interface::CacheableDataChange);
-		}
-	}
-
-	/**
-	 * Gets the blob size
-	 *
-	 * @return integer size
-	 */
-	public function GetSize()
-	{
-		if ($this->size === null) {
-			$this->size = $this->strategy->Size($this);
-
+			$datachanged = true;
+		}
+
+		if ($this->size > GitPHP_Blob::LargeBlobSize) {
+			$this->data = null;
+			$this->dataRead = false;
+		} else {
+			$datachanged = true;
+		}
+
+		if ($datachanged) {
 			foreach ($this->observers as $observer) {
 				$observer->ObjectChanged($this, GitPHP_Observer_Interface::CacheableDataChange);
 			}
 		}
-		
-		return $this->size;
-	}
-
-	/**
-	 * Sets the blob size
-	 *
-	 * @param integer $size size
-	 */
-	public function SetSize($size)
-	{
-		$this->size = $size;
-	}
-
-	/**
-	 * Tests if this blob is a binary file
-	 *
-	 * @return boolean true if binary file
-	 */
-	public function IsBinary()
-	{
-		if ($this->binary === null) {
-			$this->binary = GitPHP_Blob::DataIsBinary($this->GetData());
+	}
+
+	/**
+	 * Gets the blob size
+	 *
+	 * @return integer size
+	 */
+	public function GetSize()
+	{
+		if ($this->size === null) {
+			$this->size = $this->strategy->Size($this);
 
 			foreach ($this->observers as $observer) {
 				$observer->ObjectChanged($this, GitPHP_Observer_Interface::CacheableDataChange);
 			}
 		}
+		
+		return $this->size;
+	}
+
+	/**
+	 * Sets the blob size
+	 *
+	 * @param integer $size size
+	 */
+	public function SetSize($size)
+	{
+		$this->size = $size;
+	}
+
+	/**
+	 * Tests if this blob is a binary file
+	 *
+	 * @return boolean true if binary file
+	 */
+	public function IsBinary()
+	{
+		if ($this->binary === null) {
+			$this->binary = GitPHP_Blob::DataIsBinary($this->GetData());
+
+			foreach ($this->observers as $observer) {
+				$observer->ObjectChanged($this, GitPHP_Observer_Interface::CacheableDataChange);
+			}
+		}
 
 		return $this->binary;
 	}

comments