OLD | NEW |
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 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 size_t len = 1; | 154 size_t len = 1; |
155 if (value <= SK_MAX_BYTE_FOR_U8) { | 155 if (value <= SK_MAX_BYTE_FOR_U8) { |
156 data[0] = value; | 156 data[0] = value; |
157 len = 1; | 157 len = 1; |
158 } else if (value <= 0xFFFF) { | 158 } else if (value <= 0xFFFF) { |
159 uint16_t value16 = value; | 159 uint16_t value16 = value; |
160 data[0] = SK_BYTE_SENTINEL_FOR_U16; | 160 data[0] = SK_BYTE_SENTINEL_FOR_U16; |
161 memcpy(&data[1], &value16, 2); | 161 memcpy(&data[1], &value16, 2); |
162 len = 3; | 162 len = 3; |
163 } else { | 163 } else { |
164 uint32_t value32 = value; | 164 uint32_t value32 = SkToU32(value); |
165 data[0] = SK_BYTE_SENTINEL_FOR_U32; | 165 data[0] = SK_BYTE_SENTINEL_FOR_U32; |
166 memcpy(&data[1], &value32, 4); | 166 memcpy(&data[1], &value32, 4); |
167 len = 5; | 167 len = 5; |
168 } | 168 } |
169 return this->write(data, len); | 169 return this->write(data, len); |
170 } | 170 } |
171 | 171 |
172 bool SkWStream::writeStream(SkStream* stream, size_t length) { | 172 bool SkWStream::writeStream(SkStream* stream, size_t length) { |
173 char scratch[1024]; | 173 char scratch[1024]; |
174 const size_t MAX = sizeof(scratch); | 174 const size_t MAX = sizeof(scratch); |
175 | 175 |
176 while (length != 0) { | 176 while (length != 0) { |
177 size_t n = length; | 177 size_t n = length; |
178 if (n > MAX) { | 178 if (n > MAX) { |
179 n = MAX; | 179 n = MAX; |
180 } | 180 } |
181 stream->read(scratch, n); | 181 stream->read(scratch, n); |
182 if (!this->write(scratch, n)) { | 182 if (!this->write(scratch, n)) { |
183 return false; | 183 return false; |
184 } | 184 } |
185 length -= n; | 185 length -= n; |
186 } | 186 } |
187 return true; | 187 return true; |
188 } | 188 } |
189 | 189 |
190 bool SkWStream::writeData(const SkData* data) { | 190 bool SkWStream::writeData(const SkData* data) { |
191 if (data) { | 191 if (data) { |
192 this->write32(data->size()); | 192 this->write32(SkToU32(data->size())); |
193 this->write(data->data(), data->size()); | 193 this->write(data->data(), data->size()); |
194 } else { | 194 } else { |
195 this->write32(0); | 195 this->write32(0); |
196 } | 196 } |
197 return true; | 197 return true; |
198 } | 198 } |
199 | 199 |
200 /////////////////////////////////////////////////////////////////////////////// | 200 /////////////////////////////////////////////////////////////////////////////// |
201 | 201 |
202 SkFILEStream::SkFILEStream(const char file[]) : fName(file), fOwnership(kCallerP
asses_Ownership) { | 202 SkFILEStream::SkFILEStream(const char file[]) : fName(file), fOwnership(kCallerP
asses_Ownership) { |
(...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
474 } | 474 } |
475 } | 475 } |
476 | 476 |
477 //////////////////////////////////////////////////////////////////////// | 477 //////////////////////////////////////////////////////////////////////// |
478 | 478 |
479 SkMemoryWStream::SkMemoryWStream(void* buffer, size_t size) | 479 SkMemoryWStream::SkMemoryWStream(void* buffer, size_t size) |
480 : fBuffer((char*)buffer), fMaxLength(size), fBytesWritten(0) | 480 : fBuffer((char*)buffer), fMaxLength(size), fBytesWritten(0) |
481 { | 481 { |
482 } | 482 } |
483 | 483 |
484 bool SkMemoryWStream::write(const void* buffer, size_t size) | 484 bool SkMemoryWStream::write(const void* buffer, size_t size) { |
485 { | 485 size = SkTMin(size, fMaxLength - fBytesWritten); |
486 size = SkMin32(size, fMaxLength - fBytesWritten); | 486 if (size > 0) { |
487 if (size > 0) | |
488 { | |
489 memcpy(fBuffer + fBytesWritten, buffer, size); | 487 memcpy(fBuffer + fBytesWritten, buffer, size); |
490 fBytesWritten += size; | 488 fBytesWritten += size; |
491 return true; | 489 return true; |
492 } | 490 } |
493 return false; | 491 return false; |
494 } | 492 } |
495 | 493 |
496 //////////////////////////////////////////////////////////////////////// | 494 //////////////////////////////////////////////////////////////////////// |
497 | 495 |
498 #define SkDynamicMemoryWStream_MinBlockSize 256 | 496 #define SkDynamicMemoryWStream_MinBlockSize 256 |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
551 bool SkDynamicMemoryWStream::write(const void* buffer, size_t count) | 549 bool SkDynamicMemoryWStream::write(const void* buffer, size_t count) |
552 { | 550 { |
553 if (count > 0) { | 551 if (count > 0) { |
554 this->invalidateCopy(); | 552 this->invalidateCopy(); |
555 | 553 |
556 fBytesWritten += count; | 554 fBytesWritten += count; |
557 | 555 |
558 size_t size; | 556 size_t size; |
559 | 557 |
560 if (fTail != NULL && fTail->avail() > 0) { | 558 if (fTail != NULL && fTail->avail() > 0) { |
561 size = SkMin32(fTail->avail(), count); | 559 size = SkTMin(fTail->avail(), count); |
562 buffer = fTail->append(buffer, size); | 560 buffer = fTail->append(buffer, size); |
563 SkASSERT(count >= size); | 561 SkASSERT(count >= size); |
564 count -= size; | 562 count -= size; |
565 if (count == 0) | 563 if (count == 0) |
566 return true; | 564 return true; |
567 } | 565 } |
568 | 566 |
569 size = SkMax32(count, SkDynamicMemoryWStream_MinBlockSize); | 567 size = SkTMax<size_t>(count, SkDynamicMemoryWStream_MinBlockSize); |
570 Block* block = (Block*)sk_malloc_throw(sizeof(Block) + size); | 568 Block* block = (Block*)sk_malloc_throw(sizeof(Block) + size); |
571 block->init(size); | 569 block->init(size); |
572 block->append(buffer, count); | 570 block->append(buffer, count); |
573 | 571 |
574 if (fTail != NULL) | 572 if (fTail != NULL) |
575 fTail->fNext = block; | 573 fTail->fNext = block; |
576 else | 574 else |
577 fHead = fTail = block; | 575 fHead = fTail = block; |
578 fTail = block; | 576 fTail = block; |
579 } | 577 } |
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
846 | 844 |
847 // If we get here, then our attempt at using mmap failed, so try normal | 845 // If we get here, then our attempt at using mmap failed, so try normal |
848 // file access. | 846 // file access. |
849 SkFILEStream* stream = SkNEW_ARGS(SkFILEStream, (path)); | 847 SkFILEStream* stream = SkNEW_ARGS(SkFILEStream, (path)); |
850 if (!stream->isValid()) { | 848 if (!stream->isValid()) { |
851 stream->unref(); | 849 stream->unref(); |
852 stream = NULL; | 850 stream = NULL; |
853 } | 851 } |
854 return stream; | 852 return stream; |
855 } | 853 } |
OLD | NEW |