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: binaryoutputstream.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/binaryoutputstream.hxx"
32 #include <osl/diagnose.h>
33 #include "oox/helper/binaryinputstream.hxx"
36 using ::com::sun::star::uno::UNO_QUERY
;
37 using ::com::sun::star::uno::Reference
;
38 using ::com::sun::star::uno::Exception
;
39 using ::com::sun::star::io::XOutputStream
;
40 using ::com::sun::star::io::XSeekable
;
44 const sal_Int32 OUTPUTSTREAM_BUFFERSIZE
= 0x8000;
46 // ============================================================================
48 void BinaryOutputStream::copyStream( BinaryInputStream
& rInStrm
, sal_Int64 nBytes
)
52 sal_Int32 nBufferSize
= getLimitedValue
< sal_Int32
, sal_Int64
>( nBytes
, 0, OUTPUTSTREAM_BUFFERSIZE
);
53 StreamDataSequence
aBuffer( nBufferSize
);
56 sal_Int32 nReadSize
= getLimitedValue
< sal_Int32
, sal_Int64
>( nBytes
, 0, nBufferSize
);
57 sal_Int32 nBytesRead
= rInStrm
.readData( aBuffer
, nReadSize
);
59 if( nReadSize
== nBytesRead
)
67 void BinaryOutputStream::writeAtom( const void* pMem
, sal_uInt8 nSize
)
69 writeMemory( pMem
, nSize
);
72 // ============================================================================
74 BinaryXOutputStream::BinaryXOutputStream( const Reference
< XOutputStream
>& rxOutStrm
, bool bAutoClose
) :
75 BinaryXSeekableStream( Reference
< XSeekable
>( rxOutStrm
, UNO_QUERY
) ),
76 maBuffer( OUTPUTSTREAM_BUFFERSIZE
),
77 mxOutStrm( rxOutStrm
),
78 mbAutoClose( bAutoClose
)
80 mbEof
= !mxOutStrm
.is();
83 BinaryXOutputStream::~BinaryXOutputStream()
89 void BinaryXOutputStream::writeData( const StreamDataSequence
& rData
)
93 OSL_ENSURE( mxOutStrm
.is(), "BinaryXOutputStream::writeData - invalid call" );
94 mxOutStrm
->writeBytes( rData
);
98 OSL_ENSURE( false, "BinaryXOutputStream::writeData - stream read error" );
102 void BinaryXOutputStream::writeMemory( const void* pMem
, sal_Int32 nBytes
)
106 sal_Int32 nBufferSize
= getLimitedValue
< sal_Int32
, sal_Int32
>( nBytes
, 0, OUTPUTSTREAM_BUFFERSIZE
);
107 const sal_uInt8
* pnMem
= reinterpret_cast< const sal_uInt8
* >( pMem
);
110 sal_Int32 nWriteSize
= getLimitedValue
< sal_Int32
, sal_Int32
>( nBytes
, 0, nBufferSize
);
111 maBuffer
.realloc( nWriteSize
);
112 memcpy( maBuffer
.getArray(), pnMem
, static_cast< size_t >( nWriteSize
) );
113 writeData( maBuffer
);
115 nBytes
-= nWriteSize
;
120 void BinaryXOutputStream::close()
122 if( mxOutStrm
.is() ) try
125 mxOutStrm
->closeOutput();
129 OSL_ENSURE( false, "BinaryXOutputStream::close - closing output stream failed" );
133 // ============================================================================
135 SequenceOutputStream::SequenceOutputStream( StreamDataSequence
& rData
) :
136 SequenceSeekableStream( rData
)
140 void SequenceOutputStream::writeData( const StreamDataSequence
& rData
)
142 if( rData
.hasElements() )
143 writeMemory( rData
.getConstArray(), rData
.getLength() );
146 void SequenceOutputStream::writeMemory( const void* pMem
, sal_Int32 nBytes
)
150 if( mrData
.getLength() - mnPos
< nBytes
)
151 const_cast< StreamDataSequence
& >( mrData
).realloc( mnPos
+ nBytes
);
152 memcpy( const_cast< StreamDataSequence
& >( mrData
).getArray() + mnPos
, pMem
, static_cast< size_t >( nBytes
) );
157 // ============================================================================