xref: /trunk/main/package/source/package/zipapi/ByteGrabber.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_package.hxx"
26 #include <ByteGrabber.hxx>
27 #include <com/sun/star/io/XSeekable.hpp>
28 #include <com/sun/star/io/XInputStream.hpp>
29 
30 using namespace ::com::sun::star;
31 
32 /** ByteGrabber implements the >> operators on an XOutputStream. This is
33  *  potentially quite slow and may need to be optimised
34  */
35 
ByteGrabber(uno::Reference<io::XInputStream> xIstream)36 ByteGrabber::ByteGrabber(uno::Reference  < io::XInputStream > xIstream)
37 : xStream(xIstream)
38 , xSeek (xIstream, uno::UNO_QUERY )
39 , aSequence ( 4 )
40 {
41     pSequence = aSequence.getArray();
42 }
43 
~ByteGrabber()44 ByteGrabber::~ByteGrabber()
45 {
46 }
47 
setInputStream(uno::Reference<io::XInputStream> xNewStream)48 void ByteGrabber::setInputStream (uno::Reference < io::XInputStream > xNewStream)
49 {
50     ::osl::MutexGuard aGuard( m_aMutex );
51     xStream = xNewStream;
52     xSeek = uno::Reference < io::XSeekable > (xNewStream, uno::UNO_QUERY);
53 }
54 
55 // XInputStream chained
readBytes(uno::Sequence<sal_Int8> & aData,sal_Int32 nBytesToRead)56 sal_Int32 SAL_CALL ByteGrabber::readBytes( uno::Sequence< sal_Int8 >& aData,
57                                         sal_Int32 nBytesToRead )
58 {
59     ::osl::MutexGuard aGuard( m_aMutex );
60     return xStream->readBytes(aData, nBytesToRead );
61 }
62 
63 // XSeekable chained...
seek(sal_Int64 location)64 sal_Int64 SAL_CALL ByteGrabber::seek( sal_Int64 location )
65 {
66     ::osl::MutexGuard aGuard( m_aMutex );
67     if (xSeek.is() )
68     {
69         sal_Int64 nLen = xSeek->getLength();
70         if ( location < 0 || location > nLen )
71             throw lang::IllegalArgumentException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >(), 1 );
72         if (location > nLen )
73             location = nLen;
74         xSeek->seek( location );
75         return location;
76     }
77     else
78         throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
79 }
80 
getPosition()81 sal_Int64 SAL_CALL ByteGrabber::getPosition(  )
82 {
83     ::osl::MutexGuard aGuard( m_aMutex );
84     if (xSeek.is() )
85         return xSeek->getPosition();
86     else
87         throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
88 }
89 
getLength()90 sal_Int64 SAL_CALL ByteGrabber::getLength(  )
91 {
92     ::osl::MutexGuard aGuard( m_aMutex );
93     if (xSeek.is() )
94         return xSeek->getLength();
95     else
96         throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
97 }
98 
operator >>(sal_Int8 & rInt8)99 ByteGrabber& ByteGrabber::operator >> (sal_Int8& rInt8)
100 {
101     ::osl::MutexGuard aGuard( m_aMutex );
102     if (xStream->readBytes(aSequence,1) != 1)
103         rInt8 = 0;
104     else
105         rInt8 = aSequence[0] & 0xFF;
106     return *this;
107 }
108 
operator >>(sal_Int16 & rInt16)109 ByteGrabber& ByteGrabber::operator >> (sal_Int16& rInt16)
110 {
111     ::osl::MutexGuard aGuard( m_aMutex );
112     if (xStream->readBytes ( aSequence, 2) != 2)
113         rInt16 = 0;
114     else
115     {
116         pSequence = aSequence.getConstArray();
117         rInt16 = static_cast <sal_Int16>
118                ( (pSequence[0] & 0xFF)
119               | (pSequence[1] & 0xFF) << 8);
120     }
121     return *this;
122 }
123 
operator >>(sal_Int32 & rInt32)124 ByteGrabber& ByteGrabber::operator >> (sal_Int32& rInt32)
125 {
126     ::osl::MutexGuard aGuard( m_aMutex );
127 
128     if (xStream->readBytes(aSequence, 4) != 4)
129         rInt32 = 0;
130     else
131     {
132         pSequence = aSequence.getConstArray();
133         rInt32 = static_cast < sal_Int32 >
134                 ( (pSequence[0] & 0xFF)
135               | ( pSequence[1] & 0xFF ) << 8
136               | ( pSequence[2] & 0xFF ) << 16
137               | ( pSequence[3] & 0xFF ) << 24 );
138     }
139     return *this;
140 }
141 
operator >>(sal_uInt8 & rInt8)142 ByteGrabber& ByteGrabber::operator >> (sal_uInt8& rInt8)
143 {
144     ::osl::MutexGuard aGuard( m_aMutex );
145 
146     if (xStream->readBytes(aSequence,1) != 1)
147         rInt8 = 0;
148     else
149         rInt8 = static_cast < sal_uInt8 > (aSequence[0] & 0xFF );
150     return *this;
151 }
operator >>(sal_uInt16 & rInt16)152 ByteGrabber& ByteGrabber::operator >> (sal_uInt16& rInt16)
153 {
154     ::osl::MutexGuard aGuard( m_aMutex );
155 
156     if (xStream->readBytes(aSequence, 2) != 2)
157         rInt16 = 0;
158     else
159     {
160         pSequence = aSequence.getConstArray();
161         rInt16 = static_cast <sal_uInt16>
162                ( (pSequence[0] & 0xFF)
163               | (pSequence[1] & 0xFF) << 8);
164     }
165     return *this;
166 }
operator >>(sal_uInt32 & ruInt32)167 ByteGrabber& ByteGrabber::operator >> (sal_uInt32& ruInt32)
168 {
169     ::osl::MutexGuard aGuard( m_aMutex );
170 
171     if (xStream->readBytes(aSequence, 4) != 4)
172         ruInt32 = 0;
173     else
174     {
175         pSequence = aSequence.getConstArray();
176         ruInt32 = static_cast < sal_uInt32 >
177                 ( (pSequence[0] & 0xFF)
178               | ( pSequence[1] & 0xFF ) << 8
179               | ( pSequence[2] & 0xFF ) << 16
180               | ( pSequence[3] & 0xFF ) << 24 );
181     }
182     return *this;
183 }
184