Index: src/images/SkStreamHelpers.cpp |
diff --git a/src/images/SkStreamHelpers.cpp b/src/images/SkStreamHelpers.cpp |
index 3e9ee45345b70292d9c2ec972d57fe4d6eaf3a0b..c7c66b4b0fa95fa2e8398c84ba1165d674082aac 100644 |
--- a/src/images/SkStreamHelpers.cpp |
+++ b/src/images/SkStreamHelpers.cpp |
@@ -5,6 +5,7 @@ |
* found in the LICENSE file. |
*/ |
+#include "SkData.h" |
#include "SkStream.h" |
#include "SkStreamHelpers.h" |
#include "SkTypes.h" |
@@ -38,3 +39,29 @@ size_t CopyStreamToStorage(SkAutoMalloc* storage, SkStream* stream) { |
tempStream.copyTo(dst); |
return length; |
} |
+ |
+SkData *CopyStreamToData(SkStream* stream) { |
+ SkASSERT(stream != NULL); |
+ |
+ if (stream->hasLength()) { |
+ const size_t length = stream->getLength(); |
+ void* dst = sk_malloc_throw(length); |
+ if (stream->read(dst, length) != length) { |
+ return 0; |
+ } |
+ return SkData::NewFromMalloc(dst, 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()); |
+ return tempStream.copyToData(); |
+} |