1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_comphelper.hxx"
30 #include <comphelper/oslfile2streamwrap.hxx>
38 //------------------------------------------------------------------
39 OSLInputStreamWrapper::OSLInputStreamWrapper( File
& _rFile
)
41 ,m_bFileOwner(sal_False
)
45 //------------------------------------------------------------------
46 OSLInputStreamWrapper::OSLInputStreamWrapper( File
* pStream
, sal_Bool bOwner
)
48 ,m_bFileOwner( bOwner
)
52 //------------------------------------------------------------------
53 OSLInputStreamWrapper::~OSLInputStreamWrapper()
59 //------------------------------------------------------------------------------
60 sal_Int32 SAL_CALL
OSLInputStreamWrapper::readBytes(staruno::Sequence
< sal_Int8
>& aData
, sal_Int32 nBytesToRead
)
61 throw( stario::NotConnectedException
, stario::BufferSizeExceededException
, staruno::RuntimeException
)
64 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak
*>(this));
67 throw stario::BufferSizeExceededException(::rtl::OUString(),static_cast<staruno::XWeak
*>(this));
69 ::osl::MutexGuard
aGuard( m_aMutex
);
71 aData
.realloc(nBytesToRead
);
74 FileBase::RC eError
= m_pFile
->read((void*)aData
.getArray(), nBytesToRead
, nRead
);
75 if (eError
!= FileBase::E_None
)
76 throw stario::BufferSizeExceededException(::rtl::OUString(),static_cast<staruno::XWeak
*>(this));
78 // Wenn gelesene Zeichen < MaxLength, staruno::Sequence anpassen
79 if (nRead
< (sal_uInt32
)nBytesToRead
)
80 aData
.realloc( sal::static_int_cast
< sal_Int32
>(nRead
) );
82 return sal::static_int_cast
< sal_Int32
>(nRead
);
85 //------------------------------------------------------------------------------
86 sal_Int32 SAL_CALL
OSLInputStreamWrapper::readSomeBytes(staruno::Sequence
< sal_Int8
>& aData
, sal_Int32 nMaxBytesToRead
) throw( stario::NotConnectedException
, stario::BufferSizeExceededException
, staruno::RuntimeException
)
89 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak
*>(this));
91 if (nMaxBytesToRead
< 0)
92 throw stario::BufferSizeExceededException(::rtl::OUString(),static_cast<staruno::XWeak
*>(this));
102 return readBytes(aData
, nMaxBytesToRead
);
105 //------------------------------------------------------------------------------
106 void SAL_CALL
OSLInputStreamWrapper::skipBytes(sal_Int32 nBytesToSkip
) throw( stario::NotConnectedException
, stario::BufferSizeExceededException
, staruno::RuntimeException
)
108 ::osl::MutexGuard
aGuard( m_aMutex
);
110 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak
*>(this));
112 sal_uInt64 nCurrentPos
;
113 m_pFile
->getPos(nCurrentPos
);
115 sal_uInt64 nNewPos
= nCurrentPos
+ nBytesToSkip
;
116 FileBase::RC eError
= m_pFile
->setPos(osl_Pos_Absolut
, nNewPos
);
117 if (eError
!= FileBase::E_None
)
119 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak
*>(this));
123 m_pFile
->getPos(nCurrentPos
);
124 // volatile int dummy = 0; // to take a look at last changes ;-)
128 //------------------------------------------------------------------------------
129 sal_Int32 SAL_CALL
OSLInputStreamWrapper::available() throw( stario::NotConnectedException
, staruno::RuntimeException
)
131 ::osl::MutexGuard
aGuard( m_aMutex
);
133 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak
*>(this));
136 FileBase::RC eError
= m_pFile
->getPos(nPos
);
137 if (eError
!= FileBase::E_None
)
138 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak
*>(this));
140 sal_uInt64 nDummy
= 0;
141 eError
= m_pFile
->setPos(Pos_End
, nDummy
);
142 if (eError
!= FileBase::E_None
)
143 throw stario::NotConnectedException(::rtl::OUString(),static_cast<staruno::XWeak
*>(this));
145 sal_uInt64 nAvailable
;
146 eError
= m_pFile
->getPos(nAvailable
);
147 if (eError
!= FileBase::E_None
)
148 throw stario::NotConnectedException(::rtl::OUString(),static_cast<staruno::XWeak
*>(this));
150 nAvailable
= nAvailable
- nPos
;
151 eError
= m_pFile
->setPos(Pos_Absolut
, nPos
);
152 if (eError
!= FileBase::E_None
)
153 throw stario::NotConnectedException(::rtl::OUString(),static_cast<staruno::XWeak
*>(this));
154 return sal::static_int_cast
< sal_Int32
>(
155 std::max(nAvailable
, sal::static_int_cast
< sal_uInt64
>(SAL_MAX_INT32
)));
158 //------------------------------------------------------------------------------
159 void SAL_CALL
OSLInputStreamWrapper::closeInput() throw( stario::NotConnectedException
, staruno::RuntimeException
)
162 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak
*>(this));
171 /*************************************************************************/
172 // stario::XOutputStream
173 //------------------------------------------------------------------------------
174 void SAL_CALL
OSLOutputStreamWrapper::writeBytes(const staruno::Sequence
< sal_Int8
>& aData
) throw( stario::NotConnectedException
, stario::BufferSizeExceededException
, staruno::RuntimeException
)
177 FileBase::RC eError
= rFile
.write(aData
.getConstArray(),aData
.getLength(), nWritten
);
178 if (eError
!= FileBase::E_None
179 || nWritten
!= sal::static_int_cast
< sal_uInt32
>(aData
.getLength()))
181 throw stario::BufferSizeExceededException(::rtl::OUString(),static_cast<staruno::XWeak
*>(this));
185 //------------------------------------------------------------------
186 void SAL_CALL
OSLOutputStreamWrapper::flush() throw( stario::NotConnectedException
, stario::BufferSizeExceededException
, staruno::RuntimeException
)
190 //------------------------------------------------------------------
191 void SAL_CALL
OSLOutputStreamWrapper::closeOutput() throw( stario::NotConnectedException
, stario::BufferSizeExceededException
, staruno::RuntimeException
)
196 } // namespace comphelper