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

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

Issue 25581002: Hide implementation details: SkFrontBufferedStream (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Add a note to handle failed alloc better. Created 7 years, 1 month 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 | « no previous file | src/utils/SkFrontBufferedStream.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2013 Google Inc. 2 * Copyright 2013 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SkStream.h" 8 #include "SkTypes.h"
9 #include "SkTemplates.h" 9
10 class SkStream;
11 class SkStreamRewindable;
10 12
11 /** 13 /**
12 * Specialized stream that only buffers the first X bytes of a stream, 14 * Specialized stream that buffers the first X bytes of a stream,
13 * where X is passed in by the user. Note that unlike some buffered 15 * 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 16 * stream APIs, once more bytes than can fit in the buffer are read,
15 * is done. This stream is designed for a use case where the caller 17 * no more buffering is done. This stream is designed for a use case
16 * knows that rewind will only be called from within X bytes (inclusive), 18 * where the caller knows that rewind will only be called from within
17 * and the wrapped stream is not necessarily able to rewind at all. 19 * X bytes (inclusive), and the wrapped stream is not necessarily
20 * able to rewind at all.
18 */ 21 */
19 class SkFrontBufferedStream : public SkStreamRewindable { 22 class SkFrontBufferedStream {
20 public: 23 public:
21 /** 24 /**
22 * Creates a new stream that wraps and buffers SkStream. 25 * Creates a new stream that wraps and buffers an SkStream.
23 * @param stream SkStream to buffer. If NULL, NULL is returned. After 26 * @param stream SkStream to buffer. If stream is NULL, NULL is
24 * this call, unref stream and do not refer to it. 27 * returned. When this call succeeds (i.e. returns non NULL),
25 * SkFrontBufferedStream is expected to be its only owner. 28 * SkFrontBufferedStream is expected to be the only owner of
26 * @param bufferSize Exact size of the buffer to be used. 29 * stream, so it should be unreffed and no longer used directly.
27 * @return An SkStream that can buffer up to bufferSize. 30 * @param minBufferSize Minimum size of buffer required.
31 * @return An SkStream that can buffer at least minBufferSize, or
32 * NULL on failure.
28 */ 33 */
29 static SkStreamRewindable* Create(SkStream* stream, size_t bufferSize); 34 static SkStreamRewindable* Create(SkStream* stream, size_t minBufferSize);
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 // Private. Use Create.
59 SkFrontBufferedStream(SkStream*, size_t bufferSize);
60
61 // Read up to size bytes from already buffered data, and copy to
62 // dst, if non-NULL. Updates fOffset. Assumes that fOffset is less
63 // than fBufferedSoFar.
64 size_t readFromBuffer(char* dst, size_t size);
65
66 // Buffer up to size bytes from the stream, and copy to dst if non-
67 // NULL. Updates fOffset and fBufferedSoFar. Assumes that fOffset is
68 // less than fBufferedSoFar, and size is greater than 0.
69 size_t bufferAndWriteTo(char* dst, size_t size);
70
71 // Read up to size bytes directly from the stream and into dst if non-
72 // NULL. Updates fOffset. Assumes fOffset is at or beyond the buffered
73 // data, and size is greater than 0.
74 size_t readDirectlyFromStream(char* dst, size_t size);
75
76 typedef SkStream INHERITED;
77 }; 35 };
OLDNEW
« no previous file with comments | « no previous file | src/utils/SkFrontBufferedStream.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698