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 <ByteGrabber.hxx>
21 : #include <com/sun/star/io/XSeekable.hpp>
22 : #include <com/sun/star/io/XInputStream.hpp>
23 :
24 : using namespace ::com::sun::star;
25 :
26 : #if OSL_DEBUG_LEVEL > 0
27 : #define THROW_WHERE SAL_WHERE
28 : #else
29 : #define THROW_WHERE ""
30 : #endif
31 :
32 : /** ByteGrabber implements the >> operators on an XOutputStream. This is
33 : * potentially quite slow and may need to be optimised
34 : */
35 :
36 21906 : ByteGrabber::ByteGrabber(uno::Reference < io::XInputStream > xIstream)
37 : : xStream(xIstream)
38 : , xSeek (xIstream, uno::UNO_QUERY )
39 21906 : , aSequence ( 4 )
40 : {
41 21906 : pSequence = aSequence.getArray();
42 21906 : }
43 :
44 21898 : ByteGrabber::~ByteGrabber()
45 : {
46 21898 : }
47 :
48 58 : void ByteGrabber::setInputStream (uno::Reference < io::XInputStream > xNewStream)
49 : {
50 58 : ::osl::MutexGuard aGuard( m_aMutex );
51 58 : xStream = xNewStream;
52 58 : xSeek = uno::Reference < io::XSeekable > (xNewStream, uno::UNO_QUERY);
53 58 : }
54 :
55 : // XInputStream chained
56 130275 : sal_Int32 SAL_CALL ByteGrabber::readBytes( uno::Sequence< sal_Int8 >& aData,
57 : sal_Int32 nBytesToRead )
58 : throw(io::NotConnectedException, io::BufferSizeExceededException, io::IOException, uno::RuntimeException)
59 : {
60 130275 : ::osl::MutexGuard aGuard( m_aMutex );
61 130275 : return xStream->readBytes(aData, nBytesToRead );
62 : }
63 :
64 : // XSeekable chained...
65 150543 : sal_Int64 SAL_CALL ByteGrabber::seek( sal_Int64 location )
66 : throw(lang::IllegalArgumentException, io::IOException, uno::RuntimeException)
67 : {
68 150543 : ::osl::MutexGuard aGuard( m_aMutex );
69 150543 : if (xSeek.is() )
70 : {
71 150543 : sal_Int64 nLen = xSeek->getLength();
72 150543 : if ( location < 0 || location > nLen )
73 0 : throw lang::IllegalArgumentException(THROW_WHERE, uno::Reference< uno::XInterface >(), 1 );
74 150543 : if (location > nLen )
75 0 : location = nLen;
76 150543 : xSeek->seek( location );
77 301086 : return location;
78 : }
79 : else
80 150543 : throw io::IOException(THROW_WHERE );
81 : }
82 :
83 89379 : sal_Int64 SAL_CALL ByteGrabber::getPosition( )
84 : throw(io::IOException, uno::RuntimeException)
85 : {
86 89379 : ::osl::MutexGuard aGuard( m_aMutex );
87 89379 : if (xSeek.is() )
88 178758 : return xSeek->getPosition();
89 : else
90 89379 : throw io::IOException(THROW_WHERE );
91 : }
92 :
93 20630 : sal_Int64 SAL_CALL ByteGrabber::getLength( )
94 : throw(io::IOException, uno::RuntimeException)
95 : {
96 20630 : ::osl::MutexGuard aGuard( m_aMutex );
97 20630 : if (xSeek.is() )
98 41260 : return xSeek->getLength();
99 : else
100 20630 : throw io::IOException(THROW_WHERE );
101 : }
102 :
103 0 : ByteGrabber& ByteGrabber::operator >> (sal_Int8& rInt8)
104 : {
105 0 : ::osl::MutexGuard aGuard( m_aMutex );
106 0 : if (xStream->readBytes(aSequence,1) != 1)
107 0 : rInt8 = 0;
108 : else
109 0 : rInt8 = aSequence[0] & 0xFF;
110 0 : return *this;
111 : }
112 :
113 446895 : ByteGrabber& ByteGrabber::operator >> (sal_Int16& rInt16)
114 : {
115 446895 : ::osl::MutexGuard aGuard( m_aMutex );
116 446895 : if (xStream->readBytes ( aSequence, 2) != 2)
117 0 : rInt16 = 0;
118 : else
119 : {
120 446895 : pSequence = aSequence.getConstArray();
121 : rInt16 = static_cast <sal_Int16>
122 446895 : ( (pSequence[0] & 0xFF)
123 446895 : | (pSequence[1] & 0xFF) << 8);
124 : }
125 446895 : return *this;
126 : }
127 :
128 487431 : ByteGrabber& ByteGrabber::operator >> (sal_Int32& rInt32)
129 : {
130 487431 : ::osl::MutexGuard aGuard( m_aMutex );
131 :
132 487431 : if (xStream->readBytes(aSequence, 4) != 4)
133 0 : rInt32 = 0;
134 : else
135 : {
136 487431 : pSequence = aSequence.getConstArray();
137 : rInt32 = static_cast < sal_Int32 >
138 487431 : ( (pSequence[0] & 0xFF)
139 487431 : | ( pSequence[1] & 0xFF ) << 8
140 487431 : | ( pSequence[2] & 0xFF ) << 16
141 487431 : | ( pSequence[3] & 0xFF ) << 24 );
142 : }
143 487431 : return *this;
144 : }
145 :
146 0 : ByteGrabber& ByteGrabber::operator >> (sal_uInt8& rInt8)
147 : {
148 0 : ::osl::MutexGuard aGuard( m_aMutex );
149 :
150 0 : if (xStream->readBytes(aSequence,1) != 1)
151 0 : rInt8 = 0;
152 : else
153 0 : rInt8 = static_cast < sal_uInt8 > (aSequence[0] & 0xFF );
154 0 : return *this;
155 : }
156 20268 : ByteGrabber& ByteGrabber::operator >> (sal_uInt16& rInt16)
157 : {
158 20268 : ::osl::MutexGuard aGuard( m_aMutex );
159 :
160 20268 : if (xStream->readBytes(aSequence, 2) != 2)
161 0 : rInt16 = 0;
162 : else
163 : {
164 20268 : pSequence = aSequence.getConstArray();
165 : rInt16 = static_cast <sal_uInt16>
166 20268 : ( (pSequence[0] & 0xFF)
167 20268 : | (pSequence[1] & 0xFF) << 8);
168 : }
169 20268 : return *this;
170 : }
171 0 : ByteGrabber& ByteGrabber::operator >> (sal_uInt32& ruInt32)
172 : {
173 0 : ::osl::MutexGuard aGuard( m_aMutex );
174 :
175 0 : if (xStream->readBytes(aSequence, 4) != 4)
176 0 : ruInt32 = 0;
177 : else
178 : {
179 0 : pSequence = aSequence.getConstArray();
180 : ruInt32 = static_cast < sal_uInt32 >
181 0 : ( (pSequence[0] & 0xFF)
182 0 : | ( pSequence[1] & 0xFF ) << 8
183 0 : | ( pSequence[2] & 0xFF ) << 16
184 0 : | ( pSequence[3] & 0xFF ) << 24 );
185 : }
186 0 : return *this;
187 : }
188 :
189 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|