OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright 2013 Google Inc. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license that can be | |
5 * found in the LICENSE file. | |
6 */ | |
7 | |
8 #include "SkBufferedStream.h" | |
9 | |
10 SkBufferedStream* SkBufferedStream::Create(SkStream* stream, size_t bufferSize) { | |
11 if (NULL == stream) { | |
12 return NULL; | |
13 } | |
14 return SkNEW_ARGS(SkBufferedStream, (stream, bufferSize)); | |
15 } | |
16 | |
17 SkBufferedStream::SkBufferedStream(SkStream* stream, size_t bufferSize) | |
18 : fStream(SkRef(stream)) | |
19 , fOffset(0) | |
20 , fBufferedSoFar(0) | |
21 , fBufferSize(bufferSize) | |
22 , fBuffer(bufferSize) {} | |
23 | |
24 SkBufferedStream::~SkBufferedStream() { | |
25 fStream->unref(); | |
26 } | |
27 | |
28 bool SkBufferedStream::isAtEnd() const { | |
29 if (fOffset < fBufferedSoFar) { | |
30 // Even if the underlying stream is at the end, this stream has been | |
mtklein
2013/09/17 19:18:32
Can you sketch out the invariants we're maintainin
scroggo
2013/09/17 20:12:52
Almost. It should be:
0 <= fBufferedSoFar <= fBuf
| |
31 // rewound after buffering, so it is not at the end. | |
32 return false; | |
33 } | |
34 | |
35 SkASSERT(fStream != NULL); | |
36 return fStream->isAtEnd(); | |
37 } | |
38 | |
39 bool SkBufferedStream::rewind() { | |
40 // Only allow a rewind if we have not exceeded the buffer. | |
41 if (fOffset <= fBufferSize) { | |
mtklein
2013/09/17 19:18:32
Isn't this off by one? When fOffset == fBufferSiz
scroggo
2013/09/17 20:12:52
No, fOffset is the number of bytes that were 'read
| |
42 fOffset = 0; | |
43 return true; | |
44 } | |
45 return false; | |
46 } | |
47 | |
48 bool SkBufferedStream::hasLength() const { | |
49 SkASSERT(fStream != NULL); | |
mtklein
2013/09/17 19:18:32
I guess the more SkASSERTs the better, but these s
scroggo
2013/09/17 20:12:52
They're impossible to fail, at least until mainten
| |
50 return fStream->hasLength(); | |
51 } | |
52 | |
53 size_t SkBufferedStream::getLength() const { | |
54 SkASSERT(fStream != NULL); | |
55 return fStream->getLength(); | |
56 } | |
57 | |
58 // Increment a void* by a number of bytes. | |
59 static void add_to_void_ptr(void** ptr, size_t bytes) { | |
mtklein
2013/09/17 19:18:32
Makes me think we should be using a char* everywhe
scroggo
2013/09/17 20:12:52
Done.
| |
60 SkASSERT(*ptr != NULL); | |
61 char* charPtr = reinterpret_cast<char*>(*ptr); | |
62 charPtr += bytes; | |
63 *ptr = reinterpret_cast<void*>(charPtr); | |
64 } | |
65 | |
66 size_t SkBufferedStream::read(void* dst, size_t size) { | |
mtklein
2013/09/17 19:18:32
This is a pretty long/complex method. I'm sort of
scroggo
2013/09/17 20:12:52
Next piece to chew off....
| |
67 SkASSERT(fStream != NULL); | |
68 SkDEBUGCODE(const size_t totalSize = size;) | |
69 size_t readSoFar = 0; | |
70 if (fOffset < fBufferedSoFar) { | |
71 // Some data has already been copied to fBuffer. Read up to the | |
72 // lesser of the size requested and the remainder of the buffered | |
73 // data. | |
74 const size_t bytesToCopy = SkTMin(size, fBufferedSoFar - fOffset); | |
75 memcpy(dst, this->getBufferAtOffset(), bytesToCopy); | |
mtklein
2013/09/17 19:18:32
This is just a suggestion. Writing the API here a
scroggo
2013/09/17 20:12:52
In this case, I only ever want the buffer at fOffs
| |
76 | |
77 // Update fOffset to the new position. It is guaranteed to be | |
78 // within the buffered data. | |
79 fOffset += bytesToCopy; | |
80 SkASSERT(fOffset <= fBufferedSoFar); | |
81 | |
82 if (bytesToCopy == size) { | |
83 // The entire requested read was inside the already buffered | |
84 // data. | |
85 SkASSERT(fOffset <= fBufferedSoFar); | |
86 return size; | |
87 } | |
88 | |
89 // The requested read extends beyond the buffered data. Update | |
90 // the remaining number of bytes needed to read, the number of | |
91 // bytes read so far, and the destination buffer. | |
92 size -= bytesToCopy; | |
93 readSoFar += bytesToCopy; | |
94 SkASSERT(size + readSoFar == totalSize); | |
95 add_to_void_ptr(&dst, bytesToCopy); | |
96 } | |
97 | |
98 // If we got here, we have read everything that was already buffered. | |
99 SkASSERT(fOffset >= fBufferedSoFar); | |
100 | |
101 if (fBufferedSoFar < fBufferSize) { | |
102 // Data needs to be buffered. Buffer up to the lesser of the size reques ted | |
103 // and the remainder of the max buffer size. | |
104 const size_t bytesToBuffer = SkTMin(size, fBufferSize - fBufferedSoFar); | |
105 void* buffer = this->getBufferAtOffset(); | |
106 const size_t buffered = fStream->read(buffer, bytesToBuffer); | |
107 fBufferedSoFar += buffered; | |
108 fOffset = fBufferedSoFar; | |
109 | |
110 // Copy the buffer to the destination buffer and update the amount read. | |
111 memcpy(dst, buffer, buffered); | |
112 readSoFar += buffered; | |
113 | |
114 if (buffered == size || fStream->isAtEnd()) { | |
115 // We were able to buffer all of the data requested (or all the data | |
116 // remaining in the stream) and provide it to the caller. | |
117 SkASSERT(fBufferedSoFar <= fBufferSize); | |
118 SkASSERT(totalSize == readSoFar || fStream->isAtEnd()); | |
119 return readSoFar; | |
120 } | |
121 | |
122 // The requested read extends beyond the length of the buffer. Update | |
123 // the remaining number of bytes needed to read and the destination | |
124 // buffer. | |
125 size -= buffered; | |
126 SkASSERT(size + readSoFar == totalSize); | |
127 add_to_void_ptr(&dst, buffered); | |
128 } | |
129 | |
130 // If we get here, we have buffered all that can be buffered. | |
131 SkASSERT(fBufferSize == fBufferedSoFar && fOffset >= fBufferSize); | |
132 | |
133 // Read directly from the stream. | |
134 const size_t bytesReadDirectly = fStream->read(dst, size); | |
135 fOffset += bytesReadDirectly; | |
136 return bytesReadDirectly + readSoFar; | |
137 } | |
OLD | NEW |