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 <unotools/streamhelper.hxx>
21 :
22 : namespace utl
23 : {
24 :
25 : //------------------------------------------------------------------------------
26 0 : void SAL_CALL OInputStreamHelper::acquire() throw ()
27 : {
28 0 : InputStreamHelper_Base::acquire();
29 0 : }
30 :
31 : //------------------------------------------------------------------------------
32 0 : void SAL_CALL OInputStreamHelper::release() throw ()
33 : {
34 0 : InputStreamHelper_Base::release();
35 0 : }
36 :
37 : //------------------------------------------------------------------------------
38 0 : sal_Int32 SAL_CALL OInputStreamHelper::readBytes(staruno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead)
39 : throw(stario::NotConnectedException, stario::BufferSizeExceededException, stario::IOException, staruno::RuntimeException)
40 : {
41 0 : if (!m_xLockBytes.Is())
42 0 : throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
43 :
44 0 : if (nBytesToRead < 0)
45 0 : throw stario::BufferSizeExceededException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
46 :
47 0 : ::osl::MutexGuard aGuard( m_aMutex );
48 0 : aData.realloc(nBytesToRead);
49 :
50 : sal_Size nRead;
51 0 : ErrCode nError = m_xLockBytes->ReadAt(m_nActPos, (void*)aData.getArray(), nBytesToRead, &nRead);
52 : // FIXME nRead could be truncated on 64-bit arches
53 0 : m_nActPos += (sal_uInt32)nRead;
54 :
55 0 : if (nError != ERRCODE_NONE)
56 0 : throw stario::IOException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
57 :
58 : // adjust sequence if data read is lower than the desired data
59 0 : if (nRead < (sal_uInt32)nBytesToRead)
60 0 : aData.realloc( nRead );
61 :
62 0 : return nRead;
63 : }
64 :
65 0 : void SAL_CALL OInputStreamHelper::seek( sal_Int64 location ) throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException)
66 : {
67 0 : ::osl::MutexGuard aGuard( m_aMutex );
68 : // cast is truncating, but position would be truncated as soon as
69 : // put into SvLockBytes anyway
70 0 : m_nActPos = sal::static_int_cast<sal_uInt32>(location);
71 0 : }
72 :
73 0 : sal_Int64 SAL_CALL OInputStreamHelper::getPosition( ) throw(::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException)
74 : {
75 0 : return m_nActPos;
76 : }
77 :
78 0 : sal_Int64 SAL_CALL OInputStreamHelper::getLength( ) throw(::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException)
79 : {
80 0 : if (!m_xLockBytes.Is())
81 0 : return 0;
82 :
83 0 : ::osl::MutexGuard aGuard( m_aMutex );
84 0 : SvLockBytesStat aStat;
85 0 : m_xLockBytes->Stat( &aStat, SVSTATFLAG_DEFAULT );
86 0 : return aStat.nSize;
87 : }
88 :
89 : //------------------------------------------------------------------------------
90 0 : sal_Int32 SAL_CALL OInputStreamHelper::readSomeBytes(staruno::Sequence< sal_Int8 >& aData,
91 : sal_Int32 nMaxBytesToRead)
92 : throw (stario::NotConnectedException, stario::BufferSizeExceededException, stario::IOException, staruno::RuntimeException)
93 : {
94 : // read all data desired
95 0 : return readBytes(aData, nMaxBytesToRead);
96 : }
97 :
98 : //------------------------------------------------------------------------------
99 0 : void SAL_CALL OInputStreamHelper::skipBytes(sal_Int32 nBytesToSkip)
100 : throw (stario::NotConnectedException, stario::BufferSizeExceededException, stario::IOException, staruno::RuntimeException)
101 : {
102 0 : ::osl::MutexGuard aGuard( m_aMutex );
103 0 : if (!m_xLockBytes.Is())
104 0 : throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
105 :
106 0 : if (nBytesToSkip < 0)
107 0 : throw stario::BufferSizeExceededException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
108 :
109 0 : m_nActPos += nBytesToSkip;
110 0 : }
111 :
112 : //------------------------------------------------------------------------------
113 0 : sal_Int32 SAL_CALL OInputStreamHelper::available()
114 : throw (stario::NotConnectedException, stario::IOException, staruno::RuntimeException)
115 : {
116 0 : ::osl::MutexGuard aGuard( m_aMutex );
117 0 : if (!m_xLockBytes.Is())
118 0 : throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
119 :
120 0 : return m_nAvailable;
121 : }
122 :
123 : //------------------------------------------------------------------------------
124 0 : void SAL_CALL OInputStreamHelper::closeInput()
125 : throw (stario::NotConnectedException, stario::IOException, staruno::RuntimeException)
126 : {
127 0 : ::osl::MutexGuard aGuard( m_aMutex );
128 0 : if (!m_xLockBytes.Is())
129 0 : throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
130 :
131 0 : m_xLockBytes = NULL;
132 0 : }
133 :
134 : } // namespace utl
135 :
136 :
137 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|