RewindInputPlugin.cxx 3.51 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * 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.
 */

#include "config.h"
21
#include "RewindInputPlugin.hxx"
22
#include "../ProxyInputStream.hxx"
23 24

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

27
class RewindInputStream final : public ProxyInputStream {
28 29
	/**
	 * The read position within the buffer.  Undefined as long as
30
	 * ReadingFromBuffer() returns false.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
	 */
	size_t head;

	/**
	 * The write/append position within the buffer.
	 */
	size_t tail;

	/**
	 * 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];

49
public:
50
	RewindInputStream(InputStream *_input)
51 52
		:ProxyInputStream(_input),
		 tail(0) {
53
	}
54

55 56 57
	/* virtual methods from InputStream */

	void Update() override {
58
		if (!ReadingFromBuffer())
59
			ProxyInputStream::Update();
60 61
	}

62
	bool IsEOF() override {
63
		return !ReadingFromBuffer() && ProxyInputStream::IsEOF();
64 65
	}

66 67
	size_t Read(void *ptr, size_t size) override;
	void Seek(offset_type offset) override;
68 69

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

79
size_t
80
RewindInputStream::Read(void *ptr, size_t read_size)
81
{
82
	if (ReadingFromBuffer()) {
83 84
		/* buffered read */

85
		assert(head == (size_t)offset);
86
		assert(tail == (size_t)input.GetOffset());
87

88 89
		if (read_size > tail - head)
			read_size = tail - head;
90

91 92 93
		memcpy(ptr, buffer + head, read_size);
		head += read_size;
		offset += read_size;
94

95
		return read_size;
96 97 98
	} else {
		/* pass method call to underlying stream */

99
		size_t nbytes = input.Read(ptr, read_size);
100

101
		if (input.GetOffset() > (offset_type)sizeof(buffer))
102
			/* disable buffering */
103
			tail = 0;
104
		else if (tail == (size_t)offset) {
105 106
			/* append to buffer */

107 108
			memcpy(buffer + tail, ptr, nbytes);
			tail += nbytes;
109

110
			assert(tail == (size_t)input.GetOffset());
111 112
		}

113
		CopyAttributes();
114 115 116 117 118

		return nbytes;
	}
}

119 120
void
RewindInputStream::Seek(offset_type new_offset)
121
{
122
	assert(IsReady());
123

124
	if (tail > 0 && new_offset <= (offset_type)tail) {
125 126
		/* buffered seek */

127
		assert(!ReadingFromBuffer() ||
128
		       head == (size_t)offset);
129
		assert(tail == (size_t)input.GetOffset());
130

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

138
		ProxyInputStream::Seek(new_offset);
139 140 141
	}
}

142 143
InputStream *
input_rewind_open(InputStream *is)
144
{
145
	assert(is != nullptr);
146
	assert(!is->IsReady() || is->GetOffset() == 0);
147

148
	if (is->IsReady() && is->IsSeekable())
149
		/* seekable resources don't need this plugin */
150
		return is;
151

152
	return new RewindInputStream(is);
153
}