OLD | NEW |
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 Loading... |
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 enum PeekResult : size_t { |
| 67 /** |
| 68 * Return value for peek() if the stream does not support peeking, or i
f |
| 69 * it cannot from this offset. |
| 70 */ |
| 71 kUnsupported_PeekResult = (size_t) -1, |
| 72 |
| 73 /** |
| 74 * Return value for peek() if the stream encountered a fatal error. The |
| 75 * stream is no unreliable. |
| 76 */ |
| 77 kFailed_PeekResult = (size_t) -2, |
| 78 }; |
| 79 |
| 80 |
| 81 /** |
| 82 * Attempt to peek at size bytes. |
| 83 * If this stream supports peeking, copy up to size bytes into buffer. If |
| 84 * less than size is returned, the stream is less than size bytes from the |
| 85 * end or the stream has reached its limit for how many bytes it can peek. |
| 86 * If there is a fatal error, kFailed_PeekResult is returned, and the strea
m |
| 87 * cannot be used anymore. |
| 88 * After this call, the stream will have the same state it did beforehand, |
| 89 * unless there was an error (and kFailed_PeekResult was returned). |
| 90 * If peeking is unsupported, returns kUnsupported_PeekResult |
| 91 * @param buffer Must not be NULL. Destination to copy bytes. |
| 92 * @param size Number of bytes to copy. |
| 93 * @return the number of bytes actually read, or an error message. |
| 94 */ |
| 95 virtual size_t peek(void* buffer, size_t size) { return kUnsupported_PeekRes
ult; } |
| 96 |
66 /** Returns true when all the bytes in the stream have been read. | 97 /** 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) | 98 * This may return true early (when there are no more bytes to be read) |
68 * or late (after the first unsuccessful read). | 99 * or late (after the first unsuccessful read). |
69 */ | 100 */ |
70 virtual bool isAtEnd() const = 0; | 101 virtual bool isAtEnd() const = 0; |
71 | 102 |
72 int8_t readS8(); | 103 int8_t readS8(); |
73 int16_t readS16(); | 104 int16_t readS16(); |
74 int32_t readS32(); | 105 int32_t readS32(); |
75 | 106 |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 /** SkStreamSeekable is a SkStreamRewindable for which position, seek, move, and
fork are required. */ | 168 /** SkStreamSeekable is a SkStreamRewindable for which position, seek, move, and
fork are required. */ |
138 class SK_API SkStreamSeekable : public SkStreamRewindable { | 169 class SK_API SkStreamSeekable : public SkStreamRewindable { |
139 public: | 170 public: |
140 SkStreamSeekable* duplicate() const override = 0; | 171 SkStreamSeekable* duplicate() const override = 0; |
141 | 172 |
142 bool hasPosition() const override { return true; } | 173 bool hasPosition() const override { return true; } |
143 size_t getPosition() const override = 0; | 174 size_t getPosition() const override = 0; |
144 bool seek(size_t position) override = 0; | 175 bool seek(size_t position) override = 0; |
145 bool move(long offset) override = 0; | 176 bool move(long offset) override = 0; |
146 SkStreamSeekable* fork() const override = 0; | 177 SkStreamSeekable* fork() const override = 0; |
| 178 // Default implementation reads and then resets the position. |
| 179 size_t peek(void* buffer, size_t size) override { |
| 180 SkASSERT(buffer != NULL); |
| 181 const size_t position = this->getPosition(); |
| 182 const size_t bytesRead = this->read(buffer, size); |
| 183 if (!this->seek(position)) { |
| 184 return kFailed_PeekResult; |
| 185 } |
| 186 return bytesRead; |
| 187 } |
147 }; | 188 }; |
148 | 189 |
149 /** SkStreamAsset is a SkStreamSeekable for which getLength is required. */ | 190 /** SkStreamAsset is a SkStreamSeekable for which getLength is required. */ |
150 class SK_API SkStreamAsset : public SkStreamSeekable { | 191 class SK_API SkStreamAsset : public SkStreamSeekable { |
151 public: | 192 public: |
152 SkStreamAsset* duplicate() const override = 0; | 193 SkStreamAsset* duplicate() const override = 0; |
153 SkStreamAsset* fork() const override = 0; | 194 SkStreamAsset* fork() const override = 0; |
154 | 195 |
155 bool hasLength() const override { return true; } | 196 bool hasLength() const override { return true; } |
156 size_t getLength() const override = 0; | 197 size_t getLength() const override = 0; |
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
437 | 478 |
438 private: | 479 private: |
439 size_t fBytesWritten; | 480 size_t fBytesWritten; |
440 typedef SkWStream INHERITED; | 481 typedef SkWStream INHERITED; |
441 }; | 482 }; |
442 | 483 |
443 // for now | 484 // for now |
444 typedef SkFILEStream SkURLStream; | 485 typedef SkFILEStream SkURLStream; |
445 | 486 |
446 #endif | 487 #endif |
OLD | NEW |