Avoid getLength in ico decoder.
Only call getLength() if hasLength() returned true. Otherwise, copy the
stream into an SkDynamicMemoryWStream and copy it into alloc'ed space.
Share common code between bmp and ico.
BUG=https://b.corp.google.com/issue?id=8432093
R=djsollen@google.com
Review URL: https://codereview.chromium.org/23330002
git-svn-id: http://skia.googlecode.com/svn/trunk@10850 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/src/images/SkImageDecoder_libbmp.cpp b/src/images/SkImageDecoder_libbmp.cpp
index 6c5ae27..73e7a20 100644
--- a/src/images/SkImageDecoder_libbmp.cpp
+++ b/src/images/SkImageDecoder_libbmp.cpp
@@ -8,10 +8,11 @@
#include "bmpdecoderhelper.h"
+#include "SkColorPriv.h"
#include "SkImageDecoder.h"
#include "SkScaledBitmapSampler.h"
#include "SkStream.h"
-#include "SkColorPriv.h"
+#include "SkStreamHelpers.h"
#include "SkTDArray.h"
#include "SkTRegistry.h"
@@ -96,31 +97,12 @@
// First read the entire stream, so that all of the data can be passed to
// the BmpDecoderHelper.
- // Byte length of all of the data.
- size_t length;
// Allocated space used to hold the data.
SkAutoMalloc storage;
-
- 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);
+ // Byte length of all of the data.
+ const size_t length = CopyStreamToStorage(&storage, stream);
+ if (0 == length) {
+ return 0;
}
const bool justBounds = SkImageDecoder::kDecodeBounds_Mode == mode;