summaryrefslogtreecommitdiff
path: root/tools/source/stream/stream.cxx
diff options
context:
space:
mode:
Diffstat (limited to 'tools/source/stream/stream.cxx')
-rw-r--r--tools/source/stream/stream.cxx28
1 files changed, 28 insertions, 0 deletions
diff --git a/tools/source/stream/stream.cxx b/tools/source/stream/stream.cxx
index a1f55392749a..312cd5f35b22 100644
--- a/tools/source/stream/stream.cxx
+++ b/tools/source/stream/stream.cxx
@@ -43,6 +43,7 @@
#include <tools/solar.h>
+#include <comphelper/string.hxx>
#define SWAPNIBBLES(c) \
unsigned char nSwapTmp=c; \
@@ -2473,4 +2474,31 @@ void SvDataCopyStream::Assign( const SvDataCopyStream& )
{
}
+//Create a OString of nSize bytes from rStream
+rtl::OString readBytesAsOString(SvStream& rStrm, sal_Size nSize)
+{
+ using comphelper::string::rtl_string_alloc;
+
+ rtl_String *pStr = NULL;
+ if (nSize)
+ {
+ nSize = std::min(nSize, static_cast<sal_Size>(SAL_MAX_INT32));
+ //alloc a (ref-count 1) rtl_String of the desired length.
+ //rtl_String's buffer is uninitialized, except for null termination
+ pStr = rtl_string_alloc(sal::static_int_cast<sal_Int32>(nSize));
+ sal_Size nWasRead = rStrm.Read(pStr->buffer, nSize);
+ if (nWasRead != nSize)
+ {
+ //on (typically unlikely) short read set length to what we could
+ //read, and null terminate. Excess buffer capacity remains of
+ //course, could create a (true) replacement OString if it matters.
+ pStr->length = sal::static_int_cast<sal_Int32>(nWasRead);
+ pStr->buffer[pStr->length] = 0;
+ }
+ }
+
+ //take ownership of buffer and return, otherwise return empty string
+ return pStr ? rtl::OString(pStr, SAL_NO_ACQUIRE) : rtl::OString();
+}
+
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */