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

Unified Diff: src/utils/SkBufferedStream.cpp

Issue 23717055: Add a buffered SkStream class. (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: 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 side-by-side diff with in-line comments
Download patch
« include/utils/SkBufferedStream.h ('K') | « include/utils/SkBufferedStream.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/utils/SkBufferedStream.cpp
diff --git a/src/utils/SkBufferedStream.cpp b/src/utils/SkBufferedStream.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..0778fbb15f781193bb5e504b1a158f07fcc0136c
--- /dev/null
+++ b/src/utils/SkBufferedStream.cpp
@@ -0,0 +1,137 @@
+/*
+ * Copyright 2013 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "SkBufferedStream.h"
+
+SkBufferedStream* SkBufferedStream::Create(SkStream* stream, size_t bufferSize) {
+ if (NULL == stream) {
+ return NULL;
+ }
+ return SkNEW_ARGS(SkBufferedStream, (stream, bufferSize));
+}
+
+SkBufferedStream::SkBufferedStream(SkStream* stream, size_t bufferSize)
+ : fStream(SkRef(stream))
+ , fOffset(0)
+ , fBufferedSoFar(0)
+ , fBufferSize(bufferSize)
+ , fBuffer(bufferSize) {}
+
+SkBufferedStream::~SkBufferedStream() {
+ fStream->unref();
+}
+
+bool SkBufferedStream::isAtEnd() const {
+ if (fOffset < fBufferedSoFar) {
+ // 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
+ // rewound after buffering, so it is not at the end.
+ return false;
+ }
+
+ SkASSERT(fStream != NULL);
+ return fStream->isAtEnd();
+}
+
+bool SkBufferedStream::rewind() {
+ // Only allow a rewind if we have not exceeded the buffer.
+ 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
+ fOffset = 0;
+ return true;
+ }
+ return false;
+}
+
+bool SkBufferedStream::hasLength() const {
+ 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
+ return fStream->hasLength();
+}
+
+size_t SkBufferedStream::getLength() const {
+ SkASSERT(fStream != NULL);
+ return fStream->getLength();
+}
+
+// Increment a void* by a number of bytes.
+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.
+ SkASSERT(*ptr != NULL);
+ char* charPtr = reinterpret_cast<char*>(*ptr);
+ charPtr += bytes;
+ *ptr = reinterpret_cast<void*>(charPtr);
+}
+
+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....
+ SkASSERT(fStream != NULL);
+ SkDEBUGCODE(const size_t totalSize = size;)
+ size_t readSoFar = 0;
+ if (fOffset < fBufferedSoFar) {
+ // Some data has already been copied to fBuffer. Read up to the
+ // lesser of the size requested and the remainder of the buffered
+ // data.
+ const size_t bytesToCopy = SkTMin(size, fBufferedSoFar - fOffset);
+ 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
+
+ // Update fOffset to the new position. It is guaranteed to be
+ // within the buffered data.
+ fOffset += bytesToCopy;
+ SkASSERT(fOffset <= fBufferedSoFar);
+
+ if (bytesToCopy == size) {
+ // The entire requested read was inside the already buffered
+ // data.
+ SkASSERT(fOffset <= fBufferedSoFar);
+ return size;
+ }
+
+ // The requested read extends beyond the buffered data. Update
+ // the remaining number of bytes needed to read, the number of
+ // bytes read so far, and the destination buffer.
+ size -= bytesToCopy;
+ readSoFar += bytesToCopy;
+ SkASSERT(size + readSoFar == totalSize);
+ add_to_void_ptr(&dst, bytesToCopy);
+ }
+
+ // If we got here, we have read everything that was already buffered.
+ SkASSERT(fOffset >= fBufferedSoFar);
+
+ if (fBufferedSoFar < fBufferSize) {
+ // Data needs to be buffered. Buffer up to the lesser of the size requested
+ // and the remainder of the max buffer size.
+ const size_t bytesToBuffer = SkTMin(size, fBufferSize - fBufferedSoFar);
+ void* buffer = this->getBufferAtOffset();
+ const size_t buffered = fStream->read(buffer, bytesToBuffer);
+ fBufferedSoFar += buffered;
+ fOffset = fBufferedSoFar;
+
+ // Copy the buffer to the destination buffer and update the amount read.
+ memcpy(dst, buffer, buffered);
+ readSoFar += buffered;
+
+ if (buffered == size || fStream->isAtEnd()) {
+ // We were able to buffer all of the data requested (or all the data
+ // remaining in the stream) and provide it to the caller.
+ SkASSERT(fBufferedSoFar <= fBufferSize);
+ SkASSERT(totalSize == readSoFar || fStream->isAtEnd());
+ return readSoFar;
+ }
+
+ // The requested read extends beyond the length of the buffer. Update
+ // the remaining number of bytes needed to read and the destination
+ // buffer.
+ size -= buffered;
+ SkASSERT(size + readSoFar == totalSize);
+ add_to_void_ptr(&dst, buffered);
+ }
+
+ // If we get here, we have buffered all that can be buffered.
+ SkASSERT(fBufferSize == fBufferedSoFar && fOffset >= fBufferSize);
+
+ // Read directly from the stream.
+ const size_t bytesReadDirectly = fStream->read(dst, size);
+ fOffset += bytesReadDirectly;
+ return bytesReadDirectly + readSoFar;
+}
« include/utils/SkBufferedStream.h ('K') | « include/utils/SkBufferedStream.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698