Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <ZipPackageBuffer.hxx>
21 : #include <string.h> // for memcpy
22 :
23 : using namespace ::com::sun::star;
24 : using namespace com::sun::star::uno;
25 : using namespace com::sun::star::io;
26 : using com::sun::star::lang::IllegalArgumentException;
27 :
28 33 : ZipPackageBuffer::ZipPackageBuffer(sal_Int64 nNewBufferSize )
29 : : m_nBufferSize (nNewBufferSize)
30 : , m_nEnd(0)
31 : , m_nCurrent(0)
32 33 : , m_bMustInitBuffer ( sal_True )
33 : {
34 33 : }
35 66 : ZipPackageBuffer::~ZipPackageBuffer(void)
36 : {
37 66 : }
38 :
39 0 : sal_Int32 SAL_CALL ZipPackageBuffer::readBytes( Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
40 : throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
41 : {
42 0 : if (nBytesToRead < 0)
43 0 : throw BufferSizeExceededException(OSL_LOG_PREFIX, *this );
44 :
45 0 : if (nBytesToRead + m_nCurrent > m_nEnd)
46 0 : nBytesToRead = static_cast < sal_Int32 > (m_nEnd - m_nCurrent);
47 :
48 0 : aData.realloc ( nBytesToRead );
49 0 : memcpy(aData.getArray(), m_aBuffer.getConstArray() + m_nCurrent, nBytesToRead);
50 0 : m_nCurrent +=nBytesToRead;
51 0 : return nBytesToRead;
52 : }
53 :
54 0 : sal_Int32 SAL_CALL ZipPackageBuffer::readSomeBytes( Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
55 : throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
56 : {
57 0 : return readBytes(aData, nMaxBytesToRead);
58 : }
59 0 : void SAL_CALL ZipPackageBuffer::skipBytes( sal_Int32 nBytesToSkip )
60 : throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
61 : {
62 0 : if (nBytesToSkip < 0)
63 0 : throw BufferSizeExceededException(OSL_LOG_PREFIX, *this );
64 :
65 0 : if (nBytesToSkip + m_nCurrent > m_nEnd)
66 0 : nBytesToSkip = static_cast < sal_Int32 > (m_nEnd - m_nCurrent);
67 :
68 0 : m_nCurrent+=nBytesToSkip;
69 0 : }
70 0 : sal_Int32 SAL_CALL ZipPackageBuffer::available( )
71 : throw(NotConnectedException, IOException, RuntimeException)
72 : {
73 0 : return static_cast < sal_Int32 > (m_nEnd - m_nCurrent);
74 : }
75 0 : void SAL_CALL ZipPackageBuffer::closeInput( )
76 : throw(NotConnectedException, IOException, RuntimeException)
77 : {
78 0 : }
79 63 : void SAL_CALL ZipPackageBuffer::writeBytes( const Sequence< sal_Int8 >& aData )
80 : throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
81 : {
82 63 : sal_Int64 nDataLen = aData.getLength(), nCombined = m_nEnd + nDataLen;
83 :
84 63 : if ( nCombined > m_nBufferSize)
85 : {
86 0 : do
87 0 : m_nBufferSize *=2;
88 : while (nCombined > m_nBufferSize);
89 0 : m_aBuffer.realloc(static_cast < sal_Int32 > (m_nBufferSize));
90 0 : m_bMustInitBuffer = sal_False;
91 : }
92 63 : else if (m_bMustInitBuffer)
93 : {
94 33 : m_aBuffer.realloc ( static_cast < sal_Int32 > ( m_nBufferSize ) );
95 33 : m_bMustInitBuffer = sal_False;
96 : }
97 63 : memcpy( m_aBuffer.getArray() + m_nCurrent, aData.getConstArray(), static_cast < sal_Int32 > (nDataLen));
98 63 : m_nCurrent+=nDataLen;
99 63 : if (m_nCurrent>m_nEnd)
100 63 : m_nEnd = m_nCurrent;
101 63 : }
102 0 : void SAL_CALL ZipPackageBuffer::flush( )
103 : throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
104 : {
105 0 : }
106 33 : void SAL_CALL ZipPackageBuffer::closeOutput( )
107 : throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
108 : {
109 33 : }
110 0 : void SAL_CALL ZipPackageBuffer::seek( sal_Int64 location )
111 : throw( IllegalArgumentException, IOException, RuntimeException)
112 : {
113 0 : if ( location > m_nEnd || location < 0 )
114 0 : throw IllegalArgumentException(OSL_LOG_PREFIX, uno::Reference< uno::XInterface >(), 1 );
115 0 : m_nCurrent = location;
116 0 : }
117 33 : sal_Int64 SAL_CALL ZipPackageBuffer::getPosition( )
118 : throw(IOException, RuntimeException)
119 : {
120 33 : return m_nCurrent;
121 : }
122 0 : sal_Int64 SAL_CALL ZipPackageBuffer::getLength( )
123 : throw(IOException, RuntimeException)
124 : {
125 0 : return m_nEnd;
126 : }
127 :
128 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|