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 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 // within the buffered data. | 112 // within the buffered data. |
113 fOffset += bytesToCopy; | 113 fOffset += bytesToCopy; |
114 SkASSERT(fOffset <= fBufferedSoFar); | 114 SkASSERT(fOffset <= fBufferedSoFar); |
115 | 115 |
116 return bytesToCopy; | 116 return bytesToCopy; |
117 } | 117 } |
118 | 118 |
119 size_t FrontBufferedStream::bufferAndWriteTo(char* dst, size_t size) { | 119 size_t FrontBufferedStream::bufferAndWriteTo(char* dst, size_t size) { |
120 SkASSERT(size > 0); | 120 SkASSERT(size > 0); |
121 SkASSERT(fOffset >= fBufferedSoFar); | 121 SkASSERT(fOffset >= fBufferedSoFar); |
| 122 SkASSERT(fBuffer); |
122 // Data needs to be buffered. Buffer up to the lesser of the size requested | 123 // Data needs to be buffered. Buffer up to the lesser of the size requested |
123 // and the remainder of the max buffer size. | 124 // and the remainder of the max buffer size. |
124 const size_t bytesToBuffer = SkTMin(size, fBufferSize - fBufferedSoFar); | 125 const size_t bytesToBuffer = SkTMin(size, fBufferSize - fBufferedSoFar); |
125 char* buffer = fBuffer + fOffset; | 126 char* buffer = fBuffer + fOffset; |
126 const size_t buffered = fStream->read(buffer, bytesToBuffer); | 127 const size_t buffered = fStream->read(buffer, bytesToBuffer); |
127 | 128 |
128 fBufferedSoFar += buffered; | 129 fBufferedSoFar += buffered; |
129 fOffset = fBufferedSoFar; | 130 fOffset = fBufferedSoFar; |
130 SkASSERT(fBufferedSoFar <= fBufferSize); | 131 SkASSERT(fBufferedSoFar <= fBufferSize); |
131 | 132 |
132 // Copy the buffer to the destination buffer and update the amount read. | 133 // Copy the buffer to the destination buffer and update the amount read. |
133 if (dst != NULL) { | 134 if (dst != NULL) { |
134 memcpy(dst, buffer, buffered); | 135 memcpy(dst, buffer, buffered); |
135 } | 136 } |
136 | 137 |
137 return buffered; | 138 return buffered; |
138 } | 139 } |
139 | 140 |
140 size_t FrontBufferedStream::readDirectlyFromStream(char* dst, size_t size) { | 141 size_t FrontBufferedStream::readDirectlyFromStream(char* dst, size_t size) { |
141 SkASSERT(size > 0); | 142 SkASSERT(size > 0); |
142 // If we get here, we have buffered all that can be buffered. | 143 // If we get here, we have buffered all that can be buffered. |
143 SkASSERT(fBufferSize == fBufferedSoFar && fOffset >= fBufferSize); | 144 SkASSERT(fBufferSize == fBufferedSoFar && fOffset >= fBufferSize); |
144 | 145 |
145 const size_t bytesReadDirectly = fStream->read(dst, size); | 146 const size_t bytesReadDirectly = fStream->read(dst, size); |
146 fOffset += bytesReadDirectly; | 147 fOffset += bytesReadDirectly; |
147 | 148 |
148 // If we have read past the end of the buffer, rewinding is no longer | 149 // If we have read past the end of the buffer, rewinding is no longer |
149 // supported, so we can go ahead and free the memory. | 150 // supported, so we can go ahead and free the memory. |
150 if (bytesReadDirectly > 0) { | 151 if (bytesReadDirectly > 0) { |
151 fBuffer.reset(0); | 152 sk_free(fBuffer.detach()); |
152 } | 153 } |
153 | 154 |
154 return bytesReadDirectly; | 155 return bytesReadDirectly; |
155 } | 156 } |
156 | 157 |
157 size_t FrontBufferedStream::read(void* voidDst, size_t size) { | 158 size_t FrontBufferedStream::read(void* voidDst, size_t size) { |
158 // Cast voidDst to a char* for easy addition. | 159 // Cast voidDst to a char* for easy addition. |
159 char* dst = reinterpret_cast<char*>(voidDst); | 160 char* dst = reinterpret_cast<char*>(voidDst); |
160 SkDEBUGCODE(const size_t totalSize = size;) | 161 SkDEBUGCODE(const size_t totalSize = size;) |
161 const size_t start = fOffset; | 162 const size_t start = fOffset; |
(...skipping 26 matching lines...) Expand all Loading... |
188 } | 189 } |
189 | 190 |
190 if (size > 0 && !fStream->isAtEnd()) { | 191 if (size > 0 && !fStream->isAtEnd()) { |
191 SkDEBUGCODE(const size_t bytesReadDirectly =) this->readDirectlyFromStre
am(dst, size); | 192 SkDEBUGCODE(const size_t bytesReadDirectly =) this->readDirectlyFromStre
am(dst, size); |
192 SkDEBUGCODE(size -= bytesReadDirectly;) | 193 SkDEBUGCODE(size -= bytesReadDirectly;) |
193 SkASSERT(size + (fOffset - start) == totalSize); | 194 SkASSERT(size + (fOffset - start) == totalSize); |
194 } | 195 } |
195 | 196 |
196 return fOffset - start; | 197 return fOffset - start; |
197 } | 198 } |
OLD | NEW |