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 <comphelper/oslfile2streamwrap.hxx>
21 :
22 : #include <algorithm>
23 :
24 : namespace comphelper
25 : {
26 : using namespace osl;
27 :
28 :
29 0 : OSLInputStreamWrapper::OSLInputStreamWrapper( File& _rFile )
30 0 : : m_pFile(&_rFile)
31 : {
32 0 : }
33 :
34 :
35 0 : OSLInputStreamWrapper::~OSLInputStreamWrapper()
36 : {
37 0 : }
38 :
39 :
40 0 : sal_Int32 SAL_CALL OSLInputStreamWrapper::readBytes(css::uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead)
41 : throw( css::io::NotConnectedException, css::io::BufferSizeExceededException, css::uno::RuntimeException, std::exception )
42 : {
43 0 : if (!m_pFile)
44 0 : throw css::io::NotConnectedException(OUString(), static_cast<css::uno::XWeak*>(this));
45 :
46 0 : if (nBytesToRead < 0)
47 0 : throw css::io::BufferSizeExceededException(OUString(),static_cast<css::uno::XWeak*>(this));
48 :
49 0 : ::osl::MutexGuard aGuard( m_aMutex );
50 :
51 0 : aData.realloc(nBytesToRead);
52 :
53 0 : sal_uInt64 nRead = 0;
54 0 : FileBase::RC eError = m_pFile->read((void*)aData.getArray(), nBytesToRead, nRead);
55 0 : if (eError != FileBase::E_None)
56 0 : throw css::io::BufferSizeExceededException(OUString(),static_cast<css::uno::XWeak*>(this));
57 :
58 : // Wenn gelesene Zeichen < MaxLength, css::uno::Sequence anpassen
59 0 : if (nRead < (sal_uInt32)nBytesToRead)
60 0 : aData.realloc( sal::static_int_cast< sal_Int32 >(nRead) );
61 :
62 0 : return sal::static_int_cast< sal_Int32 >(nRead);
63 : }
64 :
65 :
66 0 : sal_Int32 SAL_CALL OSLInputStreamWrapper::readSomeBytes(css::uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead) throw( css::io::NotConnectedException, css::io::BufferSizeExceededException, css::uno::RuntimeException, std::exception )
67 : {
68 0 : if (!m_pFile)
69 0 : throw css::io::NotConnectedException(OUString(), static_cast<css::uno::XWeak*>(this));
70 :
71 0 : if (nMaxBytesToRead < 0)
72 0 : throw css::io::BufferSizeExceededException(OUString(),static_cast<css::uno::XWeak*>(this));
73 :
74 0 : return readBytes(aData, nMaxBytesToRead);
75 : }
76 :
77 :
78 0 : void SAL_CALL OSLInputStreamWrapper::skipBytes(sal_Int32 nBytesToSkip) throw( css::io::NotConnectedException, css::io::BufferSizeExceededException, css::uno::RuntimeException, std::exception )
79 : {
80 0 : ::osl::MutexGuard aGuard( m_aMutex );
81 0 : if (!m_pFile)
82 0 : throw css::io::NotConnectedException(OUString(), static_cast<css::uno::XWeak*>(this));
83 :
84 : sal_uInt64 nCurrentPos;
85 0 : m_pFile->getPos(nCurrentPos);
86 :
87 0 : sal_uInt64 nNewPos = nCurrentPos + nBytesToSkip;
88 0 : FileBase::RC eError = m_pFile->setPos(osl_Pos_Absolut, nNewPos);
89 0 : if (eError != FileBase::E_None)
90 : {
91 0 : throw css::io::NotConnectedException(OUString(), static_cast<css::uno::XWeak*>(this));
92 0 : }
93 0 : }
94 :
95 :
96 0 : sal_Int32 SAL_CALL OSLInputStreamWrapper::available() throw( css::io::NotConnectedException, css::uno::RuntimeException, std::exception )
97 : {
98 0 : ::osl::MutexGuard aGuard( m_aMutex );
99 0 : if (!m_pFile)
100 0 : throw css::io::NotConnectedException(OUString(), static_cast<css::uno::XWeak*>(this));
101 :
102 : sal_uInt64 nPos;
103 0 : FileBase::RC eError = m_pFile->getPos(nPos);
104 0 : if (eError != FileBase::E_None)
105 0 : throw css::io::NotConnectedException(OUString(), static_cast<css::uno::XWeak*>(this));
106 :
107 0 : sal_uInt64 nDummy = 0;
108 0 : eError = m_pFile->setPos(osl_Pos_End, nDummy);
109 0 : if (eError != FileBase::E_None)
110 0 : throw css::io::NotConnectedException(OUString(),static_cast<css::uno::XWeak*>(this));
111 :
112 : sal_uInt64 nAvailable;
113 0 : eError = m_pFile->getPos(nAvailable);
114 0 : if (eError != FileBase::E_None)
115 0 : throw css::io::NotConnectedException(OUString(),static_cast<css::uno::XWeak*>(this));
116 :
117 0 : nAvailable = nAvailable - nPos;
118 0 : eError = m_pFile->setPos(osl_Pos_Absolut, nPos);
119 0 : if (eError != FileBase::E_None)
120 0 : throw css::io::NotConnectedException(OUString(),static_cast<css::uno::XWeak*>(this));
121 : return sal::static_int_cast< sal_Int32 >(
122 0 : std::max(nAvailable, sal::static_int_cast< sal_uInt64 >(SAL_MAX_INT32)));
123 : }
124 :
125 :
126 0 : void SAL_CALL OSLInputStreamWrapper::closeInput() throw( css::io::NotConnectedException, css::uno::RuntimeException, std::exception )
127 : {
128 0 : if (!m_pFile)
129 0 : throw css::io::NotConnectedException(OUString(), static_cast<css::uno::XWeak*>(this));
130 :
131 0 : m_pFile->close();
132 :
133 0 : m_pFile = NULL;
134 0 : }
135 :
136 : /*************************************************************************/
137 : // css::io::XOutputStream
138 :
139 :
140 0 : OSLOutputStreamWrapper::OSLOutputStreamWrapper(osl::File & _rFile):
141 0 : rFile(_rFile)
142 0 : {}
143 :
144 0 : OSLOutputStreamWrapper::~OSLOutputStreamWrapper() {}
145 :
146 0 : void SAL_CALL OSLOutputStreamWrapper::writeBytes(const css::uno::Sequence< sal_Int8 >& aData) throw( css::io::NotConnectedException, css::io::BufferSizeExceededException, css::uno::RuntimeException, std::exception )
147 : {
148 : sal_uInt64 nWritten;
149 0 : FileBase::RC eError = rFile.write(aData.getConstArray(),aData.getLength(), nWritten);
150 0 : if (eError != FileBase::E_None
151 0 : || nWritten != sal::static_int_cast< sal_uInt32 >(aData.getLength()))
152 : {
153 0 : throw css::io::BufferSizeExceededException(OUString(),static_cast<css::uno::XWeak*>(this));
154 : }
155 0 : }
156 :
157 :
158 0 : void SAL_CALL OSLOutputStreamWrapper::flush() throw( css::io::NotConnectedException, css::io::BufferSizeExceededException, css::uno::RuntimeException, std::exception )
159 : {
160 0 : }
161 :
162 :
163 0 : void SAL_CALL OSLOutputStreamWrapper::closeOutput() throw( css::io::NotConnectedException, css::io::BufferSizeExceededException, css::uno::RuntimeException, std::exception )
164 : {
165 0 : rFile.close();
166 0 : }
167 :
168 : } // namespace comphelper
169 :
170 :
171 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|