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 0 : ByteGrabber::ByteGrabber(uno::Reference < io::XInputStream > xIstream)
37 : : xStream(xIstream)
38 : , xSeek (xIstream, uno::UNO_QUERY )
39 0 : , aSequence ( 4 )
40 : {
41 0 : pSequence = aSequence.getArray();
42 0 : }
43 :
44 0 : ByteGrabber::~ByteGrabber()
45 : {
46 0 : }
47 :
48 0 : void ByteGrabber::setInputStream (uno::Reference < io::XInputStream > xNewStream)
49 : {
50 0 : ::osl::MutexGuard aGuard( m_aMutex );
51 0 : xStream = xNewStream;
52 0 : xSeek = uno::Reference < io::XSeekable > (xNewStream, uno::UNO_QUERY);
53 0 : }
54 :
55 : // XInputStream chained
56 0 : 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 0 : ::osl::MutexGuard aGuard( m_aMutex );
61 0 : return xStream->readBytes(aData, nBytesToRead );
62 : }
63 :
64 : // XSeekable chained...
65 0 : sal_Int64 SAL_CALL ByteGrabber::seek( sal_Int64 location )
66 : throw(lang::IllegalArgumentException, io::IOException, uno::RuntimeException)
67 : {
68 0 : ::osl::MutexGuard aGuard( m_aMutex );
69 0 : if (xSeek.is() )
70 : {
71 0 : sal_Int64 nLen = xSeek->getLength();
72 0 : if ( location < 0 || location > nLen )
73 0 : throw lang::IllegalArgumentException(THROW_WHERE, uno::Reference< uno::XInterface >(), 1 );
74 0 : if (location > nLen )
75 0 : location = nLen;
76 0 : xSeek->seek( location );
77 0 : return location;
78 : }
79 : else
80 0 : throw io::IOException(THROW_WHERE, uno::Reference< uno::XInterface >() );
81 : }
82 :
83 0 : sal_Int64 SAL_CALL ByteGrabber::getPosition( )
84 : throw(io::IOException, uno::RuntimeException)
85 : {
86 0 : ::osl::MutexGuard aGuard( m_aMutex );
87 0 : if (xSeek.is() )
88 0 : return xSeek->getPosition();
89 : else
90 0 : throw io::IOException(THROW_WHERE, uno::Reference< uno::XInterface >() );
91 : }
92 :
93 0 : sal_Int64 SAL_CALL ByteGrabber::getLength( )
94 : throw(io::IOException, uno::RuntimeException)
95 : {
96 0 : ::osl::MutexGuard aGuard( m_aMutex );
97 0 : if (xSeek.is() )
98 0 : return xSeek->getLength();
99 : else
100 0 : throw io::IOException(THROW_WHERE, uno::Reference< uno::XInterface >() );
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 0 : ByteGrabber& ByteGrabber::operator >> (sal_Int16& rInt16)
114 : {
115 0 : ::osl::MutexGuard aGuard( m_aMutex );
116 0 : if (xStream->readBytes ( aSequence, 2) != 2)
117 0 : rInt16 = 0;
118 : else
119 : {
120 0 : pSequence = aSequence.getConstArray();
121 : rInt16 = static_cast <sal_Int16>
122 0 : ( (pSequence[0] & 0xFF)
123 0 : | (pSequence[1] & 0xFF) << 8);
124 : }
125 0 : return *this;
126 : }
127 :
128 0 : ByteGrabber& ByteGrabber::operator >> (sal_Int32& rInt32)
129 : {
130 0 : ::osl::MutexGuard aGuard( m_aMutex );
131 :
132 0 : if (xStream->readBytes(aSequence, 4) != 4)
133 0 : rInt32 = 0;
134 : else
135 : {
136 0 : pSequence = aSequence.getConstArray();
137 : rInt32 = static_cast < sal_Int32 >
138 0 : ( (pSequence[0] & 0xFF)
139 0 : | ( pSequence[1] & 0xFF ) << 8
140 0 : | ( pSequence[2] & 0xFF ) << 16
141 0 : | ( pSequence[3] & 0xFF ) << 24 );
142 : }
143 0 : 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 0 : ByteGrabber& ByteGrabber::operator >> (sal_uInt16& rInt16)
157 : {
158 0 : ::osl::MutexGuard aGuard( m_aMutex );
159 :
160 0 : if (xStream->readBytes(aSequence, 2) != 2)
161 0 : rInt16 = 0;
162 : else
163 : {
164 0 : pSequence = aSequence.getConstArray();
165 : rInt16 = static_cast <sal_uInt16>
166 0 : ( (pSequence[0] & 0xFF)
167 0 : | (pSequence[1] & 0xFF) << 8);
168 : }
169 0 : 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: */
|