OLD | NEW |
---|---|
(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 SkStreamRewindable* Create(SkStream* stream, size_t bufferSize); | |
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(); | |
mtklein
2013/09/26 15:36:21
Seems unneeded given the existence of another priv
scroggo
2013/09/26 17:26:03
Done.
| |
60 | |
61 // Private. Use Create. | |
62 SkFrontBufferedStream(SkStream*, size_t bufferSize); | |
63 | |
64 // Read up to size bytes from already buffered data, and copy to | |
65 // dst, if non-NULL. Updates fOffset. Assumes that fOffset is less | |
66 // than fBufferedSoFar. | |
67 size_t readFromBuffer(char* dst, size_t size); | |
68 | |
69 // Buffer up to size bytes from the stream, and copy to dst if non- | |
70 // NULL. Updates fOffset and fBufferedSoFar. Assumes that fOffset is | |
71 // less than fBufferedSoFar, and size is greater than 0. | |
72 size_t bufferAndWriteTo(char* dst, size_t size); | |
73 | |
74 // Read up to size bytes directly from the stream and into dst if non- | |
75 // NULL. Updates fOffset. Assumes fOffset is at or beyond the buffered | |
76 // data, and size is greater than 0. | |
77 size_t readDirectlyFromStream(char* dst, size_t size); | |
78 | |
79 typedef SkStream INHERITED; | |
80 }; | |
OLD | NEW |