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

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

Issue 23717055: Add a buffered SkStream class. (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Fix a bug Created 7 years, 2 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
« no previous file with comments | « include/utils/SkFrontBufferedStream.h ('k') | tests/FrontBufferedStreamTest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "SkFrontBufferedStream.h"
9
10 SkStreamRewindable* SkFrontBufferedStream::Create(SkStream* stream, size_t buffe rSize) {
11 if (NULL == stream) {
12 return NULL;
13 }
14 return SkNEW_ARGS(SkFrontBufferedStream, (stream, bufferSize));
15 }
16
17 SkFrontBufferedStream::SkFrontBufferedStream(SkStream* stream, size_t bufferSize )
18 : fStream(SkRef(stream))
19 , fOffset(0)
20 , fBufferedSoFar(0)
21 , fBufferSize(bufferSize)
22 , fBuffer(bufferSize) {}
23
24 bool SkFrontBufferedStream::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 SkFrontBufferedStream::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 SkFrontBufferedStream::hasLength() const {
44 return fStream->hasLength();
45 }
46
47 size_t SkFrontBufferedStream::getLength() const {
48 return fStream->getLength();
49 }
50
51 size_t SkFrontBufferedStream::readFromBuffer(char* dst, size_t size) {
52 SkASSERT(fOffset < fBufferedSoFar);
53 // Some data has already been copied to fBuffer. Read up to the
54 // lesser of the size requested and the remainder of the buffered
55 // data.
56 const size_t bytesToCopy = SkTMin(size, fBufferedSoFar - fOffset);
57 if (dst != NULL) {
58 memcpy(dst, fBuffer + fOffset, bytesToCopy);
59 }
60
61 // Update fOffset to the new position. It is guaranteed to be
62 // within the buffered data.
63 fOffset += bytesToCopy;
64 SkASSERT(fOffset <= fBufferedSoFar);
65
66 return bytesToCopy;
67 }
68
69 size_t SkFrontBufferedStream::bufferAndWriteTo(char* dst, size_t size) {
70 SkASSERT(size > 0);
71 SkASSERT(fOffset >= fBufferedSoFar);
72 // Data needs to be buffered. Buffer up to the lesser of the size requested
73 // and the remainder of the max buffer size.
74 const size_t bytesToBuffer = SkTMin(size, fBufferSize - fBufferedSoFar);
75 char* buffer = fBuffer + fOffset;
76 const size_t buffered = fStream->read(buffer, bytesToBuffer);
77
78 fBufferedSoFar += buffered;
79 fOffset = fBufferedSoFar;
80 SkASSERT(fBufferedSoFar <= fBufferSize);
81
82 // Copy the buffer to the destination buffer and update the amount read.
83 if (dst != NULL) {
84 memcpy(dst, buffer, buffered);
85 }
86
87 return buffered;
88 }
89
90 size_t SkFrontBufferedStream::readDirectlyFromStream(char* dst, size_t size) {
91 SkASSERT(size > 0);
92 // If we get here, we have buffered all that can be buffered.
93 SkASSERT(fBufferSize == fBufferedSoFar && fOffset >= fBufferSize);
94
95 const size_t bytesReadDirectly = fStream->read(dst, size);
96 fOffset += bytesReadDirectly;
97
98 // 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.
100 if (bytesReadDirectly > 0) {
scroggo 2013/09/26 17:26:04 In patch set 9 I accidentally removed this line, w
101 fBuffer.reset(0);
102 }
103
104 return bytesReadDirectly;
105 }
106
107 size_t SkFrontBufferedStream::read(void* voidDst, size_t size) {
108 // Cast voidDst to a char* for easy addition.
109 char* dst = reinterpret_cast<char*>(voidDst);
110 SkDEBUGCODE(const size_t totalSize = size;)
111 const size_t start = fOffset;
112
113 // First, read any data that was previously buffered.
114 if (fOffset < fBufferedSoFar) {
115 const size_t bytesCopied = this->readFromBuffer(dst, size);
116
117 // Update the remaining number of bytes needed to read
118 // and the destination buffer.
119 size -= bytesCopied;
120 SkASSERT(size + (fOffset - start) == totalSize);
121 if (dst != NULL) {
122 dst += bytesCopied;
123 }
124 }
125
126 // Buffer any more data that should be buffered, and copy it to the
127 // destination.
128 if (size > 0 && fBufferedSoFar < fBufferSize) {
129 const size_t buffered = this->bufferAndWriteTo(dst, size);
130
131 // Update the remaining number of bytes needed to read
132 // and the destination buffer.
133 size -= buffered;
134 SkASSERT(size + (fOffset - start) == totalSize);
135 if (dst != NULL) {
136 dst += buffered;
137 }
138 }
139
140 if (size > 0 && !fStream->isAtEnd()) {
141 const size_t bytesReadDirectly = this->readDirectlyFromStream(dst, size) ;
142 SkDEBUGCODE(size -= bytesReadDirectly;)
143 SkASSERT(size + (fOffset - start) == totalSize);
144 }
145
146 return fOffset - start;
147 }
OLDNEW
« no previous file with comments | « include/utils/SkFrontBufferedStream.h ('k') | tests/FrontBufferedStreamTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698