1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: oslfile2streamwrap.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_comphelper.hxx"
33 #include <comphelper/oslfile2streamwrap.hxx>
41 //------------------------------------------------------------------
42 OSLInputStreamWrapper::OSLInputStreamWrapper( File
& _rFile
)
44 ,m_bFileOwner(sal_False
)
48 //------------------------------------------------------------------
49 OSLInputStreamWrapper::OSLInputStreamWrapper( File
* pStream
, sal_Bool bOwner
)
51 ,m_bFileOwner( bOwner
)
55 //------------------------------------------------------------------
56 OSLInputStreamWrapper::~OSLInputStreamWrapper()
62 //------------------------------------------------------------------------------
63 sal_Int32 SAL_CALL
OSLInputStreamWrapper::readBytes(staruno::Sequence
< sal_Int8
>& aData
, sal_Int32 nBytesToRead
)
64 throw( stario::NotConnectedException
, stario::BufferSizeExceededException
, staruno::RuntimeException
)
67 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak
*>(this));
70 throw stario::BufferSizeExceededException(::rtl::OUString(),static_cast<staruno::XWeak
*>(this));
72 ::osl::MutexGuard
aGuard( m_aMutex
);
74 aData
.realloc(nBytesToRead
);
77 FileBase::RC eError
= m_pFile
->read((void*)aData
.getArray(), nBytesToRead
, nRead
);
78 if (eError
!= FileBase::E_None
)
79 throw stario::BufferSizeExceededException(::rtl::OUString(),static_cast<staruno::XWeak
*>(this));
81 // Wenn gelesene Zeichen < MaxLength, staruno::Sequence anpassen
82 if (nRead
< (sal_uInt32
)nBytesToRead
)
83 aData
.realloc( sal::static_int_cast
< sal_Int32
>(nRead
) );
85 return sal::static_int_cast
< sal_Int32
>(nRead
);
88 //------------------------------------------------------------------------------
89 sal_Int32 SAL_CALL
OSLInputStreamWrapper::readSomeBytes(staruno::Sequence
< sal_Int8
>& aData
, sal_Int32 nMaxBytesToRead
) throw( stario::NotConnectedException
, stario::BufferSizeExceededException
, staruno::RuntimeException
)
92 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak
*>(this));
94 if (nMaxBytesToRead
< 0)
95 throw stario::BufferSizeExceededException(::rtl::OUString(),static_cast<staruno::XWeak
*>(this));
105 return readBytes(aData
, nMaxBytesToRead
);
108 //------------------------------------------------------------------------------
109 void SAL_CALL
OSLInputStreamWrapper::skipBytes(sal_Int32 nBytesToSkip
) throw( stario::NotConnectedException
, stario::BufferSizeExceededException
, staruno::RuntimeException
)
111 ::osl::MutexGuard
aGuard( m_aMutex
);
113 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak
*>(this));
115 sal_uInt64 nCurrentPos
;
116 m_pFile
->getPos(nCurrentPos
);
118 sal_uInt64 nNewPos
= nCurrentPos
+ nBytesToSkip
;
119 FileBase::RC eError
= m_pFile
->setPos(osl_Pos_Absolut
, nNewPos
);
120 if (eError
!= FileBase::E_None
)
122 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak
*>(this));
126 m_pFile
->getPos(nCurrentPos
);
127 // volatile int dummy = 0; // to take a look at last changes ;-)
131 //------------------------------------------------------------------------------
132 sal_Int32 SAL_CALL
OSLInputStreamWrapper::available() throw( stario::NotConnectedException
, staruno::RuntimeException
)
134 ::osl::MutexGuard
aGuard( m_aMutex
);
136 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak
*>(this));
139 FileBase::RC eError
= m_pFile
->getPos(nPos
);
140 if (eError
!= FileBase::E_None
)
141 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak
*>(this));
143 sal_uInt64 nDummy
= 0;
144 eError
= m_pFile
->setPos(Pos_End
, nDummy
);
145 if (eError
!= FileBase::E_None
)
146 throw stario::NotConnectedException(::rtl::OUString(),static_cast<staruno::XWeak
*>(this));
148 sal_uInt64 nAvailable
;
149 eError
= m_pFile
->getPos(nAvailable
);
150 if (eError
!= FileBase::E_None
)
151 throw stario::NotConnectedException(::rtl::OUString(),static_cast<staruno::XWeak
*>(this));
153 nAvailable
= nAvailable
- nPos
;
154 eError
= m_pFile
->setPos(Pos_Absolut
, nPos
);
155 if (eError
!= FileBase::E_None
)
156 throw stario::NotConnectedException(::rtl::OUString(),static_cast<staruno::XWeak
*>(this));
157 return sal::static_int_cast
< sal_Int32
>(
158 std::max(nAvailable
, sal::static_int_cast
< sal_uInt64
>(SAL_MAX_INT32
)));
161 //------------------------------------------------------------------------------
162 void SAL_CALL
OSLInputStreamWrapper::closeInput() throw( stario::NotConnectedException
, staruno::RuntimeException
)
165 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak
*>(this));
174 /*************************************************************************/
175 // stario::XOutputStream
176 //------------------------------------------------------------------------------
177 void SAL_CALL
OSLOutputStreamWrapper::writeBytes(const staruno::Sequence
< sal_Int8
>& aData
) throw( stario::NotConnectedException
, stario::BufferSizeExceededException
, staruno::RuntimeException
)
180 FileBase::RC eError
= rFile
.write(aData
.getConstArray(),aData
.getLength(), nWritten
);
181 if (eError
!= FileBase::E_None
182 || nWritten
!= sal::static_int_cast
< sal_uInt32
>(aData
.getLength()))
184 throw stario::BufferSizeExceededException(::rtl::OUString(),static_cast<staruno::XWeak
*>(this));
188 //------------------------------------------------------------------
189 void SAL_CALL
OSLOutputStreamWrapper::flush() throw( stario::NotConnectedException
, stario::BufferSizeExceededException
, staruno::RuntimeException
)
193 //------------------------------------------------------------------
194 void SAL_CALL
OSLOutputStreamWrapper::closeOutput() throw( stario::NotConnectedException
, stario::BufferSizeExceededException
, staruno::RuntimeException
)
199 } // namespace comphelper