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

Unified Diff: src/utils/SkBufferedStream.cpp

Issue 23717055: Add a buffered SkStream class. (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Comments, check for NULL dst 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
Index: src/utils/SkBufferedStream.cpp
diff --git a/src/utils/SkBufferedStream.cpp b/src/utils/SkBufferedStream.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..50699983fd8927f04e3fc1ef84df246dfc21c4f3
--- /dev/null
+++ b/src/utils/SkBufferedStream.cpp
@@ -0,0 +1,135 @@
+/*
+ * 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));
bungeman-skia 2013/09/17 22:19:11 One nice thing about this Create maybe being a vir
scroggo 2013/09/18 15:15:44 As Ben and I discussed in person, in my sole use c
+}
+
+SkBufferedStream::SkBufferedStream(SkStream* stream, size_t bufferSize)
+ : fStream(SkRef(stream))
+ , fOffset(0)
+ , fBufferedSoFar(0)
+ , fBufferSize(bufferSize)
+ , fBuffer(bufferSize) {}
+
+bool SkBufferedStream::isAtEnd() const {
+ if (fOffset < fBufferedSoFar) {
+ // Even if the underlying stream is at the end, this stream has been
+ // rewound after buffering, so it is not at the end.
+ return false;
+ }
+
+ return fStream->isAtEnd();
+}
+
+bool SkBufferedStream::rewind() {
+ // Only allow a rewind if we have not exceeded the buffer.
+ if (fOffset <= fBufferSize) {
+ fOffset = 0;
+ return true;
+ }
+ return false;
bungeman-skia 2013/09/17 22:19:11 My reaction was "returning NULL in here!? ...oh, t
+}
+
+bool SkBufferedStream::hasLength() const {
+ return fStream->hasLength();
+}
+
+size_t SkBufferedStream::getLength() const {
+ return fStream->getLength();
+}
+
+size_t SkBufferedStream::read(void* voidDst, size_t size) {
+ // Cast voidDst to a char* for easy addition.
+ char* dst = reinterpret_cast<char*>(voidDst);
+ SkDEBUGCODE(const size_t totalSize = size;)
+ size_t readSoFar = 0;
+
+ // First, read any data that was previously buffered.
+ 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);
+ if (dst != NULL) {
+ memcpy(dst, this->getBufferAtOffset(), bytesToCopy);
+ }
+
+ // 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);
+ if (dst != NULL) {
+ dst += bytesToCopy;
+ }
+ }
+
+ // If we got here, we have read everything that was already buffered.
+ SkASSERT(fOffset >= fBufferedSoFar);
+
+ // Buffer any more data that should be buffered, and copy it to the
+ // destination.
+ 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);
+ char* 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.
+ if (dst != NULL) {
+ 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);
+ if (dst != NULL) {
+ dst += buffered;
+ }
+ }
+
+ // If we get here, we have buffered all that can be buffered.
+ SkASSERT(fBufferSize == fBufferedSoFar && fOffset >= fBufferSize);
bungeman-skia 2013/09/17 22:19:11 It seems like once you get here you can release th
scroggo 2013/09/18 15:15:44 Almost. The stream could be at the end, in which c
+
+ // Read directly from the stream.
+ const size_t bytesReadDirectly = fStream->read(dst, size);
+ fOffset += bytesReadDirectly;
+ return bytesReadDirectly + readSoFar;
+}

Powered by Google App Engine
This is Rietveld 408576698