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" |
| 10 #include "SkTemplates.h" |
| 11 |
| 12 class FrontBufferedStream : public SkStreamRewindable { |
| 13 public: |
| 14 // Called by Create. |
| 15 FrontBufferedStream(SkStream*, size_t bufferSize); |
| 16 |
| 17 virtual size_t read(void* buffer, size_t size) SK_OVERRIDE; |
| 18 |
| 19 virtual bool isAtEnd() const SK_OVERRIDE; |
| 20 |
| 21 virtual bool rewind() SK_OVERRIDE; |
| 22 |
| 23 virtual bool hasPosition() const SK_OVERRIDE { return true; } |
| 24 |
| 25 virtual size_t getPosition() const SK_OVERRIDE { return fOffset; } |
| 26 |
| 27 virtual bool hasLength() const SK_OVERRIDE; |
| 28 |
| 29 virtual size_t getLength() const SK_OVERRIDE; |
| 30 |
| 31 virtual SkStreamRewindable* duplicate() const SK_OVERRIDE { return NULL; } |
| 32 |
| 33 private: |
| 34 SkAutoTUnref<SkStream> fStream; |
| 35 // Current offset into the stream. Always >= 0. |
| 36 size_t fOffset; |
| 37 // Amount that has been buffered by calls to read. Will always be less than |
| 38 // fBufferSize. |
| 39 size_t fBufferedSoFar; |
| 40 // Total size of the buffer. |
| 41 const size_t fBufferSize; |
| 42 // FIXME: SkAutoTMalloc throws on failure. Instead, Create should return a |
| 43 // NULL stream. |
| 44 SkAutoTMalloc<char> fBuffer; |
| 45 |
| 46 // Read up to size bytes from already buffered data, and copy to |
| 47 // dst, if non-NULL. Updates fOffset. Assumes that fOffset is less |
| 48 // than fBufferedSoFar. |
| 49 size_t readFromBuffer(char* dst, size_t size); |
| 50 |
| 51 // Buffer up to size bytes from the stream, and copy to dst if non- |
| 52 // NULL. Updates fOffset and fBufferedSoFar. Assumes that fOffset is |
| 53 // less than fBufferedSoFar, and size is greater than 0. |
| 54 size_t bufferAndWriteTo(char* dst, size_t size); |
| 55 |
| 56 // Read up to size bytes directly from the stream and into dst if non- |
| 57 // NULL. Updates fOffset. Assumes fOffset is at or beyond the buffered |
| 58 // data, and size is greater than 0. |
| 59 size_t readDirectlyFromStream(char* dst, size_t size); |
| 60 |
| 61 typedef SkStream INHERITED; |
| 62 }; |
9 | 63 |
10 SkStreamRewindable* SkFrontBufferedStream::Create(SkStream* stream, size_t buffe
rSize) { | 64 SkStreamRewindable* SkFrontBufferedStream::Create(SkStream* stream, size_t buffe
rSize) { |
11 if (NULL == stream) { | 65 if (NULL == stream) { |
12 return NULL; | 66 return NULL; |
13 } | 67 } |
14 return SkNEW_ARGS(SkFrontBufferedStream, (stream, bufferSize)); | 68 return SkNEW_ARGS(FrontBufferedStream, (stream, bufferSize)); |
15 } | 69 } |
16 | 70 |
17 SkFrontBufferedStream::SkFrontBufferedStream(SkStream* stream, size_t bufferSize
) | 71 FrontBufferedStream::FrontBufferedStream(SkStream* stream, size_t bufferSize) |
18 : fStream(SkRef(stream)) | 72 : fStream(SkRef(stream)) |
19 , fOffset(0) | 73 , fOffset(0) |
20 , fBufferedSoFar(0) | 74 , fBufferedSoFar(0) |
21 , fBufferSize(bufferSize) | 75 , fBufferSize(bufferSize) |
22 , fBuffer(bufferSize) {} | 76 , fBuffer(bufferSize) {} |
23 | 77 |
24 bool SkFrontBufferedStream::isAtEnd() const { | 78 bool FrontBufferedStream::isAtEnd() const { |
25 if (fOffset < fBufferedSoFar) { | 79 if (fOffset < fBufferedSoFar) { |
26 // Even if the underlying stream is at the end, this stream has been | 80 // Even if the underlying stream is at the end, this stream has been |
27 // rewound after buffering, so it is not at the end. | 81 // rewound after buffering, so it is not at the end. |
28 return false; | 82 return false; |
29 } | 83 } |
30 | 84 |
31 return fStream->isAtEnd(); | 85 return fStream->isAtEnd(); |
32 } | 86 } |
33 | 87 |
34 bool SkFrontBufferedStream::rewind() { | 88 bool FrontBufferedStream::rewind() { |
35 // Only allow a rewind if we have not exceeded the buffer. | 89 // Only allow a rewind if we have not exceeded the buffer. |
36 if (fOffset <= fBufferSize) { | 90 if (fOffset <= fBufferSize) { |
37 fOffset = 0; | 91 fOffset = 0; |
38 return true; | 92 return true; |
39 } | 93 } |
40 return false; | 94 return false; |
41 } | 95 } |
42 | 96 |
43 bool SkFrontBufferedStream::hasLength() const { | 97 bool FrontBufferedStream::hasLength() const { |
44 return fStream->hasLength(); | 98 return fStream->hasLength(); |
45 } | 99 } |
46 | 100 |
47 size_t SkFrontBufferedStream::getLength() const { | 101 size_t FrontBufferedStream::getLength() const { |
48 return fStream->getLength(); | 102 return fStream->getLength(); |
49 } | 103 } |
50 | 104 |
51 size_t SkFrontBufferedStream::readFromBuffer(char* dst, size_t size) { | 105 size_t FrontBufferedStream::readFromBuffer(char* dst, size_t size) { |
52 SkASSERT(fOffset < fBufferedSoFar); | 106 SkASSERT(fOffset < fBufferedSoFar); |
53 // Some data has already been copied to fBuffer. Read up to the | 107 // Some data has already been copied to fBuffer. Read up to the |
54 // lesser of the size requested and the remainder of the buffered | 108 // lesser of the size requested and the remainder of the buffered |
55 // data. | 109 // data. |
56 const size_t bytesToCopy = SkTMin(size, fBufferedSoFar - fOffset); | 110 const size_t bytesToCopy = SkTMin(size, fBufferedSoFar - fOffset); |
57 if (dst != NULL) { | 111 if (dst != NULL) { |
58 memcpy(dst, fBuffer + fOffset, bytesToCopy); | 112 memcpy(dst, fBuffer + fOffset, bytesToCopy); |
59 } | 113 } |
60 | 114 |
61 // Update fOffset to the new position. It is guaranteed to be | 115 // Update fOffset to the new position. It is guaranteed to be |
62 // within the buffered data. | 116 // within the buffered data. |
63 fOffset += bytesToCopy; | 117 fOffset += bytesToCopy; |
64 SkASSERT(fOffset <= fBufferedSoFar); | 118 SkASSERT(fOffset <= fBufferedSoFar); |
65 | 119 |
66 return bytesToCopy; | 120 return bytesToCopy; |
67 } | 121 } |
68 | 122 |
69 size_t SkFrontBufferedStream::bufferAndWriteTo(char* dst, size_t size) { | 123 size_t FrontBufferedStream::bufferAndWriteTo(char* dst, size_t size) { |
70 SkASSERT(size > 0); | 124 SkASSERT(size > 0); |
71 SkASSERT(fOffset >= fBufferedSoFar); | 125 SkASSERT(fOffset >= fBufferedSoFar); |
72 // Data needs to be buffered. Buffer up to the lesser of the size requested | 126 // Data needs to be buffered. Buffer up to the lesser of the size requested |
73 // and the remainder of the max buffer size. | 127 // and the remainder of the max buffer size. |
74 const size_t bytesToBuffer = SkTMin(size, fBufferSize - fBufferedSoFar); | 128 const size_t bytesToBuffer = SkTMin(size, fBufferSize - fBufferedSoFar); |
75 char* buffer = fBuffer + fOffset; | 129 char* buffer = fBuffer + fOffset; |
76 const size_t buffered = fStream->read(buffer, bytesToBuffer); | 130 const size_t buffered = fStream->read(buffer, bytesToBuffer); |
77 | 131 |
78 fBufferedSoFar += buffered; | 132 fBufferedSoFar += buffered; |
79 fOffset = fBufferedSoFar; | 133 fOffset = fBufferedSoFar; |
80 SkASSERT(fBufferedSoFar <= fBufferSize); | 134 SkASSERT(fBufferedSoFar <= fBufferSize); |
81 | 135 |
82 // Copy the buffer to the destination buffer and update the amount read. | 136 // Copy the buffer to the destination buffer and update the amount read. |
83 if (dst != NULL) { | 137 if (dst != NULL) { |
84 memcpy(dst, buffer, buffered); | 138 memcpy(dst, buffer, buffered); |
85 } | 139 } |
86 | 140 |
87 return buffered; | 141 return buffered; |
88 } | 142 } |
89 | 143 |
90 size_t SkFrontBufferedStream::readDirectlyFromStream(char* dst, size_t size) { | 144 size_t FrontBufferedStream::readDirectlyFromStream(char* dst, size_t size) { |
91 SkASSERT(size > 0); | 145 SkASSERT(size > 0); |
92 // If we get here, we have buffered all that can be buffered. | 146 // If we get here, we have buffered all that can be buffered. |
93 SkASSERT(fBufferSize == fBufferedSoFar && fOffset >= fBufferSize); | 147 SkASSERT(fBufferSize == fBufferedSoFar && fOffset >= fBufferSize); |
94 | 148 |
95 const size_t bytesReadDirectly = fStream->read(dst, size); | 149 const size_t bytesReadDirectly = fStream->read(dst, size); |
96 fOffset += bytesReadDirectly; | 150 fOffset += bytesReadDirectly; |
97 | 151 |
98 // If we have read past the end of the buffer, rewinding is no longer | 152 // If we have read past the end of the buffer, rewinding is no longer |
99 // supported, so we can go ahead and free the memory. | 153 // supported, so we can go ahead and free the memory. |
100 if (bytesReadDirectly > 0) { | 154 if (bytesReadDirectly > 0) { |
101 fBuffer.reset(0); | 155 fBuffer.reset(0); |
102 } | 156 } |
103 | 157 |
104 return bytesReadDirectly; | 158 return bytesReadDirectly; |
105 } | 159 } |
106 | 160 |
107 size_t SkFrontBufferedStream::read(void* voidDst, size_t size) { | 161 size_t FrontBufferedStream::read(void* voidDst, size_t size) { |
108 // Cast voidDst to a char* for easy addition. | 162 // Cast voidDst to a char* for easy addition. |
109 char* dst = reinterpret_cast<char*>(voidDst); | 163 char* dst = reinterpret_cast<char*>(voidDst); |
110 SkDEBUGCODE(const size_t totalSize = size;) | 164 SkDEBUGCODE(const size_t totalSize = size;) |
111 const size_t start = fOffset; | 165 const size_t start = fOffset; |
112 | 166 |
113 // First, read any data that was previously buffered. | 167 // First, read any data that was previously buffered. |
114 if (fOffset < fBufferedSoFar) { | 168 if (fOffset < fBufferedSoFar) { |
115 const size_t bytesCopied = this->readFromBuffer(dst, size); | 169 const size_t bytesCopied = this->readFromBuffer(dst, size); |
116 | 170 |
117 // Update the remaining number of bytes needed to read | 171 // Update the remaining number of bytes needed to read |
(...skipping 20 matching lines...) Expand all Loading... |
138 } | 192 } |
139 | 193 |
140 if (size > 0 && !fStream->isAtEnd()) { | 194 if (size > 0 && !fStream->isAtEnd()) { |
141 SkDEBUGCODE(const size_t bytesReadDirectly =) this->readDirectlyFromStre
am(dst, size); | 195 SkDEBUGCODE(const size_t bytesReadDirectly =) this->readDirectlyFromStre
am(dst, size); |
142 SkDEBUGCODE(size -= bytesReadDirectly;) | 196 SkDEBUGCODE(size -= bytesReadDirectly;) |
143 SkASSERT(size + (fOffset - start) == totalSize); | 197 SkASSERT(size + (fOffset - start) == totalSize); |
144 } | 198 } |
145 | 199 |
146 return fOffset - start; | 200 return fOffset - start; |
147 } | 201 } |
OLD | NEW |