Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(289)

Side by Side Diff: src/core/SkStream.cpp

Issue 568683002: use SkData::NewUninitialized (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: add SkStream::readIntoData Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 1
2 /* 2 /*
3 * Copyright 2006 The Android Open Source Project 3 * Copyright 2006 The Android Open Source Project
4 * 4 *
5 * Use of this source code is governed by a BSD-style license that can be 5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file. 6 * found in the LICENSE file.
7 */ 7 */
8 8
9 9
10 #include "SkStream.h" 10 #include "SkStream.h"
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 return this->readU16(); 59 return this->readU16();
60 } else if (SK_BYTE_SENTINEL_FOR_U32 == byte) { 60 } else if (SK_BYTE_SENTINEL_FOR_U32 == byte) {
61 return this->readU32(); 61 return this->readU32();
62 } else { 62 } else {
63 return byte; 63 return byte;
64 } 64 }
65 } 65 }
66 66
67 SkData* SkStream::readData() { 67 SkData* SkStream::readData() {
68 size_t size = this->readU32(); 68 size_t size = this->readU32();
69 if (0 == size) { 69 return size ? this->readIntoData(size) : SkData::NewEmpty();
70 return SkData::NewEmpty(); 70 }
71 } else { 71
72 SkData* data = SkData::NewUninitialized(size); 72 SkData* SkStream::readIntoData(size_t size) {
73 this->read(data->writable_data(), size); 73 SkAutoDataUnref data(SkData::NewUninitialized(size));
74 return data; 74 if (this->read(data->writable_data(), size) != size) {
75 return NULL;
75 } 76 }
77 return data.detach();
76 } 78 }
77 79
78 //////////////////////////////////////////////////////////////////////////////// ////// 80 //////////////////////////////////////////////////////////////////////////////// //////
79 81
80 SkWStream::~SkWStream() 82 SkWStream::~SkWStream()
81 { 83 {
82 } 84 }
83 85
84 void SkWStream::newline() 86 void SkWStream::newline()
85 { 87 {
(...skipping 797 matching lines...) Expand 10 before | Expand all | Expand 10 after
883 void* dst = storage->reset(length); 885 void* dst = storage->reset(length);
884 tempStream.copyTo(dst); 886 tempStream.copyTo(dst);
885 return length; 887 return length;
886 } 888 }
887 889
888 // Declared in SkStreamPriv.h: 890 // Declared in SkStreamPriv.h:
889 SkData* SkCopyStreamToData(SkStream* stream) { 891 SkData* SkCopyStreamToData(SkStream* stream) {
890 SkASSERT(stream != NULL); 892 SkASSERT(stream != NULL);
891 893
892 if (stream->hasLength()) { 894 if (stream->hasLength()) {
893 const size_t length = stream->getLength(); 895 return stream->readIntoData(stream->getLength());
894 SkData* data = SkData::NewUninitialized(length);
895 if (stream->read(data->writable_data(), length) != length) {
896 data->unref();
897 data = NULL;
898 }
899 return data;
900 } 896 }
901 897
902 SkDynamicMemoryWStream tempStream; 898 SkDynamicMemoryWStream tempStream;
903 const size_t bufferSize = 4096; 899 const size_t bufferSize = 4096;
904 char buffer[bufferSize]; 900 char buffer[bufferSize];
905 do { 901 do {
906 size_t bytesRead = stream->read(buffer, bufferSize); 902 size_t bytesRead = stream->read(buffer, bufferSize);
907 tempStream.write(buffer, bytesRead); 903 tempStream.write(buffer, bytesRead);
908 } while (!stream->isAtEnd()); 904 } while (!stream->isAtEnd());
909 return tempStream.copyToData(); 905 return tempStream.copyToData();
910 } 906 }
911 907
912 SkStreamRewindable* SkStreamRewindableFromSkStream(SkStream* stream) { 908 SkStreamRewindable* SkStreamRewindableFromSkStream(SkStream* stream) {
913 if (!stream) { 909 if (!stream) {
914 return NULL; 910 return NULL;
915 } 911 }
916 SkAutoTUnref<SkStreamRewindable> dupStream(stream->duplicate()); 912 SkAutoTUnref<SkStreamRewindable> dupStream(stream->duplicate());
917 if (dupStream) { 913 if (dupStream) {
918 return dupStream.detach(); 914 return dupStream.detach();
919 } 915 }
920 stream->rewind(); 916 stream->rewind();
921 if (stream->hasLength()) { 917 if (stream->hasLength()) {
922 size_t length = stream->getLength(); 918 size_t length = stream->getLength();
923 if (stream->hasPosition()) { // If stream has length, but can't rewind. 919 if (stream->hasPosition()) { // If stream has length, but can't rewind.
924 length -= stream->getPosition(); 920 length -= stream->getPosition();
925 } 921 }
926 SkAutoTUnref<SkData> data(SkData::NewUninitialized(length)); 922 SkAutoTUnref<SkData> data(stream->readIntoData(length));
927 SkDEBUGCODE(size_t read =) stream->read(data->writable_data(), length);
928 SkASSERT(length == read);
929 return SkNEW_ARGS(SkMemoryStream, (data.get())); 923 return SkNEW_ARGS(SkMemoryStream, (data.get()));
930 } 924 }
931 SkDynamicMemoryWStream tempStream; 925 SkDynamicMemoryWStream tempStream;
932 const size_t bufferSize = 4096; 926 const size_t bufferSize = 4096;
933 char buffer[bufferSize]; 927 char buffer[bufferSize];
934 do { 928 do {
935 size_t bytesRead = stream->read(buffer, bufferSize); 929 size_t bytesRead = stream->read(buffer, bufferSize);
936 tempStream.write(buffer, bytesRead); 930 tempStream.write(buffer, bytesRead);
937 } while (!stream->isAtEnd()); 931 } while (!stream->isAtEnd());
938 return tempStream.detachAsStream(); // returns a SkBlockMemoryStream, 932 return tempStream.detachAsStream(); // returns a SkBlockMemoryStream,
939 // cheaper than copying to SkData 933 // cheaper than copying to SkData
940 } 934 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698