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