add copyToData() to dyanamicwstream
git-svn-id: http://skia.googlecode.com/svn/trunk@1695 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/include/core/SkStream.h b/include/core/SkStream.h
index c3d8185..9dfb8df 100644
--- a/include/core/SkStream.h
+++ b/include/core/SkStream.h
@@ -20,6 +20,8 @@
#include "SkRefCnt.h"
#include "SkScalar.h"
+class SkDataRef;
+
class SK_API SkStream : public SkRefCnt {
public:
virtual ~SkStream();
@@ -289,8 +291,14 @@
const char* getStream() const;
// same as getStream, but additionally detach the flattened datat
- const char* detach();
-
+ // DEPRECATED : use copyToData() instead
+ const char* detach() const;
+
+ /**
+ * Return a copy of the data written so far
+ */
+ SkDataRef* copyToData() const;
+
// reset the stream to its original state
void reset();
void padToAlign4();
diff --git a/src/core/SkStream.cpp b/src/core/SkStream.cpp
index 21ee05e..c0a7348 100644
--- a/src/core/SkStream.cpp
+++ b/src/core/SkStream.cpp
@@ -16,6 +16,7 @@
*/
#include "SkStream.h"
+#include "SkDataRef.h"
#include "SkFixed.h"
#include "SkString.h"
#include "SkOSFile.h"
@@ -594,7 +595,7 @@
reset();
}
-const char* SkDynamicMemoryWStream::detach()
+const char* SkDynamicMemoryWStream::detach() const
{
const char* result = getStream();
fCopyToCache = NULL;
@@ -724,6 +725,15 @@
write(&zero, padBytes);
}
+static void sk_free_release_proc(const void* ptr, size_t length, void*) {
+ sk_free((void*)ptr);
+}
+
+SkDataRef* SkDynamicMemoryWStream::copyToData() const {
+ return SkDataRef::NewWithProc(this->detach(), fBytesWritten,
+ sk_free_release_proc, NULL);
+}
+
/////////////////////////////////////////////////////////////////////////////////////////////////////////
void SkDebugWStream::newline()
diff --git a/tests/StreamTest.cpp b/tests/StreamTest.cpp
index e62f2ed..9aaa8b4 100644
--- a/tests/StreamTest.cpp
+++ b/tests/StreamTest.cpp
@@ -1,6 +1,7 @@
#include "Test.h"
#include "SkRandom.h"
#include "SkStream.h"
+#include "SkDataRef.h"
#define MAX_SIZE (256 * 1024)
@@ -82,6 +83,13 @@
REPORTER_ASSERT(reporter, memcmp(&dst[i * 26], s, 26) == 0);
}
REPORTER_ASSERT(reporter, memcmp(dst, ds.getStream(), 100*26) == 0);
+
+ {
+ SkDataRef* data = ds.copyToData();
+ REPORTER_ASSERT(reporter, 100 * 26 == data->size());
+ REPORTER_ASSERT(reporter, memcmp(dst, data->data(), data->size()) == 0);
+ data->unref();
+ }
delete[] dst;
}