merge the formfield patch from ooo-build
[ooovba.git] / oox / source / helper / binaryinputstream.cxx
blob5f7f0c29591e3e8b7a586df00acaf3789b62b2a3
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"
32 #include <string.h>
33 #include <vector>
34 #include <rtl/strbuf.hxx>
35 #include <rtl/ustrbuf.hxx>
37 using ::rtl::OString;
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;
48 namespace oox {
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 )
77 if( nChars <= 0 )
78 return OString();
80 ::std::vector< sal_Char > aBuffer( static_cast< size_t >( nChars ) );
81 size_t nCharsRead = static_cast< size_t >( readMemory( &aBuffer.front(), nChars ) );
82 if( !bAllowNulChars )
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;
95 if( nChars > 0 )
97 aBuffer.ensureCapacity( nChars );
98 sal_uInt16 nChar;
99 for( sal_uInt16 nCharIdx = 0; !mbEof && (nCharIdx < nChars); ++nCharIdx )
101 readValue( nChar );
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()
126 if( mbAutoClose )
127 close();
130 sal_Int32 BinaryXInputStream::readData( StreamDataSequence& orData, sal_Int32 nBytes )
132 sal_Int32 nRet = 0;
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;
139 catch( Exception& )
141 mbEof = true;
143 return nRet;
146 sal_Int32 BinaryXInputStream::readMemory( void* opMem, sal_Int32 nBytes )
148 sal_Int32 nRet = 0;
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 );
157 if( nBytesRead > 0 )
158 memcpy( opnMem, maBuffer.getConstArray(), static_cast< size_t >( nBytesRead ) );
159 opnMem += nBytesRead;
160 nBytes -= nBytesRead;
161 nRet += nBytesRead;
164 return nRet;
167 void BinaryXInputStream::skip( sal_Int32 nBytes )
169 if( !mbEof ) try
171 OSL_ENSURE( mxInStrm.is(), "BinaryXInputStream::skip - invalid call" );
172 mxInStrm->skipBytes( nBytes );
174 catch( Exception& )
176 mbEof = true;
180 void BinaryXInputStream::close()
182 if( mxInStrm.is() ) try
184 mxInStrm->closeInput();
185 mxInStrm.clear();
187 catch( Exception& )
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;
203 if( !mbEof )
205 nReadBytes = getLimitedValue< sal_Int32, sal_Int32 >( nBytes, 0, mrData.getLength() - mnPos );
206 orData.realloc( nReadBytes );
207 if( nReadBytes > 0 )
208 memcpy( orData.getArray(), mrData.getConstArray() + mnPos, static_cast< size_t >( nReadBytes ) );
209 mnPos += nReadBytes;
210 mbEof = nReadBytes < nBytes;
212 return nReadBytes;
215 sal_Int32 SequenceInputStream::readMemory( void* opMem, sal_Int32 nBytes )
217 sal_Int32 nReadBytes = 0;
218 if( !mbEof )
220 nReadBytes = ::std::min< sal_Int32 >( nBytes, mrData.getLength() - mnPos );
221 if( nReadBytes > 0 )
222 memcpy( opMem, mrData.getConstArray() + mnPos, static_cast< size_t >( nReadBytes ) );
223 mnPos += nReadBytes;
224 mbEof = nReadBytes < nBytes;
226 return nReadBytes;
229 void SequenceInputStream::skip( sal_Int32 nBytes )
231 if( !mbEof )
233 sal_Int32 nSkipBytes = ::std::min< sal_Int32 >( nBytes, mrData.getLength() - mnPos );
234 mnPos += nSkipBytes;
235 mbEof = nSkipBytes < nBytes;
239 // ============================================================================
241 } // namespace oox