OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
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 #include "SkFrontBufferedStream.h" | 8 #include "SkFrontBufferedStream.h" |
9 #include "SkStream.h" | 9 #include "SkStream.h" |
10 #include "SkTemplates.h" | 10 #include "SkTemplates.h" |
(...skipping 12 matching lines...) Expand all Loading... |
23 bool rewind() override; | 23 bool rewind() override; |
24 | 24 |
25 bool hasPosition() const override { return true; } | 25 bool hasPosition() const override { return true; } |
26 | 26 |
27 size_t getPosition() const override { return fOffset; } | 27 size_t getPosition() const override { return fOffset; } |
28 | 28 |
29 bool hasLength() const override { return fHasLength; } | 29 bool hasLength() const override { return fHasLength; } |
30 | 30 |
31 size_t getLength() const override { return fLength; } | 31 size_t getLength() const override { return fLength; } |
32 | 32 |
33 SkStreamRewindable* duplicate() const override { return NULL; } | 33 SkStreamRewindable* duplicate() const override { return nullptr; } |
34 | 34 |
35 private: | 35 private: |
36 SkAutoTDelete<SkStream> fStream; | 36 SkAutoTDelete<SkStream> fStream; |
37 const bool fHasLength; | 37 const bool fHasLength; |
38 const size_t fLength; | 38 const size_t fLength; |
39 // Current offset into the stream. Always >= 0. | 39 // Current offset into the stream. Always >= 0. |
40 size_t fOffset; | 40 size_t fOffset; |
41 // Amount that has been buffered by calls to read. Will always be less than | 41 // Amount that has been buffered by calls to read. Will always be less than |
42 // fBufferSize. | 42 // fBufferSize. |
43 size_t fBufferedSoFar; | 43 size_t fBufferedSoFar; |
44 // Total size of the buffer. | 44 // Total size of the buffer. |
45 const size_t fBufferSize; | 45 const size_t fBufferSize; |
46 // FIXME: SkAutoTMalloc throws on failure. Instead, Create should return a | 46 // FIXME: SkAutoTMalloc throws on failure. Instead, Create should return a |
47 // NULL stream. | 47 // nullptr stream. |
48 SkAutoTMalloc<char> fBuffer; | 48 SkAutoTMalloc<char> fBuffer; |
49 | 49 |
50 // Read up to size bytes from already buffered data, and copy to | 50 // Read up to size bytes from already buffered data, and copy to |
51 // dst, if non-NULL. Updates fOffset. Assumes that fOffset is less | 51 // dst, if non-nullptr. Updates fOffset. Assumes that fOffset is less |
52 // than fBufferedSoFar. | 52 // than fBufferedSoFar. |
53 size_t readFromBuffer(char* dst, size_t size); | 53 size_t readFromBuffer(char* dst, size_t size); |
54 | 54 |
55 // Buffer up to size bytes from the stream, and copy to dst if non- | 55 // Buffer up to size bytes from the stream, and copy to dst if non- |
56 // NULL. Updates fOffset and fBufferedSoFar. Assumes that fOffset is | 56 // nullptr. Updates fOffset and fBufferedSoFar. Assumes that fOffset is |
57 // less than fBufferedSoFar, and size is greater than 0. | 57 // less than fBufferedSoFar, and size is greater than 0. |
58 size_t bufferAndWriteTo(char* dst, size_t size); | 58 size_t bufferAndWriteTo(char* dst, size_t size); |
59 | 59 |
60 // Read up to size bytes directly from the stream and into dst if non- | 60 // Read up to size bytes directly from the stream and into dst if non- |
61 // NULL. Updates fOffset. Assumes fOffset is at or beyond the buffered | 61 // nullptr. Updates fOffset. Assumes fOffset is at or beyond the buffered |
62 // data, and size is greater than 0. | 62 // data, and size is greater than 0. |
63 size_t readDirectlyFromStream(char* dst, size_t size); | 63 size_t readDirectlyFromStream(char* dst, size_t size); |
64 | 64 |
65 typedef SkStream INHERITED; | 65 typedef SkStream INHERITED; |
66 }; | 66 }; |
67 | 67 |
68 SkStreamRewindable* SkFrontBufferedStream::Create(SkStream* stream, size_t buffe
rSize) { | 68 SkStreamRewindable* SkFrontBufferedStream::Create(SkStream* stream, size_t buffe
rSize) { |
69 if (NULL == stream) { | 69 if (nullptr == stream) { |
70 return NULL; | 70 return nullptr; |
71 } | 71 } |
72 return new FrontBufferedStream(stream, bufferSize); | 72 return new FrontBufferedStream(stream, bufferSize); |
73 } | 73 } |
74 | 74 |
75 FrontBufferedStream::FrontBufferedStream(SkStream* stream, size_t bufferSize) | 75 FrontBufferedStream::FrontBufferedStream(SkStream* stream, size_t bufferSize) |
76 : fStream(stream) | 76 : fStream(stream) |
77 , fHasLength(stream->hasPosition() && stream->hasLength()) | 77 , fHasLength(stream->hasPosition() && stream->hasLength()) |
78 , fLength(stream->getLength() - stream->getPosition()) | 78 , fLength(stream->getLength() - stream->getPosition()) |
79 , fOffset(0) | 79 , fOffset(0) |
80 , fBufferedSoFar(0) | 80 , fBufferedSoFar(0) |
(...skipping 18 matching lines...) Expand all Loading... |
99 } | 99 } |
100 return false; | 100 return false; |
101 } | 101 } |
102 | 102 |
103 size_t FrontBufferedStream::readFromBuffer(char* dst, size_t size) { | 103 size_t FrontBufferedStream::readFromBuffer(char* dst, size_t size) { |
104 SkASSERT(fOffset < fBufferedSoFar); | 104 SkASSERT(fOffset < fBufferedSoFar); |
105 // Some data has already been copied to fBuffer. Read up to the | 105 // Some data has already been copied to fBuffer. Read up to the |
106 // lesser of the size requested and the remainder of the buffered | 106 // lesser of the size requested and the remainder of the buffered |
107 // data. | 107 // data. |
108 const size_t bytesToCopy = SkTMin(size, fBufferedSoFar - fOffset); | 108 const size_t bytesToCopy = SkTMin(size, fBufferedSoFar - fOffset); |
109 if (dst != NULL) { | 109 if (dst != nullptr) { |
110 memcpy(dst, fBuffer + fOffset, bytesToCopy); | 110 memcpy(dst, fBuffer + fOffset, bytesToCopy); |
111 } | 111 } |
112 | 112 |
113 // Update fOffset to the new position. It is guaranteed to be | 113 // Update fOffset to the new position. It is guaranteed to be |
114 // within the buffered data. | 114 // within the buffered data. |
115 fOffset += bytesToCopy; | 115 fOffset += bytesToCopy; |
116 SkASSERT(fOffset <= fBufferedSoFar); | 116 SkASSERT(fOffset <= fBufferedSoFar); |
117 | 117 |
118 return bytesToCopy; | 118 return bytesToCopy; |
119 } | 119 } |
120 | 120 |
121 size_t FrontBufferedStream::bufferAndWriteTo(char* dst, size_t size) { | 121 size_t FrontBufferedStream::bufferAndWriteTo(char* dst, size_t size) { |
122 SkASSERT(size > 0); | 122 SkASSERT(size > 0); |
123 SkASSERT(fOffset >= fBufferedSoFar); | 123 SkASSERT(fOffset >= fBufferedSoFar); |
124 SkASSERT(fBuffer); | 124 SkASSERT(fBuffer); |
125 // Data needs to be buffered. Buffer up to the lesser of the size requested | 125 // Data needs to be buffered. Buffer up to the lesser of the size requested |
126 // and the remainder of the max buffer size. | 126 // and the remainder of the max buffer size. |
127 const size_t bytesToBuffer = SkTMin(size, fBufferSize - fBufferedSoFar); | 127 const size_t bytesToBuffer = SkTMin(size, fBufferSize - fBufferedSoFar); |
128 char* buffer = fBuffer + fOffset; | 128 char* buffer = fBuffer + fOffset; |
129 const size_t buffered = fStream->read(buffer, bytesToBuffer); | 129 const size_t buffered = fStream->read(buffer, bytesToBuffer); |
130 | 130 |
131 fBufferedSoFar += buffered; | 131 fBufferedSoFar += buffered; |
132 fOffset = fBufferedSoFar; | 132 fOffset = fBufferedSoFar; |
133 SkASSERT(fBufferedSoFar <= fBufferSize); | 133 SkASSERT(fBufferedSoFar <= fBufferSize); |
134 | 134 |
135 // Copy the buffer to the destination buffer and update the amount read. | 135 // Copy the buffer to the destination buffer and update the amount read. |
136 if (dst != NULL) { | 136 if (dst != nullptr) { |
137 memcpy(dst, buffer, buffered); | 137 memcpy(dst, buffer, buffered); |
138 } | 138 } |
139 | 139 |
140 return buffered; | 140 return buffered; |
141 } | 141 } |
142 | 142 |
143 size_t FrontBufferedStream::readDirectlyFromStream(char* dst, size_t size) { | 143 size_t FrontBufferedStream::readDirectlyFromStream(char* dst, size_t size) { |
144 SkASSERT(size > 0); | 144 SkASSERT(size > 0); |
145 // If we get here, we have buffered all that can be buffered. | 145 // If we get here, we have buffered all that can be buffered. |
146 SkASSERT(fBufferSize == fBufferedSoFar && fOffset >= fBufferSize); | 146 SkASSERT(fBufferSize == fBufferedSoFar && fOffset >= fBufferSize); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 const size_t start = fOffset; | 178 const size_t start = fOffset; |
179 | 179 |
180 // First, read any data that was previously buffered. | 180 // First, read any data that was previously buffered. |
181 if (fOffset < fBufferedSoFar) { | 181 if (fOffset < fBufferedSoFar) { |
182 const size_t bytesCopied = this->readFromBuffer(dst, size); | 182 const size_t bytesCopied = this->readFromBuffer(dst, size); |
183 | 183 |
184 // Update the remaining number of bytes needed to read | 184 // Update the remaining number of bytes needed to read |
185 // and the destination buffer. | 185 // and the destination buffer. |
186 size -= bytesCopied; | 186 size -= bytesCopied; |
187 SkASSERT(size + (fOffset - start) == totalSize); | 187 SkASSERT(size + (fOffset - start) == totalSize); |
188 if (dst != NULL) { | 188 if (dst != nullptr) { |
189 dst += bytesCopied; | 189 dst += bytesCopied; |
190 } | 190 } |
191 } | 191 } |
192 | 192 |
193 // Buffer any more data that should be buffered, and copy it to the | 193 // Buffer any more data that should be buffered, and copy it to the |
194 // destination. | 194 // destination. |
195 if (size > 0 && fBufferedSoFar < fBufferSize && !fStream->isAtEnd()) { | 195 if (size > 0 && fBufferedSoFar < fBufferSize && !fStream->isAtEnd()) { |
196 const size_t buffered = this->bufferAndWriteTo(dst, size); | 196 const size_t buffered = this->bufferAndWriteTo(dst, size); |
197 | 197 |
198 // Update the remaining number of bytes needed to read | 198 // Update the remaining number of bytes needed to read |
199 // and the destination buffer. | 199 // and the destination buffer. |
200 size -= buffered; | 200 size -= buffered; |
201 SkASSERT(size + (fOffset - start) == totalSize); | 201 SkASSERT(size + (fOffset - start) == totalSize); |
202 if (dst != NULL) { | 202 if (dst != nullptr) { |
203 dst += buffered; | 203 dst += buffered; |
204 } | 204 } |
205 } | 205 } |
206 | 206 |
207 if (size > 0 && !fStream->isAtEnd()) { | 207 if (size > 0 && !fStream->isAtEnd()) { |
208 SkDEBUGCODE(const size_t bytesReadDirectly =) this->readDirectlyFromStre
am(dst, size); | 208 SkDEBUGCODE(const size_t bytesReadDirectly =) this->readDirectlyFromStre
am(dst, size); |
209 SkDEBUGCODE(size -= bytesReadDirectly;) | 209 SkDEBUGCODE(size -= bytesReadDirectly;) |
210 SkASSERT(size + (fOffset - start) == totalSize); | 210 SkASSERT(size + (fOffset - start) == totalSize); |
211 } | 211 } |
212 | 212 |
213 return fOffset - start; | 213 return fOffset - start; |
214 } | 214 } |
OLD | NEW |