Remove OatWriter buffering to memory for ElfWriterQuick

This allows the oat contents to be directly written to the file.

Change-Id: Ibc7ddf57477b152f07784b52f7334be73fd22833
diff --git a/dex2oat/dex2oat.cc b/dex2oat/dex2oat.cc
index 0e32f0b..1a6a98a 100644
--- a/dex2oat/dex2oat.cc
+++ b/dex2oat/dex2oat.cc
@@ -282,27 +282,14 @@
       }
     }
 
-    std::vector<uint8_t> oat_contents;
-    // TODO: change ElfWriterQuick to not require the creation of oat_contents. The old pre-mclinker
-    //       OatWriter streamed directly to disk. The new could can be adapted to do it as follows:
-    // 1.) use first pass of OatWriter to calculate size of oat structure,
-    // 2.) call ElfWriterQuick with pointer to OatWriter instead of contents,
-    // 3.) have ElfWriterQuick call back to OatWriter to stream generate the output directly in
-    //     place in the elf file.
-    oat_contents.reserve(5 * MB);
-    VectorOutputStream vector_output_stream(oat_file->GetPath(), oat_contents);
-    if (!OatWriter::Create(vector_output_stream,
-                           dex_files,
-                           image_file_location_oat_checksum,
-                           image_file_location_oat_data_begin,
-                           image_file_location,
-                           *driver.get())) {
-      LOG(ERROR) << "Failed to create oat file " << oat_file->GetPath();
-      return NULL;
-    }
+    OatWriter oat_writer(dex_files,
+                         image_file_location_oat_checksum,
+                         image_file_location_oat_data_begin,
+                         image_file_location,
+                         driver.get());
     timings.AddSplit("dex2oat OatWriter");
 
-    if (!driver->WriteElf(android_root, is_host, dex_files, oat_contents, oat_file)) {
+    if (!driver->WriteElf(android_root, is_host, dex_files, oat_writer, oat_file)) {
       LOG(ERROR) << "Failed to write ELF file " << oat_file->GetPath();
       return NULL;
     }
@@ -1019,10 +1006,10 @@
                                                            *compiler.get());
     timings.AddSplit("dex2oat ImageWriter");
     Thread::Current()->TransitionFromSuspendedToRunnable();
-    LOG(INFO) << "Image written successfully: " << image_filename;
     if (!image_creation_success) {
       return EXIT_FAILURE;
     }
+    LOG(INFO) << "Image written successfully: " << image_filename;
   }
 
   if (is_host) {