clean up UNO available() implementations

There seems to be some confusion here. available() is actually the
number of bytes that can be read without blocking, but most
implementations seems to be just returning the number of bytes remaining
in the stream. Since we're doing that, let's do it properly.

(*) some of them were just casting, instead of clamping, which will
return wrong values sometimes.
(*) FileStreamWrapper_Impl/OInputStreamWrapper/OTempFileService were
doing unnecessary work, instead of just asking the underlying SvStream
for it's remaining size

Change-Id: I3ef26e0363e989ed3e00be0fdb993e1cdeb7819f
Reviewed-on: https://gerrit.libreoffice.org/57264
Tested-by: Jenkins
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
This commit is contained in:
Noel Grandin
2018-07-11 09:57:47 +02:00
parent ce795510d3
commit 00850e3fa7
13 changed files with 23 additions and 32 deletions

View File

@@ -150,7 +150,7 @@ void SAL_CALL UNOMemoryStream::skipBytes( sal_Int32 nBytesToSkip )
sal_Int32 SAL_CALL UNOMemoryStream::available()
{
return static_cast< sal_Int32 >( maData.size() ) - mnCursor;
return std::min<sal_Int64>( SAL_MAX_INT32, maData.size() - mnCursor);
}
void SAL_CALL UNOMemoryStream::closeInput()

View File

@@ -120,8 +120,7 @@ sal_Int32 SAL_CALL OSLInputStreamWrapper::available()
eError = m_pFile->setPos(osl_Pos_Absolut, nPos);
if (eError != FileBase::E_None)
throw css::io::NotConnectedException(OUString(),static_cast<css::uno::XWeak*>(this));
return sal::static_int_cast< sal_Int32 >(
std::max(nAvailable, sal::static_int_cast< sal_uInt64 >(SAL_MAX_INT32)));
return std::min<sal_Int64>(nAvailable, SAL_MAX_INT32);
}

View File

@@ -76,7 +76,7 @@ void SAL_CALL ZipPackageBuffer::skipBytes( sal_Int32 nBytesToSkip )
}
sal_Int32 SAL_CALL ZipPackageBuffer::available( )
{
return static_cast < sal_Int32 > (m_nEnd - m_nCurrent);
return std::min<sal_Int64>(SAL_MAX_INT32, m_nEnd - m_nCurrent);
}
void SAL_CALL ZipPackageBuffer::closeInput( )
{

View File

@@ -203,17 +203,10 @@ sal_Int32 SAL_CALL FileStreamWrapper_Impl::available()
::osl::MutexGuard aGuard( m_aMutex );
checkConnected();
sal_uInt32 nPos = m_pSvStream->Tell();
sal_Int64 nAvailable = m_pSvStream->remainingSize();
checkError();
m_pSvStream->Seek(STREAM_SEEK_TO_END);
checkError();
sal_Int32 nAvailable = static_cast<sal_Int32>(m_pSvStream->Tell()) - nPos;
m_pSvStream->Seek(nPos);
checkError();
return nAvailable;
return std::min<sal_Int64>(SAL_MAX_INT32, nAvailable);
}

View File

@@ -127,7 +127,7 @@ namespace cmis
sal_Int32 SAL_CALL StdInputStream::available( )
{
return sal::static_int_cast< sal_Int32 >( m_nLength - getPosition() );
return std::min<sal_Int64>( SAL_MAX_INT32, m_nLength - getPosition() );
}
void SAL_CALL StdInputStream::closeInput( )

View File

@@ -170,7 +170,8 @@ XStream_impl::skipBytes( sal_Int32 nBytesToSkip )
sal_Int32 SAL_CALL
XStream_impl::available()
{
return 0;
sal_Int64 avail = getLength() - getPosition();
return std::min<sal_Int64>(avail, SAL_MAX_INT32);
}

View File

@@ -105,7 +105,7 @@ void SAL_CALL NeonInputStream::skipBytes( sal_Int32 nBytesToSkip )
// Returns the number of unread bytes currently remaining on the stream
sal_Int32 SAL_CALL NeonInputStream::available( )
{
return sal::static_int_cast<sal_Int32>(mLen - mPos);
return std::min<sal_Int64>(SAL_MAX_INT32, mLen - mPos);
}
void SAL_CALL NeonInputStream::closeInput()

View File

@@ -120,7 +120,7 @@ void SAL_CALL SerfInputStream::skipBytes( sal_Int32 nBytesToSkip )
sal_Int32 SAL_CALL SerfInputStream::available( )
{
return sal::static_int_cast<sal_Int32>(mLen - mPos);
return std::min<sal_Int64>(SAL_MAX_INT32, mLen - mPos);
}

View File

@@ -99,7 +99,7 @@ namespace ucbhelper
sal_Int32 SAL_CALL FdInputStream::available()
{
return sal::static_int_cast<sal_Int32>(m_nLength - getPosition());
return std::min<sal_Int64>(SAL_MAX_INT32, m_nLength - getPosition());
}

View File

@@ -107,17 +107,10 @@ sal_Int32 SAL_CALL OInputStreamWrapper::available()
::osl::MutexGuard aGuard( m_aMutex );
checkConnected();
sal_uInt32 nPos = m_pSvStream->Tell();
sal_Int64 nAvailable = m_pSvStream->remainingSize();
checkError();
m_pSvStream->Seek(STREAM_SEEK_TO_END);
checkError();
sal_Int32 nAvailable = static_cast<sal_Int32>(m_pSvStream->Tell()) - nPos;
m_pSvStream->Seek(nPos);
checkError();
return nAvailable;
return std::min<sal_Int64>(SAL_MAX_INT32, nAvailable);
}
void SAL_CALL OInputStreamWrapper::closeInput()

View File

@@ -206,11 +206,10 @@ sal_Int32 SAL_CALL OTempFileService::available( )
checkConnected();
sal_uInt32 const nAvailable =
static_cast<sal_uInt32>(mpStream->remainingSize());
sal_Int64 nAvailable = mpStream->remainingSize();
checkError();
return nAvailable;
return std::min<sal_Int64>(SAL_MAX_INT32, nAvailable);
}
void SAL_CALL OTempFileService::closeInput( )
{

View File

@@ -116,7 +116,13 @@ XInputStream_impl::skipBytes(
sal_Int32 SAL_CALL
XInputStream_impl::available()
{
return 0;
sal_uInt64 uPos;
if( osl::FileBase::E_None != m_aFile.getPos( uPos ) )
throw io::IOException();
sal_uInt64 uSize;
if( osl::FileBase::E_None != m_aFile.getSize( uSize ) )
throw io::IOException();
return std::min<sal_uInt64>(SAL_MAX_INT32, uSize - uPos);
}

View File

@@ -924,7 +924,7 @@ void SAL_CALL InputStreamTransformer::skipBytes( sal_Int32 nBytesToSkip )
sal_Int32 SAL_CALL InputStreamTransformer::available()
{
osl::MutexGuard aGuard( m_aMutex );
return std::max<sal_Int32>(buffer.getLength() - pos, 0);
return std::min<sal_Int64>(SAL_MAX_INT32, buffer.getLength() - pos);
}