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

20
#include "config.h"
21
#include "DecoderBuffer.hxx"
22
#include "DecoderAPI.hxx"
23 24 25 26

#include <assert.h>

bool
27
DecoderBuffer::Fill()
28
{
29
	auto w = buffer.Write();
30
	if (w.IsEmpty())
31 32 33
		/* buffer is full */
		return false;

34
	size_t nbytes = decoder_read(decoder, is,
35
				     w.data, w.size);
36 37 38 39 40
	if (nbytes == 0)
		/* end of file, I/O error or decoder command
		   received */
		return false;

41
	buffer.Append(nbytes);
42 43 44
	return true;
}

45
ConstBuffer<void>
46
DecoderBuffer::Need(size_t min_size)
47 48
{
	while (true) {
49
		const auto r = Read();
50 51
		if (r.size >= min_size)
			return r;
52

53
		if (!Fill())
54 55 56 57
			return nullptr;
	}
}

58
bool
59
DecoderBuffer::Skip(size_t nbytes)
60
{
61
	const auto r = buffer.Read();
62
	if (r.size >= nbytes) {
63
		buffer.Consume(nbytes);
64 65
		return true;
	}
66

67
	buffer.Clear();
68
	nbytes -= r.size;
69

70
	return decoder_skip(decoder, is, nbytes);
71
}