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 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 44 ByteGrabber::~ByteGrabber() 45 { 46 } 47 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 56 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 ::osl::MutexGuard aGuard( m_aMutex ); 61 return xStream->readBytes(aData, nBytesToRead ); 62 } 63 64 // XSeekable chained... 65 sal_Int64 SAL_CALL ByteGrabber::seek( sal_Int64 location ) 66 throw(lang::IllegalArgumentException, io::IOException, uno::RuntimeException) 67 { 68 ::osl::MutexGuard aGuard( m_aMutex ); 69 if (xSeek.is() ) 70 { 71 sal_Int64 nLen = xSeek->getLength(); 72 if ( location < 0 || location > nLen ) 73 throw lang::IllegalArgumentException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >(), 1 ); 74 if (location > nLen ) 75 location = nLen; 76 xSeek->seek( location ); 77 return location; 78 } 79 else 80 throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); 81 } 82 83 sal_Int64 SAL_CALL ByteGrabber::getPosition( ) 84 throw(io::IOException, uno::RuntimeException) 85 { 86 ::osl::MutexGuard aGuard( m_aMutex ); 87 if (xSeek.is() ) 88 return xSeek->getPosition(); 89 else 90 throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); 91 } 92 93 sal_Int64 SAL_CALL ByteGrabber::getLength( ) 94 throw(io::IOException, uno::RuntimeException) 95 { 96 ::osl::MutexGuard aGuard( m_aMutex ); 97 if (xSeek.is() ) 98 return xSeek->getLength(); 99 else 100 throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); 101 } 102 103 ByteGrabber& ByteGrabber::operator >> (sal_Int8& rInt8) 104 { 105 ::osl::MutexGuard aGuard( m_aMutex ); 106 if (xStream->readBytes(aSequence,1) != 1) 107 rInt8 = 0; 108 else 109 rInt8 = aSequence[0] & 0xFF; 110 return *this; 111 } 112 113 ByteGrabber& ByteGrabber::operator >> (sal_Int16& rInt16) 114 { 115 ::osl::MutexGuard aGuard( m_aMutex ); 116 if (xStream->readBytes ( aSequence, 2) != 2) 117 rInt16 = 0; 118 else 119 { 120 pSequence = aSequence.getConstArray(); 121 rInt16 = static_cast <sal_Int16> 122 ( (pSequence[0] & 0xFF) 123 | (pSequence[1] & 0xFF) << 8); 124 } 125 return *this; 126 } 127 128 ByteGrabber& ByteGrabber::operator >> (sal_Int32& rInt32) 129 { 130 ::osl::MutexGuard aGuard( m_aMutex ); 131 132 if (xStream->readBytes(aSequence, 4) != 4) 133 rInt32 = 0; 134 else 135 { 136 pSequence = aSequence.getConstArray(); 137 rInt32 = static_cast < sal_Int32 > 138 ( (pSequence[0] & 0xFF) 139 | ( pSequence[1] & 0xFF ) << 8 140 | ( pSequence[2] & 0xFF ) << 16 141 | ( pSequence[3] & 0xFF ) << 24 ); 142 } 143 return *this; 144 } 145 146 ByteGrabber& ByteGrabber::operator >> (sal_uInt8& rInt8) 147 { 148 ::osl::MutexGuard aGuard( m_aMutex ); 149 150 if (xStream->readBytes(aSequence,1) != 1) 151 rInt8 = 0; 152 else 153 rInt8 = static_cast < sal_uInt8 > (aSequence[0] & 0xFF ); 154 return *this; 155 } 156 ByteGrabber& ByteGrabber::operator >> (sal_uInt16& rInt16) 157 { 158 ::osl::MutexGuard aGuard( m_aMutex ); 159 160 if (xStream->readBytes(aSequence, 2) != 2) 161 rInt16 = 0; 162 else 163 { 164 pSequence = aSequence.getConstArray(); 165 rInt16 = static_cast <sal_uInt16> 166 ( (pSequence[0] & 0xFF) 167 | (pSequence[1] & 0xFF) << 8); 168 } 169 return *this; 170 } 171 ByteGrabber& ByteGrabber::operator >> (sal_uInt32& ruInt32) 172 { 173 ::osl::MutexGuard aGuard( m_aMutex ); 174 175 if (xStream->readBytes(aSequence, 4) != 4) 176 ruInt32 = 0; 177 else 178 { 179 pSequence = aSequence.getConstArray(); 180 ruInt32 = static_cast < sal_uInt32 > 181 ( (pSequence[0] & 0xFF) 182 | ( pSequence[1] & 0xFF ) << 8 183 | ( pSequence[2] & 0xFF ) << 16 184 | ( pSequence[3] & 0xFF ) << 24 ); 185 } 186 return *this; 187 } 188