ScummVM API documentation
buffered_stream.h
1/* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22// BufferedStream represents a buffered file stream; uses memory buffer
23// during read and write operations to limit number reads and writes on disk
24// and thus improve i/o performance.
25//
26// BufferedSectionStream is a subclass stream that limits reading by an
27// arbitrary offset range.
28
29#ifndef AGS_SHARED_UTIL_BUFFEREDSTREAM_H
30#define AGS_SHARED_UTIL_BUFFEREDSTREAM_H
31
32#include "common/std/vector.h"
33#include "ags/shared/util/file_stream.h"
34#include "ags/shared/util/file.h" // TODO: extract filestream mode constants
35
36namespace AGS3 {
37namespace AGS {
38namespace Shared {
39
40class BufferedStream : public FileStream {
41public:
42 // Needs tuning depending on the platform.
43 static const size_t BufferSize = 1024u * 8;
44
45 // The constructor may raise std::runtime_error if
46 // - there is an issue opening the file (does not exist, locked, permissions, etc)
47 // - the open mode could not be determined
48 // - could not determine the length of the stream
49 // It is recommended to use File::OpenFile to safely construct this object.
50 BufferedStream(const String &file_name, FileOpenMode open_mode, FileWorkMode work_mode, DataEndianess stream_endianess = kLittleEndian);
52
53 // Is end of stream
54 bool EOS() const override;
55 // Total length of stream (if known)
56 soff_t GetLength() const override;
57 // Current position (if known)
58 soff_t GetPosition() const override;
59
60 void Close() override;
61 bool Flush() override;
62
63 size_t Read(void *buffer, size_t size) override;
64 int32_t ReadByte() override;
65 size_t Write(const void *buffer, size_t size) override;
66 int32_t WriteByte(uint8_t b) override;
67
68 soff_t Seek(soff_t offset, StreamSeek origin) override;
69
70protected:
71 soff_t _start = 0; // valid section starting offset
72 soff_t _end = -1; // valid section ending offset
73
74private:
75 // Reads a chunk of file into the buffer, starting from the given offset
76 void FillBufferFromPosition(soff_t position);
77 // Writes a buffer into the file, and reposition to the new offset
78 void FlushBuffer(soff_t position);
79
80 soff_t _position = 0; // absolute read/write offset
81 soff_t _bufferPosition = 0; // buffer's location relative to file
83};
84
85
86// Creates a BufferedStream limited by an arbitrary offset range
88public:
89 BufferedSectionStream(const String &file_name, soff_t start_pos, soff_t end_pos,
90 FileOpenMode open_mode, FileWorkMode work_mode, DataEndianess stream_endianess = kLittleEndian);
91};
92
93} // namespace Shared
94} // namespace AGS
95} // namespace AGS3
96
97#endif
Definition: buffered_stream.h:87
Definition: buffered_stream.h:40
Definition: file_stream.h:35
Definition: string.h:60