1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/src/images/SkStreamHelpers.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,40 @@ 1.4 +/* 1.5 + * Copyright 2013 Google Inc. 1.6 + * 1.7 + * Use of this source code is governed by a BSD-style license that can be 1.8 + * found in the LICENSE file. 1.9 + */ 1.10 + 1.11 +#include "SkStream.h" 1.12 +#include "SkStreamHelpers.h" 1.13 +#include "SkTypes.h" 1.14 + 1.15 +size_t CopyStreamToStorage(SkAutoMalloc* storage, SkStream* stream) { 1.16 + SkASSERT(storage != NULL); 1.17 + SkASSERT(stream != NULL); 1.18 + 1.19 + if (stream->hasLength()) { 1.20 + const size_t length = stream->getLength(); 1.21 + void* dst = storage->reset(length); 1.22 + if (stream->read(dst, length) != length) { 1.23 + return 0; 1.24 + } 1.25 + return length; 1.26 + } 1.27 + 1.28 + SkDynamicMemoryWStream tempStream; 1.29 + // Arbitrary buffer size. 1.30 + const size_t bufferSize = 256 * 1024; // 256KB 1.31 + char buffer[bufferSize]; 1.32 + SkDEBUGCODE(size_t debugLength = 0;) 1.33 + do { 1.34 + size_t bytesRead = stream->read(buffer, bufferSize); 1.35 + tempStream.write(buffer, bytesRead); 1.36 + SkDEBUGCODE(debugLength += bytesRead); 1.37 + SkASSERT(tempStream.bytesWritten() == debugLength); 1.38 + } while (!stream->isAtEnd()); 1.39 + const size_t length = tempStream.bytesWritten(); 1.40 + void* dst = storage->reset(length); 1.41 + tempStream.copyTo(dst); 1.42 + return length; 1.43 +}