private function writeChunkedFile()
 {
     $engine = $this->getChunkEngine();
     $parameters = $this->getNewFileParameters();
     $parameters = array('isPartial' => true) + $parameters;
     $data_length = $this->getDataLength();
     if ($data_length !== null) {
         $length = $data_length;
     } else {
         $length = 0;
     }
     $file = PhabricatorFile::newChunkedFile($engine, $length, $parameters);
     $file->save();
     $rope = $this->getRope();
     // Read the source, writing chunks as we get enough data.
     while ($this->readFileData()) {
         while (true) {
             $rope_length = $rope->getByteLength();
             if ($rope_length < $engine->getChunkSize()) {
                 break;
             }
             $this->writeChunk($file, $engine);
         }
     }
     // If we have extra bytes at the end, write them.
     if ($rope->getByteLength()) {
         $this->writeChunk($file, $engine);
     }
     $file->setIsPartial(0);
     if ($data_length === null) {
         $file->setByteSize($this->getTotalBytesWritten());
     }
     $file->save();
     return $file;
 }
 public function allocateChunks($length, array $properties)
 {
     $file = PhabricatorFile::newChunkedFile($this, $length, $properties);
     $chunk_size = $this->getChunkSize();
     $handle = $file->getStorageHandle();
     $chunks = array();
     for ($ii = 0; $ii < $length; $ii += $chunk_size) {
         $chunks[] = PhabricatorFileChunk::initializeNewChunk($handle, $ii, min($ii + $chunk_size, $length));
     }
     $file->openTransaction();
     foreach ($chunks as $chunk) {
         $chunk->save();
     }
     $file->save();
     $file->saveTransaction();
     return $file;
 }