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 1513 : ByteGrabber::ByteGrabber(uno::Reference < io::XInputStream > xIstream)
31 : : xStream(xIstream)
32 : , xSeek (xIstream, uno::UNO_QUERY )
33 1513 : , aSequence ( 4 )
34 : {
35 1513 : pSequence = aSequence.getArray();
36 1513 : }
37 :
38 1483 : ByteGrabber::~ByteGrabber()
39 : {
40 1483 : }
41 :
42 24 : void ByteGrabber::setInputStream (uno::Reference < io::XInputStream > xNewStream)
43 : {
44 24 : ::osl::MutexGuard aGuard( m_aMutex );
45 24 : xStream = xNewStream;
46 24 : xSeek = uno::Reference < io::XSeekable > (xNewStream, uno::UNO_QUERY);
47 24 : }
48 :
49 : // XInputStream chained
50 10454 : 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 10454 : ::osl::MutexGuard aGuard( m_aMutex );
55 10454 : return xStream->readBytes(aData, nBytesToRead );
56 : }
57 :
58 : // XSeekable chained...
59 11762 : sal_Int64 SAL_CALL ByteGrabber::seek( sal_Int64 location )
60 : throw(lang::IllegalArgumentException, io::IOException, uno::RuntimeException)
61 : {
62 11762 : ::osl::MutexGuard aGuard( m_aMutex );
63 11762 : if (xSeek.is() )
64 : {
65 11762 : sal_Int64 nLen = xSeek->getLength();
66 11762 : if ( location < 0 || location > nLen )
67 0 : throw lang::IllegalArgumentException(OSL_LOG_PREFIX, uno::Reference< uno::XInterface >(), 1 );
68 11762 : if (location > nLen )
69 0 : location = nLen;
70 11762 : xSeek->seek( location );
71 23524 : return location;
72 : }
73 : else
74 11762 : throw io::IOException(OSL_LOG_PREFIX, uno::Reference< uno::XInterface >() );
75 : }
76 :
77 7838 : sal_Int64 SAL_CALL ByteGrabber::getPosition( )
78 : throw(io::IOException, uno::RuntimeException)
79 : {
80 7838 : ::osl::MutexGuard aGuard( m_aMutex );
81 7838 : if (xSeek.is() )
82 15676 : return xSeek->getPosition();
83 : else
84 7838 : throw io::IOException(OSL_LOG_PREFIX, uno::Reference< uno::XInterface >() );
85 : }
86 :
87 1308 : sal_Int64 SAL_CALL ByteGrabber::getLength( )
88 : throw(io::IOException, uno::RuntimeException)
89 : {
90 1308 : ::osl::MutexGuard aGuard( m_aMutex );
91 1308 : if (xSeek.is() )
92 2616 : return xSeek->getLength();
93 : else
94 1308 : 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 39190 : ByteGrabber& ByteGrabber::operator >> (sal_Int16& rInt16)
108 : {
109 39190 : ::osl::MutexGuard aGuard( m_aMutex );
110 39190 : if (xStream->readBytes ( aSequence, 2) != 2)
111 0 : rInt16 = 0;
112 : else
113 : {
114 39190 : pSequence = aSequence.getConstArray();
115 : rInt16 = static_cast <sal_Int16>
116 39190 : ( (pSequence[0] & 0xFF)
117 39190 : | (pSequence[1] & 0xFF) << 8);
118 : }
119 39190 : return *this;
120 : }
121 :
122 41806 : ByteGrabber& ByteGrabber::operator >> (sal_Int32& rInt32)
123 : {
124 41806 : ::osl::MutexGuard aGuard( m_aMutex );
125 :
126 41806 : if (xStream->readBytes(aSequence, 4) != 4)
127 0 : rInt32 = 0;
128 : else
129 : {
130 41806 : pSequence = aSequence.getConstArray();
131 : rInt32 = static_cast < sal_Int32 >
132 41806 : ( (pSequence[0] & 0xFF)
133 41806 : | ( pSequence[1] & 0xFF ) << 8
134 41806 : | ( pSequence[2] & 0xFF ) << 16
135 41806 : | ( pSequence[3] & 0xFF ) << 24 );
136 : }
137 41806 : 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 1308 : ByteGrabber& ByteGrabber::operator >> (sal_uInt16& rInt16)
151 : {
152 1308 : ::osl::MutexGuard aGuard( m_aMutex );
153 :
154 1308 : if (xStream->readBytes(aSequence, 2) != 2)
155 0 : rInt16 = 0;
156 : else
157 : {
158 1308 : pSequence = aSequence.getConstArray();
159 : rInt16 = static_cast <sal_uInt16>
160 1308 : ( (pSequence[0] & 0xFF)
161 1308 : | (pSequence[1] & 0xFF) << 8);
162 : }
163 1308 : 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: */
|