1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_package.hxx"
30*cdf0e10cSrcweir #include <ZipPackageBuffer.hxx>
31*cdf0e10cSrcweir #include <string.h> // for memcpy
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir using namespace ::com::sun::star;
34*cdf0e10cSrcweir using namespace com::sun::star::uno;
35*cdf0e10cSrcweir using namespace com::sun::star::io;
36*cdf0e10cSrcweir using com::sun::star::lang::IllegalArgumentException;
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir ZipPackageBuffer::ZipPackageBuffer(sal_Int64 nNewBufferSize )
39*cdf0e10cSrcweir : m_nBufferSize (nNewBufferSize)
40*cdf0e10cSrcweir , m_nEnd(0)
41*cdf0e10cSrcweir , m_nCurrent(0)
42*cdf0e10cSrcweir , m_bMustInitBuffer ( sal_True )
43*cdf0e10cSrcweir {
44*cdf0e10cSrcweir }
45*cdf0e10cSrcweir ZipPackageBuffer::~ZipPackageBuffer(void)
46*cdf0e10cSrcweir {
47*cdf0e10cSrcweir }
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir sal_Int32 SAL_CALL ZipPackageBuffer::readBytes( Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
50*cdf0e10cSrcweir 		throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
51*cdf0e10cSrcweir {
52*cdf0e10cSrcweir 	if (nBytesToRead < 0)
53*cdf0e10cSrcweir 		throw BufferSizeExceededException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), *this );
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir 	if (nBytesToRead + m_nCurrent > m_nEnd)
56*cdf0e10cSrcweir 		nBytesToRead = static_cast < sal_Int32 > (m_nEnd - m_nCurrent);
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir 	aData.realloc ( nBytesToRead );
59*cdf0e10cSrcweir 	memcpy(aData.getArray(), m_aBuffer.getConstArray() + m_nCurrent, nBytesToRead);
60*cdf0e10cSrcweir 	m_nCurrent +=nBytesToRead;
61*cdf0e10cSrcweir 	return nBytesToRead;
62*cdf0e10cSrcweir }
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir sal_Int32 SAL_CALL ZipPackageBuffer::readSomeBytes( Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
65*cdf0e10cSrcweir 		throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
66*cdf0e10cSrcweir {
67*cdf0e10cSrcweir 	return readBytes(aData, nMaxBytesToRead);
68*cdf0e10cSrcweir }
69*cdf0e10cSrcweir void SAL_CALL ZipPackageBuffer::skipBytes( sal_Int32 nBytesToSkip )
70*cdf0e10cSrcweir 		throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
71*cdf0e10cSrcweir {
72*cdf0e10cSrcweir 	if (nBytesToSkip < 0)
73*cdf0e10cSrcweir 		throw BufferSizeExceededException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), *this );
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir 	if (nBytesToSkip + m_nCurrent > m_nEnd)
76*cdf0e10cSrcweir 		nBytesToSkip = static_cast < sal_Int32 > (m_nEnd - m_nCurrent);
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir 	m_nCurrent+=nBytesToSkip;
79*cdf0e10cSrcweir }
80*cdf0e10cSrcweir sal_Int32 SAL_CALL ZipPackageBuffer::available(  )
81*cdf0e10cSrcweir 		throw(NotConnectedException, IOException, RuntimeException)
82*cdf0e10cSrcweir {
83*cdf0e10cSrcweir 	return static_cast < sal_Int32 > (m_nEnd - m_nCurrent);
84*cdf0e10cSrcweir }
85*cdf0e10cSrcweir void SAL_CALL ZipPackageBuffer::closeInput(  )
86*cdf0e10cSrcweir 		throw(NotConnectedException, IOException, RuntimeException)
87*cdf0e10cSrcweir {
88*cdf0e10cSrcweir }
89*cdf0e10cSrcweir void SAL_CALL ZipPackageBuffer::writeBytes( const Sequence< sal_Int8 >& aData )
90*cdf0e10cSrcweir 		throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
91*cdf0e10cSrcweir {
92*cdf0e10cSrcweir 	sal_Int64 nDataLen = aData.getLength(), nCombined = m_nEnd + nDataLen;
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir 	if ( nCombined > m_nBufferSize)
95*cdf0e10cSrcweir 	{
96*cdf0e10cSrcweir 		do
97*cdf0e10cSrcweir 			m_nBufferSize *=2;
98*cdf0e10cSrcweir 		while (nCombined > m_nBufferSize);
99*cdf0e10cSrcweir 		m_aBuffer.realloc(static_cast < sal_Int32 > (m_nBufferSize));
100*cdf0e10cSrcweir 		m_bMustInitBuffer = sal_False;
101*cdf0e10cSrcweir 	}
102*cdf0e10cSrcweir 	else if (m_bMustInitBuffer)
103*cdf0e10cSrcweir 	{
104*cdf0e10cSrcweir  		m_aBuffer.realloc ( static_cast < sal_Int32 > ( m_nBufferSize ) );
105*cdf0e10cSrcweir 		m_bMustInitBuffer = sal_False;
106*cdf0e10cSrcweir 	}
107*cdf0e10cSrcweir 	memcpy( m_aBuffer.getArray() + m_nCurrent, aData.getConstArray(), static_cast < sal_Int32 > (nDataLen));
108*cdf0e10cSrcweir 	m_nCurrent+=nDataLen;
109*cdf0e10cSrcweir 	if (m_nCurrent>m_nEnd)
110*cdf0e10cSrcweir 		m_nEnd = m_nCurrent;
111*cdf0e10cSrcweir }
112*cdf0e10cSrcweir void SAL_CALL ZipPackageBuffer::flush(  )
113*cdf0e10cSrcweir 		throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
114*cdf0e10cSrcweir {
115*cdf0e10cSrcweir }
116*cdf0e10cSrcweir void SAL_CALL ZipPackageBuffer::closeOutput(  )
117*cdf0e10cSrcweir 		throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
118*cdf0e10cSrcweir {
119*cdf0e10cSrcweir }
120*cdf0e10cSrcweir void SAL_CALL ZipPackageBuffer::seek( sal_Int64 location )
121*cdf0e10cSrcweir 		throw( IllegalArgumentException, IOException, RuntimeException)
122*cdf0e10cSrcweir {
123*cdf0e10cSrcweir 	if ( location > m_nEnd || location < 0 )
124*cdf0e10cSrcweir 		throw IllegalArgumentException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >(), 1 );
125*cdf0e10cSrcweir 	m_nCurrent = location;
126*cdf0e10cSrcweir }
127*cdf0e10cSrcweir sal_Int64 SAL_CALL ZipPackageBuffer::getPosition(  )
128*cdf0e10cSrcweir 		throw(IOException, RuntimeException)
129*cdf0e10cSrcweir {
130*cdf0e10cSrcweir 	return m_nCurrent;
131*cdf0e10cSrcweir }
132*cdf0e10cSrcweir sal_Int64 SAL_CALL ZipPackageBuffer::getLength(  )
133*cdf0e10cSrcweir 		throw(IOException, RuntimeException)
134*cdf0e10cSrcweir {
135*cdf0e10cSrcweir 	return m_nEnd;
136*cdf0e10cSrcweir }
137