/src/libreoffice/package/source/zippackage/ZipPackageBuffer.cxx
Line | Count | Source |
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 <PackageConstants.hxx> |
22 | | #include <algorithm> |
23 | | #include <string.h> |
24 | | #include <sal/log.hxx> |
25 | | |
26 | | #include <com/sun/star/io/BufferSizeExceededException.hpp> |
27 | | #include <com/sun/star/lang/IllegalArgumentException.hpp> |
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 | | |
34 | | ZipPackageBuffer::ZipPackageBuffer() |
35 | 0 | : m_nBufferSize (n_ConstBufferSize) |
36 | 0 | , m_nEnd(0) |
37 | 0 | , m_nCurrent(0) |
38 | 0 | , m_bMustInitBuffer ( true ) |
39 | 0 | { |
40 | 0 | } |
41 | | ZipPackageBuffer::~ZipPackageBuffer() |
42 | 0 | { |
43 | 0 | } |
44 | | |
45 | | sal_Int32 SAL_CALL ZipPackageBuffer::readBytes( Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead ) |
46 | 0 | { |
47 | 0 | if (nBytesToRead < 0) |
48 | 0 | throw BufferSizeExceededException(u""_ustr, *this ); |
49 | | |
50 | 0 | if (nBytesToRead + m_nCurrent > m_nEnd) |
51 | 0 | nBytesToRead = static_cast < sal_Int32 > (m_nEnd - m_nCurrent); |
52 | |
|
53 | 0 | aData.realloc ( nBytesToRead ); |
54 | 0 | std::copy_n(m_aBuffer.getConstArray() + m_nCurrent, nBytesToRead, aData.getArray()); |
55 | 0 | m_nCurrent +=nBytesToRead; |
56 | 0 | return nBytesToRead; |
57 | 0 | } |
58 | | |
59 | | sal_Int32 SAL_CALL ZipPackageBuffer::readSomeBytes( Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead ) |
60 | 0 | { |
61 | 0 | return readBytes(aData, nMaxBytesToRead); |
62 | 0 | } |
63 | | void SAL_CALL ZipPackageBuffer::skipBytes( sal_Int32 nBytesToSkip ) |
64 | 0 | { |
65 | 0 | if (nBytesToSkip < 0) |
66 | 0 | throw BufferSizeExceededException(u""_ustr, *this ); |
67 | | |
68 | 0 | if (nBytesToSkip + m_nCurrent > m_nEnd) |
69 | 0 | nBytesToSkip = static_cast < sal_Int32 > (m_nEnd - m_nCurrent); |
70 | |
|
71 | 0 | m_nCurrent+=nBytesToSkip; |
72 | 0 | } |
73 | | sal_Int32 SAL_CALL ZipPackageBuffer::available( ) |
74 | 0 | { |
75 | 0 | return std::min<sal_Int64>(SAL_MAX_INT32, m_nEnd - m_nCurrent); |
76 | 0 | } |
77 | | void SAL_CALL ZipPackageBuffer::closeInput( ) |
78 | 0 | { |
79 | 0 | } |
80 | | void SAL_CALL ZipPackageBuffer::writeBytes( const Sequence< sal_Int8 >& aData ) |
81 | 0 | { |
82 | 0 | sal_Int64 nDataLen = aData.getLength(), nCombined = m_nEnd + nDataLen; |
83 | |
|
84 | 0 | if ( nCombined > m_nBufferSize) |
85 | 0 | { |
86 | 0 | do |
87 | 0 | m_nBufferSize *=2; |
88 | 0 | while (nCombined > m_nBufferSize); |
89 | 0 | m_aBuffer.realloc(static_cast < sal_Int32 > (m_nBufferSize)); |
90 | 0 | m_bMustInitBuffer = false; |
91 | 0 | } |
92 | 0 | else if (m_bMustInitBuffer) |
93 | 0 | { |
94 | 0 | m_aBuffer.realloc ( static_cast < sal_Int32 > ( m_nBufferSize ) ); |
95 | 0 | m_bMustInitBuffer = false; |
96 | 0 | } |
97 | 0 | std::copy_n(aData.getConstArray(), static_cast<sal_Int32>(nDataLen), |
98 | 0 | m_aBuffer.getArray() + m_nCurrent); |
99 | 0 | m_nCurrent+=nDataLen; |
100 | 0 | if (m_nCurrent>m_nEnd) |
101 | 0 | m_nEnd = m_nCurrent; |
102 | 0 | } |
103 | | void SAL_CALL ZipPackageBuffer::flush( ) |
104 | 0 | { |
105 | 0 | } |
106 | | void SAL_CALL ZipPackageBuffer::closeOutput( ) |
107 | 0 | { |
108 | 0 | } |
109 | | void SAL_CALL ZipPackageBuffer::seek( sal_Int64 location ) |
110 | 0 | { |
111 | 0 | if ( location > m_nEnd || location < 0 ) |
112 | 0 | throw IllegalArgumentException(u""_ustr, uno::Reference< uno::XInterface >(), 1 ); |
113 | 0 | m_nCurrent = location; |
114 | 0 | } |
115 | | sal_Int64 SAL_CALL ZipPackageBuffer::getPosition( ) |
116 | 0 | { |
117 | 0 | return m_nCurrent; |
118 | 0 | } |
119 | | sal_Int64 SAL_CALL ZipPackageBuffer::getLength( ) |
120 | 0 | { |
121 | 0 | return m_nEnd; |
122 | 0 | } |
123 | | |
124 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |