1 /*
2    Copyright 2013-2015 Skytechnology sp. z o.o.
3 
4    This file is part of LizardFS.
5 
6    LizardFS is free software: you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation, version 3.
9 
10    LizardFS is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with LizardFS. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #pragma once
20 
21 #include "common/platform.h"
22 
23 #include <stdlib.h>
24 #include <cstdint>
25 #include <cstring>
26 #include <vector>
27 #include <sys/types.h>
28 
29 class OutputBuffer {
30 public:
31 	enum WriteStatus {
32 		WRITE_DONE,
33 		WRITE_AGAIN,
34 		WRITE_ERROR
35 	};
36 
37 	OutputBuffer(size_t internalBufferCapacity);
38 	~OutputBuffer();
39 
40 	ssize_t copyIntoBuffer(int inputFileDescriptor, size_t len, off_t* offset);
41 	ssize_t copyIntoBuffer(const void *mem, size_t len);
42 
43 	bool checkCRC(size_t bytes, uint32_t crc) const;
44 
copyIntoBuffer(const std::vector<uint8_t> & mem)45 	ssize_t copyIntoBuffer(const std::vector<uint8_t>& mem) {
46 		return copyIntoBuffer(mem.data(), mem.size());
47 	}
48 
49 	WriteStatus writeOutToAFileDescriptor(int outputFileDescriptor);
50 
51 	size_t bytesInABuffer() const;
data()52 	const uint8_t* data() const {
53 		return buffer_.data();
54 	}
55 	void clear();
56 
57 private:
58 	const size_t internalBufferCapacity_;
59 	std::vector<uint8_t> buffer_;
60 	size_t bufferUnflushedDataFirstIndex_;
61 	size_t bufferUnflushedDataOneAfterLastIndex_;
62 };
63