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

Side by Side Diff: include/utils/SkFrontBufferedStream.h

Issue 23717055: Add a buffered SkStream class. (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Rename test 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 "SkStream.h"
9 #include "SkTemplates.h"
10
11 /**
12 * Specialized stream that only buffers the first X bytes of a stream,
13 * where X is passed in by the user. Note that unlike some buffered
14 * stream APIs, once more than those bytes are read, no more buffering
15 * is done. This stream is designed for a use case where the caller
16 * knows that rewind will only be called from within X bytes (inclusive),
17 * and the wrapped stream is not necessarily able to rewind at all.
18 */
19 class SkFrontBufferedStream : public SkStreamRewindable {
20 public:
21 /**
22 * Creates a new stream that wraps and buffers SkStream.
23 * @param stream SkStream to buffer. If NULL, NULL is returned. After
24 * this call, unref stream and do not refer to it.
25 * SkFrontBufferedStream is expected to be its only owner.
26 * @param bufferSize Exact size of the buffer to be used.
27 * @return An SkStream that can buffer up to bufferSize.
28 */
29 static SkFrontBufferedStream* Create(SkStream* stream, size_t bufferSize);
reed1 2013/09/18 18:25:54 Why does this return SkFontBufferedStream, instead
scroggo 2013/09/25 17:36:01 We do not. Changed to an SkStreamRewindable.
30
31 virtual size_t read(void* buffer, size_t size) SK_OVERRIDE;
32
33 virtual bool isAtEnd() const SK_OVERRIDE;
34
35 virtual bool rewind() SK_OVERRIDE;
36
37 virtual bool hasPosition() const SK_OVERRIDE { return true; }
38
39 virtual size_t getPosition() const SK_OVERRIDE { return fOffset; }
40
41 virtual bool hasLength() const SK_OVERRIDE;
42
43 virtual size_t getLength() const SK_OVERRIDE;
44
45 virtual SkStreamRewindable* duplicate() const SK_OVERRIDE { return NULL; }
46
47 private:
48 SkAutoTUnref<SkStream> fStream;
49 // Current offset into the stream. Always >= 0.
50 size_t fOffset;
51 // Amount that has been buffered by calls to read. Will always be less than
52 // fBufferSize.
53 size_t fBufferedSoFar;
54 // Total size of the buffer.
55 const size_t fBufferSize;
56 SkAutoTMalloc<char> fBuffer;
57
58 // Unimplemented.
59 SkFrontBufferedStream();
60
61 // Private. Use Create.
62 SkFrontBufferedStream(SkStream*, size_t bufferSize);
63
64 char* getBufferAtOffset() {
65 return fBuffer.get() + fOffset;
mtklein 2013/09/19 18:56:53 Now that you're using SkAutoTMalloc<char>, I think
scroggo 2013/09/25 17:36:01 Done.
66 }
67
68 typedef SkStream INHERITED;
69 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698