Index: src/images/SkImageDecoder.cpp |
diff --git a/src/images/SkImageDecoder.cpp b/src/images/SkImageDecoder.cpp |
index a671607ef36f811695fd8fbe4831749a294634d6..a68e324d07e88833915e853cce1cdee5a47aa8d9 100644 |
--- a/src/images/SkImageDecoder.cpp |
+++ b/src/images/SkImageDecoder.cpp |
@@ -467,3 +467,44 @@ bool SkImageDecoder::DecodeStream(SkStream* stream, SkBitmap* bm, |
} |
return success; |
} |
+ |
+/** |
+ * Copy the provided stream to memory allocated by storage. |
+ * Used by SkImageDecoder_libbmp and SkImageDecoder_libico. |
+ * @param storage Allocator to hold the memory. Will be reset to be large |
+ * enough to hold the entire stream. Upon successful return, |
+ * storage->get() will point to data holding the streams entire |
+ * contents. |
+ * @param stream SkStream to be copied into storage. |
+ * @return size_t Total number of bytes in the SkStream, which is also the |
+ * number of bytes pointed to by storage->get(). Returns 0 on failure. |
+ */ |
+size_t copy_stream_to_storage(SkAutoMalloc* storage, SkStream* stream) { |
+ SkASSERT(storage != NULL); |
+ SkASSERT(stream != NULL); |
+ |
+ if (stream->hasLength()) { |
+ const size_t length = stream->getLength(); |
+ void* dst = storage->reset(length); |
+ if (stream->read(dst, length) != length) { |
+ return 0; |
+ } |
+ return length; |
+ } |
+ |
+ SkDynamicMemoryWStream tempStream; |
+ // Arbitrary buffer size. |
+ const size_t bufferSize = 256 * 1024; // 256KB |
+ char buffer[bufferSize]; |
+ SkDEBUGCODE(size_t debugLength = 0;) |
+ do { |
+ size_t bytesRead = stream->read(buffer, bufferSize); |
+ tempStream.write(buffer, bytesRead); |
+ SkDEBUGCODE(debugLength += bytesRead); |
+ SkASSERT(tempStream.bytesWritten() == debugLength); |
+ } while (!stream->isAtEnd()); |
+ const size_t length = tempStream.bytesWritten(); |
+ void* dst = storage->reset(length); |
+ tempStream.copyTo(dst); |
+ return length; |
+} |