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

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

Issue 1490923005: Allow SkStream::peek() to partially succeed (Closed) Base URL: https://skia.googlesource.com/skia.git@wbmp
Patch Set: Use the correct return type Created 5 years 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
« no previous file with comments | « include/core/SkStream.h ('k') | src/utils/SkFrontBufferedStream.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 349 matching lines...) Expand 10 before | Expand all | Expand 10 after
360 if (size > dataSize - fOffset) { 360 if (size > dataSize - fOffset) {
361 size = dataSize - fOffset; 361 size = dataSize - fOffset;
362 } 362 }
363 if (buffer) { 363 if (buffer) {
364 memcpy(buffer, fData->bytes() + fOffset, size); 364 memcpy(buffer, fData->bytes() + fOffset, size);
365 } 365 }
366 fOffset += size; 366 fOffset += size;
367 return size; 367 return size;
368 } 368 }
369 369
370 bool SkMemoryStream::peek(void* buffer, size_t size) const { 370 size_t SkMemoryStream::peek(void* buffer, size_t size) const {
371 SkASSERT(buffer != nullptr); 371 SkASSERT(buffer != nullptr);
372 const size_t position = fOffset; 372
373 if (size > fData->size() - position) { 373 const size_t currentOffset = fOffset;
374 // The stream is not large enough to satisfy this request.
375 return false;
376 }
377 SkMemoryStream* nonConstThis = const_cast<SkMemoryStream*>(this); 374 SkMemoryStream* nonConstThis = const_cast<SkMemoryStream*>(this);
378 SkDEBUGCODE(const size_t bytesRead =) nonConstThis->read(buffer, size); 375 const size_t bytesRead = nonConstThis->read(buffer, size);
379 SkASSERT(bytesRead == size); 376 nonConstThis->fOffset = currentOffset;
380 nonConstThis->fOffset = position; 377 return bytesRead;
381 return true;
382 } 378 }
383 379
384 bool SkMemoryStream::isAtEnd() const { 380 bool SkMemoryStream::isAtEnd() const {
385 return fOffset == fData->size(); 381 return fOffset == fData->size();
386 } 382 }
387 383
388 bool SkMemoryStream::rewind() { 384 bool SkMemoryStream::rewind() {
389 fOffset = 0; 385 fOffset = 0;
390 return true; 386 return true;
391 } 387 }
(...skipping 326 matching lines...) Expand 10 before | Expand all | Expand 10 after
718 fCurrentOffset = 0; 714 fCurrentOffset = 0;
719 } 715 }
720 SkASSERT(false); 716 SkASSERT(false);
721 return 0; 717 return 0;
722 } 718 }
723 719
724 bool isAtEnd() const override { 720 bool isAtEnd() const override {
725 return fOffset == fSize; 721 return fOffset == fSize;
726 } 722 }
727 723
728 bool peek(void* buff, size_t size) const override { 724 size_t peek(void* buff, size_t bytesToPeek) const override {
729 SkASSERT(buff != nullptr); 725 SkASSERT(buff != nullptr);
730 if (fOffset + size > fSize) { 726
731 return false; 727 bytesToPeek = SkTMin(bytesToPeek, fSize - fOffset);
732 } 728
729 size_t bytesLeftToPeek = bytesToPeek;
733 char* buffer = static_cast<char*>(buff); 730 char* buffer = static_cast<char*>(buff);
734 const SkDynamicMemoryWStream::Block* current = fCurrent; 731 const SkDynamicMemoryWStream::Block* current = fCurrent;
735 size_t currentOffset = fCurrentOffset; 732 size_t currentOffset = fCurrentOffset;
736 while (size) { 733 while (bytesLeftToPeek) {
737 SkASSERT(current); 734 SkASSERT(current);
738 size_t bytesFromCurrent = 735 size_t bytesFromCurrent =
739 SkTMin(current->written() - currentOffset, size); 736 SkTMin(current->written() - currentOffset, bytesLeftToPeek);
740 memcpy(buffer, current->start() + currentOffset, bytesFromCurrent); 737 memcpy(buffer, current->start() + currentOffset, bytesFromCurrent);
741 size -= bytesFromCurrent; 738 bytesLeftToPeek -= bytesFromCurrent;
742 buffer += bytesFromCurrent; 739 buffer += bytesFromCurrent;
743 current = current->fNext; 740 current = current->fNext;
744 currentOffset = 0; 741 currentOffset = 0;
745 } 742 }
746 return true; 743 return bytesToPeek;
747 } 744 }
748 745
749 bool rewind() override { 746 bool rewind() override {
750 fCurrent = fBlockMemory->fHead; 747 fCurrent = fBlockMemory->fHead;
751 fOffset = 0; 748 fOffset = 0;
752 fCurrentOffset = 0; 749 fCurrentOffset = 0;
753 return true; 750 return true;
754 } 751 }
755 752
756 SkBlockMemoryStream* duplicate() const override { 753 SkBlockMemoryStream* duplicate() const override {
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after
972 while (true) { 969 while (true) {
973 count = input->read(scratch, sizeof(scratch)); 970 count = input->read(scratch, sizeof(scratch));
974 if (0 == count) { 971 if (0 == count) {
975 return true; 972 return true;
976 } 973 }
977 if (!out->write(scratch, count)) { 974 if (!out->write(scratch, count)) {
978 return false; 975 return false;
979 } 976 }
980 } 977 }
981 } 978 }
OLDNEW
« no previous file with comments | « include/core/SkStream.h ('k') | src/utils/SkFrontBufferedStream.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698