1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: binaryinputstream.cxx,v $
10 * $Revision: 1.4.22.2 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include "oox/helper/binaryinputstream.hxx"
34 #include <rtl/strbuf.hxx>
35 #include <rtl/ustrbuf.hxx>
38 using ::rtl::OStringBuffer
;
39 using ::rtl::OStringToOUString
;
40 using ::rtl::OUString
;
41 using ::rtl::OUStringBuffer
;
42 using ::com::sun::star::uno::UNO_QUERY
;
43 using ::com::sun::star::uno::Reference
;
44 using ::com::sun::star::uno::Exception
;
45 using ::com::sun::star::io::XInputStream
;
46 using ::com::sun::star::io::XSeekable
;
50 const sal_Int32 INPUTSTREAM_BUFFERSIZE
= 0x8000;
52 // ============================================================================
54 OString
BinaryInputStream::readNulCharArray()
56 OStringBuffer aBuffer
;
57 for( sal_uInt8 nChar
= readuInt8(); !mbEof
&& (nChar
> 0); readValue( nChar
) )
58 aBuffer
.append( static_cast< sal_Char
>( nChar
) );
59 return aBuffer
.makeStringAndClear();
62 OUString
BinaryInputStream::readNulCharArrayUC( rtl_TextEncoding eTextEnc
)
64 return OStringToOUString( readNulCharArray(), eTextEnc
);
67 OUString
BinaryInputStream::readNulUnicodeArray()
69 OUStringBuffer aBuffer
;
70 for( sal_uInt16 nChar
= readuInt16(); !mbEof
&& (nChar
> 0); readValue( nChar
) )
71 aBuffer
.append( static_cast< sal_Unicode
>( nChar
) );
72 return aBuffer
.makeStringAndClear();
75 OString
BinaryInputStream::readCharArray( sal_Int32 nChars
, bool bAllowNulChars
)
80 ::std::vector
< sal_Char
> aBuffer( static_cast< size_t >( nChars
) );
81 size_t nCharsRead
= static_cast< size_t >( readMemory( &aBuffer
.front(), nChars
) );
83 ::std::replace( aBuffer
.begin(), aBuffer
.begin() + nCharsRead
, '\0', '?' );
84 return OString( &aBuffer
.front(), nCharsRead
);
87 OUString
BinaryInputStream::readCharArrayUC( sal_Int32 nChars
, rtl_TextEncoding eTextEnc
, bool bAllowNulChars
)
89 return OStringToOUString( readCharArray( nChars
, bAllowNulChars
), eTextEnc
);
92 OUString
BinaryInputStream::readUnicodeArray( sal_Int32 nChars
, bool bAllowNulChars
)
94 OUStringBuffer aBuffer
;
97 aBuffer
.ensureCapacity( nChars
);
99 for( sal_uInt16 nCharIdx
= 0; !mbEof
&& (nCharIdx
< nChars
); ++nCharIdx
)
102 aBuffer
.append( static_cast< sal_Unicode
>( (!bAllowNulChars
&& (nChar
== 0)) ? '?' : nChar
) );
105 return aBuffer
.makeStringAndClear();
108 void BinaryInputStream::readAtom( void* opMem
, sal_uInt8 nSize
)
110 readMemory( opMem
, nSize
);
113 // ============================================================================
115 BinaryXInputStream::BinaryXInputStream( const Reference
< XInputStream
>& rxInStrm
, bool bAutoClose
) :
116 BinaryXSeekableStream( Reference
< XSeekable
>( rxInStrm
, UNO_QUERY
) ),
117 maBuffer( INPUTSTREAM_BUFFERSIZE
),
118 mxInStrm( rxInStrm
),
119 mbAutoClose( bAutoClose
)
121 mbEof
= !mxInStrm
.is();
124 BinaryXInputStream::~BinaryXInputStream()
130 sal_Int32
BinaryXInputStream::readData( StreamDataSequence
& orData
, sal_Int32 nBytes
)
133 if( !mbEof
&& (nBytes
> 0) ) try
135 OSL_ENSURE( mxInStrm
.is(), "BinaryXInputStream::readData - invalid call" );
136 nRet
= mxInStrm
->readBytes( orData
, nBytes
);
137 mbEof
= nRet
!= nBytes
;
146 sal_Int32
BinaryXInputStream::readMemory( void* opMem
, sal_Int32 nBytes
)
149 if( !mbEof
&& (nBytes
> 0) )
151 sal_Int32 nBufferSize
= getLimitedValue
< sal_Int32
, sal_Int32
>( nBytes
, 0, INPUTSTREAM_BUFFERSIZE
);
152 sal_uInt8
* opnMem
= reinterpret_cast< sal_uInt8
* >( opMem
);
153 while( !mbEof
&& (nBytes
> 0) )
155 sal_Int32 nReadSize
= getLimitedValue
< sal_Int32
, sal_Int32
>( nBytes
, 0, nBufferSize
);
156 sal_Int32 nBytesRead
= readData( maBuffer
, nReadSize
);
158 memcpy( opnMem
, maBuffer
.getConstArray(), static_cast< size_t >( nBytesRead
) );
159 opnMem
+= nBytesRead
;
160 nBytes
-= nBytesRead
;
167 void BinaryXInputStream::skip( sal_Int32 nBytes
)
171 OSL_ENSURE( mxInStrm
.is(), "BinaryXInputStream::skip - invalid call" );
172 mxInStrm
->skipBytes( nBytes
);
180 void BinaryXInputStream::close()
182 if( mxInStrm
.is() ) try
184 mxInStrm
->closeInput();
189 OSL_ENSURE( false, "BinaryXInputStream::close - closing input stream failed" );
193 // ============================================================================
195 SequenceInputStream::SequenceInputStream( const StreamDataSequence
& rData
) :
196 SequenceSeekableStream( rData
)
200 sal_Int32
SequenceInputStream::readData( StreamDataSequence
& orData
, sal_Int32 nBytes
)
202 sal_Int32 nReadBytes
= 0;
205 nReadBytes
= getLimitedValue
< sal_Int32
, sal_Int32
>( nBytes
, 0, mrData
.getLength() - mnPos
);
206 orData
.realloc( nReadBytes
);
208 memcpy( orData
.getArray(), mrData
.getConstArray() + mnPos
, static_cast< size_t >( nReadBytes
) );
210 mbEof
= nReadBytes
< nBytes
;
215 sal_Int32
SequenceInputStream::readMemory( void* opMem
, sal_Int32 nBytes
)
217 sal_Int32 nReadBytes
= 0;
220 nReadBytes
= ::std::min
< sal_Int32
>( nBytes
, mrData
.getLength() - mnPos
);
222 memcpy( opMem
, mrData
.getConstArray() + mnPos
, static_cast< size_t >( nReadBytes
) );
224 mbEof
= nReadBytes
< nBytes
;
229 void SequenceInputStream::skip( sal_Int32 nBytes
)
233 sal_Int32 nSkipBytes
= ::std::min
< sal_Int32
>( nBytes
, mrData
.getLength() - mnPos
);
235 mbEof
= nSkipBytes
< nBytes
;
239 // ============================================================================