1*a3872823SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*a3872823SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*a3872823SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*a3872823SAndrew Rist  * distributed with this work for additional information
6*a3872823SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*a3872823SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*a3872823SAndrew Rist  * "License"); you may not use this file except in compliance
9*a3872823SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*a3872823SAndrew Rist  *
11*a3872823SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*a3872823SAndrew Rist  *
13*a3872823SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*a3872823SAndrew Rist  * software distributed under the License is distributed on an
15*a3872823SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*a3872823SAndrew Rist  * KIND, either express or implied.  See the License for the
17*a3872823SAndrew Rist  * specific language governing permissions and limitations
18*a3872823SAndrew Rist  * under the License.
19*a3872823SAndrew Rist  *
20*a3872823SAndrew Rist  *************************************************************/
21*a3872823SAndrew Rist 
22*a3872823SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_package.hxx"
26cdf0e10cSrcweir #include <ZipPackageBuffer.hxx>
27cdf0e10cSrcweir #include <string.h> // for memcpy
28cdf0e10cSrcweir 
29cdf0e10cSrcweir using namespace ::com::sun::star;
30cdf0e10cSrcweir using namespace com::sun::star::uno;
31cdf0e10cSrcweir using namespace com::sun::star::io;
32cdf0e10cSrcweir using com::sun::star::lang::IllegalArgumentException;
33cdf0e10cSrcweir 
ZipPackageBuffer(sal_Int64 nNewBufferSize)34cdf0e10cSrcweir ZipPackageBuffer::ZipPackageBuffer(sal_Int64 nNewBufferSize )
35cdf0e10cSrcweir : m_nBufferSize (nNewBufferSize)
36cdf0e10cSrcweir , m_nEnd(0)
37cdf0e10cSrcweir , m_nCurrent(0)
38cdf0e10cSrcweir , m_bMustInitBuffer ( sal_True )
39cdf0e10cSrcweir {
40cdf0e10cSrcweir }
~ZipPackageBuffer(void)41cdf0e10cSrcweir ZipPackageBuffer::~ZipPackageBuffer(void)
42cdf0e10cSrcweir {
43cdf0e10cSrcweir }
44cdf0e10cSrcweir 
readBytes(Sequence<sal_Int8> & aData,sal_Int32 nBytesToRead)45cdf0e10cSrcweir sal_Int32 SAL_CALL ZipPackageBuffer::readBytes( Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
46cdf0e10cSrcweir 		throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
47cdf0e10cSrcweir {
48cdf0e10cSrcweir 	if (nBytesToRead < 0)
49cdf0e10cSrcweir 		throw BufferSizeExceededException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), *this );
50cdf0e10cSrcweir 
51cdf0e10cSrcweir 	if (nBytesToRead + m_nCurrent > m_nEnd)
52cdf0e10cSrcweir 		nBytesToRead = static_cast < sal_Int32 > (m_nEnd - m_nCurrent);
53cdf0e10cSrcweir 
54cdf0e10cSrcweir 	aData.realloc ( nBytesToRead );
55cdf0e10cSrcweir 	memcpy(aData.getArray(), m_aBuffer.getConstArray() + m_nCurrent, nBytesToRead);
56cdf0e10cSrcweir 	m_nCurrent +=nBytesToRead;
57cdf0e10cSrcweir 	return nBytesToRead;
58cdf0e10cSrcweir }
59cdf0e10cSrcweir 
readSomeBytes(Sequence<sal_Int8> & aData,sal_Int32 nMaxBytesToRead)60cdf0e10cSrcweir sal_Int32 SAL_CALL ZipPackageBuffer::readSomeBytes( Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
61cdf0e10cSrcweir 		throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
62cdf0e10cSrcweir {
63cdf0e10cSrcweir 	return readBytes(aData, nMaxBytesToRead);
64cdf0e10cSrcweir }
skipBytes(sal_Int32 nBytesToSkip)65cdf0e10cSrcweir void SAL_CALL ZipPackageBuffer::skipBytes( sal_Int32 nBytesToSkip )
66cdf0e10cSrcweir 		throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
67cdf0e10cSrcweir {
68cdf0e10cSrcweir 	if (nBytesToSkip < 0)
69cdf0e10cSrcweir 		throw BufferSizeExceededException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), *this );
70cdf0e10cSrcweir 
71cdf0e10cSrcweir 	if (nBytesToSkip + m_nCurrent > m_nEnd)
72cdf0e10cSrcweir 		nBytesToSkip = static_cast < sal_Int32 > (m_nEnd - m_nCurrent);
73cdf0e10cSrcweir 
74cdf0e10cSrcweir 	m_nCurrent+=nBytesToSkip;
75cdf0e10cSrcweir }
available()76cdf0e10cSrcweir sal_Int32 SAL_CALL ZipPackageBuffer::available(  )
77cdf0e10cSrcweir 		throw(NotConnectedException, IOException, RuntimeException)
78cdf0e10cSrcweir {
79cdf0e10cSrcweir 	return static_cast < sal_Int32 > (m_nEnd - m_nCurrent);
80cdf0e10cSrcweir }
closeInput()81cdf0e10cSrcweir void SAL_CALL ZipPackageBuffer::closeInput(  )
82cdf0e10cSrcweir 		throw(NotConnectedException, IOException, RuntimeException)
83cdf0e10cSrcweir {
84cdf0e10cSrcweir }
writeBytes(const Sequence<sal_Int8> & aData)85cdf0e10cSrcweir void SAL_CALL ZipPackageBuffer::writeBytes( const Sequence< sal_Int8 >& aData )
86cdf0e10cSrcweir 		throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
87cdf0e10cSrcweir {
88cdf0e10cSrcweir 	sal_Int64 nDataLen = aData.getLength(), nCombined = m_nEnd + nDataLen;
89cdf0e10cSrcweir 
90cdf0e10cSrcweir 	if ( nCombined > m_nBufferSize)
91cdf0e10cSrcweir 	{
92cdf0e10cSrcweir 		do
93cdf0e10cSrcweir 			m_nBufferSize *=2;
94cdf0e10cSrcweir 		while (nCombined > m_nBufferSize);
95cdf0e10cSrcweir 		m_aBuffer.realloc(static_cast < sal_Int32 > (m_nBufferSize));
96cdf0e10cSrcweir 		m_bMustInitBuffer = sal_False;
97cdf0e10cSrcweir 	}
98cdf0e10cSrcweir 	else if (m_bMustInitBuffer)
99cdf0e10cSrcweir 	{
100cdf0e10cSrcweir  		m_aBuffer.realloc ( static_cast < sal_Int32 > ( m_nBufferSize ) );
101cdf0e10cSrcweir 		m_bMustInitBuffer = sal_False;
102cdf0e10cSrcweir 	}
103cdf0e10cSrcweir 	memcpy( m_aBuffer.getArray() + m_nCurrent, aData.getConstArray(), static_cast < sal_Int32 > (nDataLen));
104cdf0e10cSrcweir 	m_nCurrent+=nDataLen;
105cdf0e10cSrcweir 	if (m_nCurrent>m_nEnd)
106cdf0e10cSrcweir 		m_nEnd = m_nCurrent;
107cdf0e10cSrcweir }
flush()108cdf0e10cSrcweir void SAL_CALL ZipPackageBuffer::flush(  )
109cdf0e10cSrcweir 		throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
110cdf0e10cSrcweir {
111cdf0e10cSrcweir }
closeOutput()112cdf0e10cSrcweir void SAL_CALL ZipPackageBuffer::closeOutput(  )
113cdf0e10cSrcweir 		throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
114cdf0e10cSrcweir {
115cdf0e10cSrcweir }
seek(sal_Int64 location)116cdf0e10cSrcweir void SAL_CALL ZipPackageBuffer::seek( sal_Int64 location )
117cdf0e10cSrcweir 		throw( IllegalArgumentException, IOException, RuntimeException)
118cdf0e10cSrcweir {
119cdf0e10cSrcweir 	if ( location > m_nEnd || location < 0 )
120cdf0e10cSrcweir 		throw IllegalArgumentException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >(), 1 );
121cdf0e10cSrcweir 	m_nCurrent = location;
122cdf0e10cSrcweir }
getPosition()123cdf0e10cSrcweir sal_Int64 SAL_CALL ZipPackageBuffer::getPosition(  )
124cdf0e10cSrcweir 		throw(IOException, RuntimeException)
125cdf0e10cSrcweir {
126cdf0e10cSrcweir 	return m_nCurrent;
127cdf0e10cSrcweir }
getLength()128cdf0e10cSrcweir sal_Int64 SAL_CALL ZipPackageBuffer::getLength(  )
129cdf0e10cSrcweir 		throw(IOException, RuntimeException)
130cdf0e10cSrcweir {
131cdf0e10cSrcweir 	return m_nEnd;
132cdf0e10cSrcweir }
133