[8ebc79b] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
---|
| 2 | // |
---|
| 3 | // Redistribution and use in source and binary forms, with or without |
---|
| 4 | // modification, are permitted provided that the following conditions are |
---|
| 5 | // met: |
---|
| 6 | // |
---|
| 7 | // * Redistributions of source code must retain the above copyright |
---|
| 8 | // notice, this list of conditions and the following disclaimer. |
---|
| 9 | // * Redistributions in binary form must reproduce the above |
---|
| 10 | // copyright notice, this list of conditions and the following disclaimer |
---|
| 11 | // in the documentation and/or other materials provided with the |
---|
| 12 | // distribution. |
---|
| 13 | // * Neither the name of Google Inc. nor the names of its |
---|
| 14 | // contributors may be used to endorse or promote products derived from |
---|
| 15 | // this software without specific prior written permission. |
---|
| 16 | // |
---|
| 17 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
| 18 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
| 19 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
| 20 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
---|
| 21 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
---|
| 22 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
---|
| 23 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
---|
| 24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
---|
| 25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
| 26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
---|
| 27 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
| 28 | |
---|
| 29 | #ifndef UTIL_SNAPPY_SNAPPY_SINKSOURCE_H_ |
---|
| 30 | #define UTIL_SNAPPY_SNAPPY_SINKSOURCE_H_ |
---|
| 31 | |
---|
| 32 | #include <stddef.h> |
---|
| 33 | |
---|
| 34 | |
---|
| 35 | namespace snappy { |
---|
| 36 | |
---|
| 37 | // A Sink is an interface that consumes a sequence of bytes. |
---|
| 38 | class Sink { |
---|
| 39 | public: |
---|
| 40 | Sink() { } |
---|
| 41 | virtual ~Sink(); |
---|
| 42 | |
---|
| 43 | // Append "bytes[0,n-1]" to this. |
---|
| 44 | virtual void Append(const char* bytes, size_t n) = 0; |
---|
| 45 | |
---|
| 46 | // Returns a writable buffer of the specified length for appending. |
---|
| 47 | // May return a pointer to the caller-owned scratch buffer which |
---|
| 48 | // must have at least the indicated length. The returned buffer is |
---|
| 49 | // only valid until the next operation on this Sink. |
---|
| 50 | // |
---|
| 51 | // After writing at most "length" bytes, call Append() with the |
---|
| 52 | // pointer returned from this function and the number of bytes |
---|
| 53 | // written. Many Append() implementations will avoid copying |
---|
| 54 | // bytes if this function returned an internal buffer. |
---|
| 55 | // |
---|
| 56 | // If a non-scratch buffer is returned, the caller may only pass a |
---|
| 57 | // prefix of it to Append(). That is, it is not correct to pass an |
---|
| 58 | // interior pointer of the returned array to Append(). |
---|
| 59 | // |
---|
| 60 | // The default implementation always returns the scratch buffer. |
---|
| 61 | virtual char* GetAppendBuffer(size_t length, char* scratch); |
---|
| 62 | |
---|
| 63 | |
---|
| 64 | private: |
---|
| 65 | // No copying |
---|
| 66 | Sink(const Sink&); |
---|
| 67 | void operator=(const Sink&); |
---|
| 68 | }; |
---|
| 69 | |
---|
| 70 | // A Source is an interface that yields a sequence of bytes |
---|
| 71 | class Source { |
---|
| 72 | public: |
---|
| 73 | Source() { } |
---|
| 74 | virtual ~Source(); |
---|
| 75 | |
---|
| 76 | // Return the number of bytes left to read from the source |
---|
| 77 | virtual size_t Available() const = 0; |
---|
| 78 | |
---|
| 79 | // Peek at the next flat region of the source. Does not reposition |
---|
| 80 | // the source. The returned region is empty iff Available()==0. |
---|
| 81 | // |
---|
| 82 | // Returns a pointer to the beginning of the region and store its |
---|
| 83 | // length in *len. |
---|
| 84 | // |
---|
| 85 | // The returned region is valid until the next call to Skip() or |
---|
| 86 | // until this object is destroyed, whichever occurs first. |
---|
| 87 | // |
---|
| 88 | // The returned region may be larger than Available() (for example |
---|
| 89 | // if this ByteSource is a view on a substring of a larger source). |
---|
| 90 | // The caller is responsible for ensuring that it only reads the |
---|
| 91 | // Available() bytes. |
---|
| 92 | virtual const char* Peek(size_t* len) = 0; |
---|
| 93 | |
---|
| 94 | // Skip the next n bytes. Invalidates any buffer returned by |
---|
| 95 | // a previous call to Peek(). |
---|
| 96 | // REQUIRES: Available() >= n |
---|
| 97 | virtual void Skip(size_t n) = 0; |
---|
| 98 | |
---|
| 99 | private: |
---|
| 100 | // No copying |
---|
| 101 | Source(const Source&); |
---|
| 102 | void operator=(const Source&); |
---|
| 103 | }; |
---|
| 104 | |
---|
| 105 | // A Source implementation that yields the contents of a flat array |
---|
| 106 | class ByteArraySource : public Source { |
---|
| 107 | public: |
---|
| 108 | ByteArraySource(const char* p, size_t n) : ptr_(p), left_(n) { } |
---|
| 109 | virtual ~ByteArraySource(); |
---|
| 110 | virtual size_t Available() const; |
---|
| 111 | virtual const char* Peek(size_t* len); |
---|
| 112 | virtual void Skip(size_t n); |
---|
| 113 | private: |
---|
| 114 | const char* ptr_; |
---|
| 115 | size_t left_; |
---|
| 116 | }; |
---|
| 117 | |
---|
| 118 | // A Sink implementation that writes to a flat array without any bound checks. |
---|
| 119 | class UncheckedByteArraySink : public Sink { |
---|
| 120 | public: |
---|
| 121 | explicit UncheckedByteArraySink(char* dest) : dest_(dest) { } |
---|
| 122 | virtual ~UncheckedByteArraySink(); |
---|
| 123 | virtual void Append(const char* data, size_t n); |
---|
| 124 | virtual char* GetAppendBuffer(size_t len, char* scratch); |
---|
| 125 | |
---|
| 126 | // Return the current output pointer so that a caller can see how |
---|
| 127 | // many bytes were produced. |
---|
| 128 | // Note: this is not a Sink method. |
---|
| 129 | char* CurrentDestination() const { return dest_; } |
---|
| 130 | private: |
---|
| 131 | char* dest_; |
---|
| 132 | }; |
---|
| 133 | |
---|
| 134 | |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | #endif // UTIL_SNAPPY_SNAPPY_SINKSOURCE_H_ |
---|