Home | History | Annotate | Download | only in images
      1 /*
      2  * Copyright 2013 Google Inc.
      3  *
      4  * Use of this source code is governed by a BSD-style license that can be
      5  * found in the LICENSE file.
      6  */
      7 
      8 #include "SkStream.h"
      9 #include "SkStreamHelpers.h"
     10 #include "SkTypes.h"
     11 
     12 size_t CopyStreamToStorage(SkAutoMalloc* storage, SkStream* stream) {
     13     SkASSERT(storage != NULL);
     14     SkASSERT(stream != NULL);
     15 
     16     if (stream->hasLength()) {
     17         const size_t length = stream->getLength();
     18         void* dst = storage->reset(length);
     19         if (stream->read(dst, length) != length) {
     20             return 0;
     21         }
     22         return length;
     23     }
     24 
     25     SkDynamicMemoryWStream tempStream;
     26     // Arbitrary buffer size.
     27     const size_t bufferSize = 256 * 1024; // 256KB
     28     char buffer[bufferSize];
     29     SkDEBUGCODE(size_t debugLength = 0;)
     30     do {
     31         size_t bytesRead = stream->read(buffer, bufferSize);
     32         tempStream.write(buffer, bytesRead);
     33         SkDEBUGCODE(debugLength += bytesRead);
     34         SkASSERT(tempStream.bytesWritten() == debugLength);
     35     } while (!stream->isAtEnd());
     36     const size_t length = tempStream.bytesWritten();
     37     void* dst = storage->reset(length);
     38     tempStream.copyTo(dst);
     39     return length;
     40 }
     41