stream: add vlc_stream_ReadPartial()

This is a variant of vlc_stream_Read(), such that it only waits for
some bytes (i.e. more than zero) rather than the full requested bytes
count.
This commit is contained in:
Rémi Denis-Courmont 2016-07-21 21:51:38 +03:00
parent e77eb3bdb2
commit b75462c33e
3 changed files with 59 additions and 0 deletions

View File

@ -177,6 +177,23 @@ enum stream_query_e
*/
VLC_API ssize_t vlc_stream_Read(stream_t *, void *buf, size_t len) VLC_USED;
/**
* Reads partial data from a byte stream.
*
* This function waits until some data is available for reading from the
* stream, a fatal error is encountered or the end-of-stream is reached.
*
* Unlike vlc_stream_Read(), this function does not wait for the full requested
* bytes count. It can return a short count even before the end of the stream
* and in the absence of any error.
*
* \param buf start of buffer to read data into [OUT]
* \param len buffer size (maximum number of bytes to read)
* \return the number of bytes read or a negative value on error.
*/
VLC_API ssize_t vlc_stream_ReadPartial(stream_t *, void *buf, size_t len)
VLC_USED;
/**
* Peeks at data from a byte stream.
*

View File

@ -430,6 +430,47 @@ ssize_t vlc_stream_Read(stream_t *s, void *buf, size_t len)
return copy;
}
ssize_t vlc_stream_ReadPartial(stream_t *s, void *buf, size_t len)
{
stream_priv_t *priv = (stream_priv_t *)s;
for (;;)
{
size_t avail = 0;
if (priv->peek != NULL)
avail += priv->peek->i_buffer;
if (priv->block != NULL)
avail += priv->block->i_buffer;
if (avail > 0)
return vlc_stream_Read(s, buf, (avail < len) ? avail : len);
if (s->pf_read != NULL)
{
ssize_t ret = s->pf_read(s, buf, len);
if (ret < 0)
return ret;
priv->offset += ret;
if (likely(len > 0))
priv->eof = !ret;
return ret;
}
if (s->pf_block != NULL)
{
bool eof;
priv->block = s->pf_block(s, &eof);
if (priv->block == NULL && eof)
return 0;
}
else
return 0;
}
}
ssize_t vlc_stream_Peek(stream_t *s, const uint8_t **restrict bufp, size_t len)
{
stream_priv_t *priv = (stream_priv_t *)s;

View File

@ -411,6 +411,7 @@ vlc_stream_Peek
vlc_stream_Read
vlc_stream_ReadBlock
vlc_stream_ReadLine
vlc_stream_ReadPartial
vlc_stream_Seek
vlc_stream_Tell
vlc_stream_NewMRL