public function setExif(PelExif $exif)
    {
        $app0_offset = 1;
        $app1_offset = - 1;

        /* Search through all sections looking for APP0 or APP1. */
        $sections_count = count($this->sections);
        for ($i = 0; $i < $sections_count; $i ++) {
            if (! empty($this->sections[$i][0])) {
                $section = $this->sections[$i];
                if ($section[0] == PelJpegMarker::APP0) {
                    $app0_offset = $i;
                } elseif (($section[0] == PelJpegMarker::APP1) && ($section[1] instanceof PelExif)) {
                    $app1_offset = $i;
                    break;
                }
            }
        }

        /*
         * Store the Exif data at the appropriate place, either where the
         * old Exif data was stored ($app1_offset) or right after APP0
         * ($app0_offset+1).
         */
        if ($app1_offset > 0) {
            $this->sections[$app1_offset][1] = $exif;
        } else {
            $this->insertSection(PelJpegMarker::APP1, $exif, $app0_offset + 1);
        }
    }