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