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

Side by Side Diff: include/core/SkStream.h

Issue 1044953002: Add a method to read a stream without advancing it. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Add error messages for SkStream::peek(). Created 5 years, 8 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
« no previous file with comments | « no previous file | 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 * Copyright 2006 The Android Open Source Project 2 * Copyright 2006 The Android Open Source Project
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #ifndef SkStream_DEFINED 8 #ifndef SkStream_DEFINED
9 #define SkStream_DEFINED 9 #define SkStream_DEFINED
10 10
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
56 */ 56 */
57 virtual size_t read(void* buffer, size_t size) = 0; 57 virtual size_t read(void* buffer, size_t size) = 0;
58 58
59 /** Skip size number of bytes. 59 /** Skip size number of bytes.
60 * @return the actual number bytes that could be skipped. 60 * @return the actual number bytes that could be skipped.
61 */ 61 */
62 size_t skip(size_t size) { 62 size_t skip(size_t size) {
63 return this->read(NULL, size); 63 return this->read(NULL, size);
64 } 64 }
65 65
66 /**
67 * Return value for peek() if the stream does not support peeking, or if
68 * it cannot from this offset.
69 */
70 static const size_t PEEK_UNSUPPORTED = (size_t) -1;
bungeman-skia 2015/03/31 22:04:48 I think we tend toward enums with 'k' prefixes (th
scroggo 2015/04/01 18:38:31 I'm fine with renaming them to use 'k' prefixes (p
71
72 /**
73 * Return value for peek() if the stream encountered a fatal error. The
74 * stream is no unreliable.
75 */
76 static const size_t PEEK_FAILURE = (size_t) -2;
77
78 /**
79 * Attempt to peek at size bytes.
80 * If this stream supports peeking, copy up to size bytes into buffer. If
81 * less than size is returned, the stream is less than size bytes from the
82 * end or the stream has reached its limit for how many bytes it can peek.
83 * If there is a fatal error, PEEK_FAILURE is returned, and the stream
84 * cannot be used anymore.
85 * After this call, the stream will have the same state it did beforehand,
86 * unless there was an error (and PEEK_FAILURE was returned).
87 * If peeking is unsupported, returns PEEK_UNSUPPORTED
88 * @param buffer Must not be NULL. Destination to copy bytes.
89 * @param size Number of bytes to copy.
90 * @return the number of bytes actually read, or an error message.
91 */
92 virtual size_t peek(void* buffer, size_t size) { return PEEK_UNSUPPORTED; }
bungeman-skia 2015/03/31 22:04:48 We already have the pattern of either returning bo
93
66 /** Returns true when all the bytes in the stream have been read. 94 /** Returns true when all the bytes in the stream have been read.
67 * This may return true early (when there are no more bytes to be read) 95 * This may return true early (when there are no more bytes to be read)
68 * or late (after the first unsuccessful read). 96 * or late (after the first unsuccessful read).
69 */ 97 */
70 virtual bool isAtEnd() const = 0; 98 virtual bool isAtEnd() const = 0;
71 99
72 int8_t readS8(); 100 int8_t readS8();
73 int16_t readS16(); 101 int16_t readS16();
74 int32_t readS32(); 102 int32_t readS32();
75 103
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
137 /** SkStreamSeekable is a SkStreamRewindable for which position, seek, move, and fork are required. */ 165 /** SkStreamSeekable is a SkStreamRewindable for which position, seek, move, and fork are required. */
138 class SK_API SkStreamSeekable : public SkStreamRewindable { 166 class SK_API SkStreamSeekable : public SkStreamRewindable {
139 public: 167 public:
140 SkStreamSeekable* duplicate() const override = 0; 168 SkStreamSeekable* duplicate() const override = 0;
141 169
142 bool hasPosition() const override { return true; } 170 bool hasPosition() const override { return true; }
143 size_t getPosition() const override = 0; 171 size_t getPosition() const override = 0;
144 bool seek(size_t position) override = 0; 172 bool seek(size_t position) override = 0;
145 bool move(long offset) override = 0; 173 bool move(long offset) override = 0;
146 SkStreamSeekable* fork() const override = 0; 174 SkStreamSeekable* fork() const override = 0;
175 // Default implementation reads and then resets the position.
176 size_t peek(void* buffer, size_t size) override {
177 SkASSERT(buffer != NULL);
178 const size_t position = this->getPosition();
179 const size_t bytesRead = this->read(buffer, size);
180 if (!this->seek(position)) {
181 return PEEK_FAILURE;
182 }
183 return bytesRead;
184 }
147 }; 185 };
148 186
149 /** SkStreamAsset is a SkStreamSeekable for which getLength is required. */ 187 /** SkStreamAsset is a SkStreamSeekable for which getLength is required. */
150 class SK_API SkStreamAsset : public SkStreamSeekable { 188 class SK_API SkStreamAsset : public SkStreamSeekable {
151 public: 189 public:
152 SkStreamAsset* duplicate() const override = 0; 190 SkStreamAsset* duplicate() const override = 0;
153 SkStreamAsset* fork() const override = 0; 191 SkStreamAsset* fork() const override = 0;
154 192
155 bool hasLength() const override { return true; } 193 bool hasLength() const override { return true; }
156 size_t getLength() const override = 0; 194 size_t getLength() const override = 0;
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after
437 475
438 private: 476 private:
439 size_t fBytesWritten; 477 size_t fBytesWritten;
440 typedef SkWStream INHERITED; 478 typedef SkWStream INHERITED;
441 }; 479 };
442 480
443 // for now 481 // for now
444 typedef SkFILEStream SkURLStream; 482 typedef SkFILEStream SkURLStream;
445 483
446 #endif 484 #endif
OLDNEW
« no previous file with comments | « no previous file | src/utils/SkFrontBufferedStream.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698