RewindInputStream.cxx 3.56 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

20 21
#include "RewindInputStream.hxx"
#include "ProxyInputStream.hxx"
22 23

#include <assert.h>
Max Kellermann's avatar
Max Kellermann committed
24
#include <string.h>
25

26
class RewindInputStream final : public ProxyInputStream {
27 28
	/**
	 * The read position within the buffer.  Undefined as long as
29
	 * ReadingFromBuffer() returns false.
30 31 32 33 34 35
	 */
	size_t head;

	/**
	 * The write/append position within the buffer.
	 */
36
	size_t tail = 0;
37 38 39 40 41 42 43 44 45 46 47

	/**
	 * The size of this buffer is the maximum number of bytes
	 * which can be rewinded cheaply without passing the "seek"
	 * call to CURL.
	 *
	 * The origin of this buffer is always the beginning of the
	 * stream (offset 0).
	 */
	char buffer[64 * 1024];

48
public:
49 50
	explicit RewindInputStream(InputStreamPtr _input)
		:ProxyInputStream(std::move(_input)) {}
51

52 53
	/* virtual methods from InputStream */

54
	void Update() noexcept override {
55
		if (!ReadingFromBuffer())
56
			ProxyInputStream::Update();
57 58
	}

59
	bool IsEOF() noexcept override {
60
		return !ReadingFromBuffer() && ProxyInputStream::IsEOF();
61 62
	}

63 64
	size_t Read(void *ptr, size_t size) override;
	void Seek(offset_type offset) override;
65 66

private:
67 68 69 70
	/**
	 * Are we currently reading from the buffer, and does the
	 * buffer contain more data for the next read operation?
	 */
71
	bool ReadingFromBuffer() const noexcept {
72
		return tail > 0 && offset < input->GetOffset();
73
	}
74
};
75

76
size_t
77
RewindInputStream::Read(void *ptr, size_t read_size)
78
{
79
	if (ReadingFromBuffer()) {
80 81
		/* buffered read */

82
		assert(head == (size_t)offset);
83
		assert(tail == (size_t)input->GetOffset());
84

85 86
		if (read_size > tail - head)
			read_size = tail - head;
87

88 89 90
		memcpy(ptr, buffer + head, read_size);
		head += read_size;
		offset += read_size;
91

92
		return read_size;
93 94 95
	} else {
		/* pass method call to underlying stream */

96
		size_t nbytes = input->Read(ptr, read_size);
97

98
		if (input->GetOffset() > (offset_type)sizeof(buffer))
99
			/* disable buffering */
100
			tail = 0;
101
		else if (tail == (size_t)offset) {
102 103
			/* append to buffer */

104 105
			memcpy(buffer + tail, ptr, nbytes);
			tail += nbytes;
106

107
			assert(tail == (size_t)input->GetOffset());
108 109
		}

110
		CopyAttributes();
111 112 113 114 115

		return nbytes;
	}
}

116 117
void
RewindInputStream::Seek(offset_type new_offset)
118
{
119
	assert(IsReady());
120

121
	if (tail > 0 && new_offset <= (offset_type)tail) {
122 123
		/* buffered seek */

124
		assert(!ReadingFromBuffer() ||
125
		       head == (size_t)offset);
126
		assert(tail == (size_t)input->GetOffset());
127

128 129
		head = (size_t)new_offset;
		offset = new_offset;
130
	} else {
131
		/* disable the buffer, because input has left the
132
		   buffered range now */
133
		tail = 0;
134

135
		ProxyInputStream::Seek(new_offset);
136 137 138
	}
}

139 140
InputStreamPtr
input_rewind_open(InputStreamPtr is)
141
{
142
	assert(is != nullptr);
143
	assert(!is->IsReady() || is->GetOffset() == 0);
144

145
	if (is->IsReady() && is->IsSeekable())
146
		/* seekable resources don't need this plugin */
147
		return is;
148

149
	return std::make_unique<RewindInputStream>(std::move(is));
150
}