/**
  * @param StreamInterface $stream
  * @param MailchimpExport_DataCallbackInterface $dataCallable
  * @throws MailchimpExport_Exception
  */
 protected function readFromStream(StreamInterface $stream, MailchimpExport_DataCallbackInterface $dataCallable)
 {
     $headings = null;
     $first = true;
     $hasHeadingRow = $dataCallable->hasHeadingRow();
     // Read until the stream is closed
     while (!$stream->feof()) {
         // Read a line from the stream
         $currentRow = trim($stream->readLine());
         if (strlen($currentRow) == 0) {
             continue;
         }
         $currentDataSet = json_decode($currentRow, true);
         if (!$currentDataSet) {
             throw new \MailchimpExport_Exception('invalid response: ' . $currentRow);
         }
         $currentRow = null;
         // check for errors
         if (isset($currentDataSet['error']) && isset($currentDataSet['code'])) {
             throw new \MailchimpExport_Exception($currentDataSet['error'], $currentDataSet['code']);
         }
         if ($first && $hasHeadingRow) {
             $headings = $currentDataSet;
             $first = false;
             continue;
         }
         // if we have headings => combine them with the dataset
         if ($headings !== null) {
             // Sometimes mailchimps gives an smaller array of data because of missing data.
             // This will cause an error in array_column
             while (count($headings) > count($currentDataSet)) {
                 $currentDataSet[] = null;
             }
             $returnValue = array_combine($headings, $currentDataSet);
         } else {
             $returnValue = $currentDataSet;
         }
         $dataCallable->addDataSet($returnValue);
         $returnValue = null;
         $currentDataSet = null;
     }
 }