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