Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / package / source / zippackage / ZipPackageBuffer.cxx
blob8df0f3a96c9bea70533971455dc52d6310c72f36
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <ZipPackageBuffer.hxx>
21 #include <PackageConstants.hxx>
22 #include <string.h>
24 #include <com/sun/star/io/BufferSizeExceededException.hpp>
25 #include <com/sun/star/lang/IllegalArgumentException.hpp>
27 using namespace ::com::sun::star;
28 using namespace com::sun::star::uno;
29 using namespace com::sun::star::io;
30 using com::sun::star::lang::IllegalArgumentException;
32 #if OSL_DEBUG_LEVEL > 0
33 #define THROW_WHERE SAL_WHERE
34 #else
35 #define THROW_WHERE ""
36 #endif
38 ZipPackageBuffer::ZipPackageBuffer()
39 : m_nBufferSize (n_ConstBufferSize)
40 , m_nEnd(0)
41 , m_nCurrent(0)
42 , m_bMustInitBuffer ( true )
45 ZipPackageBuffer::~ZipPackageBuffer()
49 sal_Int32 SAL_CALL ZipPackageBuffer::readBytes( Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
51 if (nBytesToRead < 0)
52 throw BufferSizeExceededException(THROW_WHERE, *this );
54 if (nBytesToRead + m_nCurrent > m_nEnd)
55 nBytesToRead = static_cast < sal_Int32 > (m_nEnd - m_nCurrent);
57 aData.realloc ( nBytesToRead );
58 memcpy(aData.getArray(), m_aBuffer.getConstArray() + m_nCurrent, nBytesToRead);
59 m_nCurrent +=nBytesToRead;
60 return nBytesToRead;
63 sal_Int32 SAL_CALL ZipPackageBuffer::readSomeBytes( Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
65 return readBytes(aData, nMaxBytesToRead);
67 void SAL_CALL ZipPackageBuffer::skipBytes( sal_Int32 nBytesToSkip )
69 if (nBytesToSkip < 0)
70 throw BufferSizeExceededException(THROW_WHERE, *this );
72 if (nBytesToSkip + m_nCurrent > m_nEnd)
73 nBytesToSkip = static_cast < sal_Int32 > (m_nEnd - m_nCurrent);
75 m_nCurrent+=nBytesToSkip;
77 sal_Int32 SAL_CALL ZipPackageBuffer::available( )
79 return static_cast < sal_Int32 > (m_nEnd - m_nCurrent);
81 void SAL_CALL ZipPackageBuffer::closeInput( )
84 void SAL_CALL ZipPackageBuffer::writeBytes( const Sequence< sal_Int8 >& aData )
86 sal_Int64 nDataLen = aData.getLength(), nCombined = m_nEnd + nDataLen;
88 if ( nCombined > m_nBufferSize)
91 m_nBufferSize *=2;
92 while (nCombined > m_nBufferSize);
93 m_aBuffer.realloc(static_cast < sal_Int32 > (m_nBufferSize));
94 m_bMustInitBuffer = false;
96 else if (m_bMustInitBuffer)
98 m_aBuffer.realloc ( static_cast < sal_Int32 > ( m_nBufferSize ) );
99 m_bMustInitBuffer = false;
101 memcpy( m_aBuffer.getArray() + m_nCurrent, aData.getConstArray(), static_cast < sal_Int32 > (nDataLen));
102 m_nCurrent+=nDataLen;
103 if (m_nCurrent>m_nEnd)
104 m_nEnd = m_nCurrent;
106 void SAL_CALL ZipPackageBuffer::flush( )
109 void SAL_CALL ZipPackageBuffer::closeOutput( )
112 void SAL_CALL ZipPackageBuffer::seek( sal_Int64 location )
114 if ( location > m_nEnd || location < 0 )
115 throw IllegalArgumentException(THROW_WHERE, uno::Reference< uno::XInterface >(), 1 );
116 m_nCurrent = location;
118 sal_Int64 SAL_CALL ZipPackageBuffer::getPosition( )
120 return m_nCurrent;
122 sal_Int64 SAL_CALL ZipPackageBuffer::getLength( )
124 return m_nEnd;
127 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */