xref: /trunk/main/package/source/package/zippackage/ZipPackageBuffer.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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 {
47     if (nBytesToRead < 0)
48         throw BufferSizeExceededException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), *this );
49 
50     if (nBytesToRead + m_nCurrent > m_nEnd)
51         nBytesToRead = static_cast < sal_Int32 > (m_nEnd - m_nCurrent);
52 
53     aData.realloc ( nBytesToRead );
54     memcpy(aData.getArray(), m_aBuffer.getConstArray() + m_nCurrent, nBytesToRead);
55     m_nCurrent +=nBytesToRead;
56     return nBytesToRead;
57 }
58 
readSomeBytes(Sequence<sal_Int8> & aData,sal_Int32 nMaxBytesToRead)59 sal_Int32 SAL_CALL ZipPackageBuffer::readSomeBytes( Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
60 {
61     return readBytes(aData, nMaxBytesToRead);
62 }
skipBytes(sal_Int32 nBytesToSkip)63 void SAL_CALL ZipPackageBuffer::skipBytes( sal_Int32 nBytesToSkip )
64 {
65     if (nBytesToSkip < 0)
66         throw BufferSizeExceededException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), *this );
67 
68     if (nBytesToSkip + m_nCurrent > m_nEnd)
69         nBytesToSkip = static_cast < sal_Int32 > (m_nEnd - m_nCurrent);
70 
71     m_nCurrent+=nBytesToSkip;
72 }
available()73 sal_Int32 SAL_CALL ZipPackageBuffer::available(  )
74 {
75     return static_cast < sal_Int32 > (m_nEnd - m_nCurrent);
76 }
closeInput()77 void SAL_CALL ZipPackageBuffer::closeInput(  )
78 {
79 }
writeBytes(const Sequence<sal_Int8> & aData)80 void SAL_CALL ZipPackageBuffer::writeBytes( const Sequence< sal_Int8 >& aData )
81 {
82     sal_Int64 nDataLen = aData.getLength(), nCombined = m_nEnd + nDataLen;
83 
84     if ( nCombined > m_nBufferSize)
85     {
86         do
87             m_nBufferSize *=2;
88         while (nCombined > m_nBufferSize);
89         m_aBuffer.realloc(static_cast < sal_Int32 > (m_nBufferSize));
90         m_bMustInitBuffer = sal_False;
91     }
92     else if (m_bMustInitBuffer)
93     {
94         m_aBuffer.realloc ( static_cast < sal_Int32 > ( m_nBufferSize ) );
95         m_bMustInitBuffer = sal_False;
96     }
97     memcpy( m_aBuffer.getArray() + m_nCurrent, aData.getConstArray(), static_cast < sal_Int32 > (nDataLen));
98     m_nCurrent+=nDataLen;
99     if (m_nCurrent>m_nEnd)
100         m_nEnd = m_nCurrent;
101 }
flush()102 void SAL_CALL ZipPackageBuffer::flush(  )
103 {
104 }
closeOutput()105 void SAL_CALL ZipPackageBuffer::closeOutput(  )
106 {
107 }
seek(sal_Int64 location)108 void SAL_CALL ZipPackageBuffer::seek( sal_Int64 location )
109 {
110     if ( location > m_nEnd || location < 0 )
111         throw IllegalArgumentException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >(), 1 );
112     m_nCurrent = location;
113 }
getPosition()114 sal_Int64 SAL_CALL ZipPackageBuffer::getPosition(  )
115 {
116     return m_nCurrent;
117 }
getLength()118 sal_Int64 SAL_CALL ZipPackageBuffer::getLength(  )
119 {
120     return m_nEnd;
121 }
122