1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements. See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership. The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License. You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied. See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18// Date: Thu Nov 22 13:57:56 CST 2012
19
20#ifndef BUTIL_ZERO_COPY_STREAM_AS_STREAMBUF_H
21#define BUTIL_ZERO_COPY_STREAM_AS_STREAMBUF_H
22
23#include <streambuf>
24#include <google/protobuf/io/zero_copy_stream.h>
25
26namespace butil {
27
28// Wrap a ZeroCopyOutputStream into std::streambuf. Notice that before
29// destruction or shrink(), BackUp() of the stream are not called. In another
30// word, if the stream is wrapped from IOBuf, the IOBuf may be larger than
31// appended data.
32class ZeroCopyStreamAsStreamBuf : public std::streambuf {
33public:
34 ZeroCopyStreamAsStreamBuf(google::protobuf::io::ZeroCopyOutputStream* stream)
35 : _zero_copy_stream(stream) {}
36 virtual ~ZeroCopyStreamAsStreamBuf();
37
38 // BackUp() unused bytes. Automatically called in destructor.
39 void shrink();
40
41protected:
42 int overflow(int ch) override;
43 int sync() override;
44 std::streampos seekoff(std::streamoff off,
45 std::ios_base::seekdir way,
46 std::ios_base::openmode which) override;
47
48private:
49 google::protobuf::io::ZeroCopyOutputStream* _zero_copy_stream;
50};
51
52} // namespace butil
53
54#endif // BUTIL_ZERO_COPY_STREAM_AS_STREAMBUF_H
55