Make BMP decoder not depend on call to getLength.

If the decoder does not have a length, use an SkDynamicMemoryStream
to copy it to contiguous memory, to be passed to BmpDecoderHelper.

BUG=https://b.corp.google.com/issue?id=8432093
R=djsollen@google.com

Review URL: https://codereview.chromium.org/22877020

git-svn-id: http://skia.googlecode.com/svn/trunk/src@10849 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/images/SkImageDecoder_libbmp.cpp b/images/SkImageDecoder_libbmp.cpp
index 14b9090..6c5ae27 100644
--- a/images/SkImageDecoder_libbmp.cpp
+++ b/images/SkImageDecoder_libbmp.cpp
@@ -93,12 +93,34 @@
 };
 
 bool SkBMPImageDecoder::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) {
+    // First read the entire stream, so that all of the data can be passed to
+    // the BmpDecoderHelper.
 
-    size_t length = stream->getLength();
-    SkAutoMalloc storage(length);
+    // Byte length of all of the data.
+    size_t length;
+    // Allocated space used to hold the data.
+    SkAutoMalloc storage;
 
-    if (stream->read(storage.get(), length) != length) {
-        return false;
+    if (stream->hasLength()) {
+        length = stream->getLength();
+        void* dst = storage.reset(length);
+        if (stream->read(dst, length) != length) {
+            return false;
+        }
+    } else {
+        SkDynamicMemoryWStream tempStream;
+        // Arbitrary buffer size.
+        const size_t bufferSize = 256 * 1024; // 256 KB
+        char buffer[bufferSize];
+        length = 0;
+        do {
+            size_t bytesRead = stream->read(buffer, bufferSize);
+            tempStream.write(buffer, bytesRead);
+            length += bytesRead;
+            SkASSERT(tempStream.bytesWritten() == length);
+        } while (!stream->isAtEnd());
+        void* dst = storage.reset(length);
+        tempStream.copyTo(dst);
     }
 
     const bool justBounds = SkImageDecoder::kDecodeBounds_Mode == mode;