Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(57)

Side by Side Diff: src/utils/SkBufferedStream.cpp

Issue 23717055: Add a buffered SkStream class. (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Respond to comments Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 bool SkBufferedStream::isAtEnd() const {
25 if (fOffset < fBufferedSoFar) {
26 // Even if the underlying stream is at the end, this stream has been
27 // rewound after buffering, so it is not at the end.
28 return false;
29 }
30
31 return fStream->isAtEnd();
32 }
33
34 bool SkBufferedStream::rewind() {
35 // Only allow a rewind if we have not exceeded the buffer.
36 if (fOffset <= fBufferSize) {
37 fOffset = 0;
38 return true;
39 }
40 return false;
41 }
42
43 bool SkBufferedStream::hasLength() const {
44 return fStream->hasLength();
45 }
46
47 size_t SkBufferedStream::getLength() const {
48 return fStream->getLength();
49 }
50
51 size_t SkBufferedStream::read(void* voidDst, size_t size) {
52 char* dst = reinterpret_cast<char*>(voidDst);
53 SkDEBUGCODE(const size_t totalSize = size;)
54 size_t readSoFar = 0;
55 if (fOffset < fBufferedSoFar) {
56 // Some data has already been copied to fBuffer. Read up to the
57 // lesser of the size requested and the remainder of the buffered
58 // data.
59 const size_t bytesToCopy = SkTMin(size, fBufferedSoFar - fOffset);
60 memcpy(dst, this->getBufferAtOffset(), bytesToCopy);
61
62 // Update fOffset to the new position. It is guaranteed to be
63 // within the buffered data.
64 fOffset += bytesToCopy;
65 SkASSERT(fOffset <= fBufferedSoFar);
66
67 if (bytesToCopy == size) {
68 // The entire requested read was inside the already buffered
69 // data.
70 SkASSERT(fOffset <= fBufferedSoFar);
71 return size;
72 }
73
74 // The requested read extends beyond the buffered data. Update
75 // the remaining number of bytes needed to read, the number of
76 // bytes read so far, and the destination buffer.
77 size -= bytesToCopy;
78 readSoFar += bytesToCopy;
79 SkASSERT(size + readSoFar == totalSize);
80 dst += bytesToCopy;
81 }
82
83 // If we got here, we have read everything that was already buffered.
84 SkASSERT(fOffset >= fBufferedSoFar);
85
86 if (fBufferedSoFar < fBufferSize) {
87 // Data needs to be buffered. Buffer up to the lesser of the size reques ted
88 // and the remainder of the max buffer size.
89 const size_t bytesToBuffer = SkTMin(size, fBufferSize - fBufferedSoFar);
90 char* buffer = this->getBufferAtOffset();
91 const size_t buffered = fStream->read(buffer, bytesToBuffer);
92 fBufferedSoFar += buffered;
93 fOffset = fBufferedSoFar;
94
95 // Copy the buffer to the destination buffer and update the amount read.
96 memcpy(dst, buffer, buffered);
97 readSoFar += buffered;
98
99 if (buffered == size || fStream->isAtEnd()) {
100 // We were able to buffer all of the data requested (or all the data
101 // remaining in the stream) and provide it to the caller.
102 SkASSERT(fBufferedSoFar <= fBufferSize);
103 SkASSERT(totalSize == readSoFar || fStream->isAtEnd());
104 return readSoFar;
105 }
106
107 // The requested read extends beyond the length of the buffer. Update
108 // the remaining number of bytes needed to read and the destination
109 // buffer.
110 size -= buffered;
111 SkASSERT(size + readSoFar == totalSize);
112 dst += buffered;
113 }
114
115 // If we get here, we have buffered all that can be buffered.
116 SkASSERT(fBufferSize == fBufferedSoFar && fOffset >= fBufferSize);
117
118 // Read directly from the stream.
119 const size_t bytesReadDirectly = fStream->read(dst, size);
120 fOffset += bytesReadDirectly;
121 return bytesReadDirectly + readSoFar;
122 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698