Index: src/core/SkStream.cpp |
diff --git a/src/core/SkStream.cpp b/src/core/SkStream.cpp |
index ebaac9ab1503ff2c4002e21948fca6a2180ac236..1022d183d93cc4bb36526f437c04f89f520ff856 100644 |
--- a/src/core/SkStream.cpp |
+++ b/src/core/SkStream.cpp |
@@ -8,10 +8,12 @@ |
#include "SkStream.h" |
+#include "SkStreamPriv.h" |
#include "SkData.h" |
#include "SkFixed.h" |
#include "SkString.h" |
#include "SkOSFile.h" |
+#include "SkTypes.h" |
/////////////////////////////////////////////////////////////////////////////// |
@@ -851,3 +853,57 @@ SkStreamAsset* SkStream::NewFromFile(const char path[]) { |
} |
return stream; |
} |
+ |
+// Declared in SkStreamPriv.h: |
+size_t SkCopyStreamToStorage(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; |
+} |
+ |
+// Declared in SkStreamPriv.h: |
+SkData* SkCopyStreamToData(SkStream* stream) { |
+ SkASSERT(stream != NULL); |
+ |
+ if (stream->hasLength()) { |
+ const size_t length = stream->getLength(); |
+ SkAutoMalloc dst(length); |
+ if (stream->read(dst.get(), length) != length) { |
+ return NULL; |
+ } |
+ return SkData::NewFromMalloc(dst.detach(), length); |
+ } |
+ |
+ SkDynamicMemoryWStream tempStream; |
+ const size_t bufferSize = 4096; |
+ char buffer[bufferSize]; |
+ do { |
+ size_t bytesRead = stream->read(buffer, bufferSize); |
+ tempStream.write(buffer, bytesRead); |
+ } while (!stream->isAtEnd()); |
+ return tempStream.copyToData(); |
+} |