update credits
[LibreOffice.git] / package / source / zipapi / ByteGrabber.cxx
blob65a8b8f66f188cd9d4027e7910cee8f4172a13b9
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <ByteGrabber.hxx>
21 #include <com/sun/star/io/XSeekable.hpp>
22 #include <com/sun/star/io/XInputStream.hpp>
24 using namespace ::com::sun::star;
26 /** ByteGrabber implements the >> operators on an XOutputStream. This is
27 * potentially quite slow and may need to be optimised
30 ByteGrabber::ByteGrabber(uno::Reference < io::XInputStream > xIstream)
31 : xStream(xIstream)
32 , xSeek (xIstream, uno::UNO_QUERY )
33 , aSequence ( 4 )
35 pSequence = aSequence.getArray();
38 ByteGrabber::~ByteGrabber()
42 void ByteGrabber::setInputStream (uno::Reference < io::XInputStream > xNewStream)
44 ::osl::MutexGuard aGuard( m_aMutex );
45 xStream = xNewStream;
46 xSeek = uno::Reference < io::XSeekable > (xNewStream, uno::UNO_QUERY);
49 // XInputStream chained
50 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)
54 ::osl::MutexGuard aGuard( m_aMutex );
55 return xStream->readBytes(aData, nBytesToRead );
58 // XSeekable chained...
59 sal_Int64 SAL_CALL ByteGrabber::seek( sal_Int64 location )
60 throw(lang::IllegalArgumentException, io::IOException, uno::RuntimeException)
62 ::osl::MutexGuard aGuard( m_aMutex );
63 if (xSeek.is() )
65 sal_Int64 nLen = xSeek->getLength();
66 if ( location < 0 || location > nLen )
67 throw lang::IllegalArgumentException(OSL_LOG_PREFIX, uno::Reference< uno::XInterface >(), 1 );
68 if (location > nLen )
69 location = nLen;
70 xSeek->seek( location );
71 return location;
73 else
74 throw io::IOException(OSL_LOG_PREFIX, uno::Reference< uno::XInterface >() );
77 sal_Int64 SAL_CALL ByteGrabber::getPosition( )
78 throw(io::IOException, uno::RuntimeException)
80 ::osl::MutexGuard aGuard( m_aMutex );
81 if (xSeek.is() )
82 return xSeek->getPosition();
83 else
84 throw io::IOException(OSL_LOG_PREFIX, uno::Reference< uno::XInterface >() );
87 sal_Int64 SAL_CALL ByteGrabber::getLength( )
88 throw(io::IOException, uno::RuntimeException)
90 ::osl::MutexGuard aGuard( m_aMutex );
91 if (xSeek.is() )
92 return xSeek->getLength();
93 else
94 throw io::IOException(OSL_LOG_PREFIX, uno::Reference< uno::XInterface >() );
97 ByteGrabber& ByteGrabber::operator >> (sal_Int8& rInt8)
99 ::osl::MutexGuard aGuard( m_aMutex );
100 if (xStream->readBytes(aSequence,1) != 1)
101 rInt8 = 0;
102 else
103 rInt8 = aSequence[0] & 0xFF;
104 return *this;
107 ByteGrabber& ByteGrabber::operator >> (sal_Int16& rInt16)
109 ::osl::MutexGuard aGuard( m_aMutex );
110 if (xStream->readBytes ( aSequence, 2) != 2)
111 rInt16 = 0;
112 else
114 pSequence = aSequence.getConstArray();
115 rInt16 = static_cast <sal_Int16>
116 ( (pSequence[0] & 0xFF)
117 | (pSequence[1] & 0xFF) << 8);
119 return *this;
122 ByteGrabber& ByteGrabber::operator >> (sal_Int32& rInt32)
124 ::osl::MutexGuard aGuard( m_aMutex );
126 if (xStream->readBytes(aSequence, 4) != 4)
127 rInt32 = 0;
128 else
130 pSequence = aSequence.getConstArray();
131 rInt32 = static_cast < sal_Int32 >
132 ( (pSequence[0] & 0xFF)
133 | ( pSequence[1] & 0xFF ) << 8
134 | ( pSequence[2] & 0xFF ) << 16
135 | ( pSequence[3] & 0xFF ) << 24 );
137 return *this;
140 ByteGrabber& ByteGrabber::operator >> (sal_uInt8& rInt8)
142 ::osl::MutexGuard aGuard( m_aMutex );
144 if (xStream->readBytes(aSequence,1) != 1)
145 rInt8 = 0;
146 else
147 rInt8 = static_cast < sal_uInt8 > (aSequence[0] & 0xFF );
148 return *this;
150 ByteGrabber& ByteGrabber::operator >> (sal_uInt16& rInt16)
152 ::osl::MutexGuard aGuard( m_aMutex );
154 if (xStream->readBytes(aSequence, 2) != 2)
155 rInt16 = 0;
156 else
158 pSequence = aSequence.getConstArray();
159 rInt16 = static_cast <sal_uInt16>
160 ( (pSequence[0] & 0xFF)
161 | (pSequence[1] & 0xFF) << 8);
163 return *this;
165 ByteGrabber& ByteGrabber::operator >> (sal_uInt32& ruInt32)
167 ::osl::MutexGuard aGuard( m_aMutex );
169 if (xStream->readBytes(aSequence, 4) != 4)
170 ruInt32 = 0;
171 else
173 pSequence = aSequence.getConstArray();
174 ruInt32 = static_cast < sal_uInt32 >
175 ( (pSequence[0] & 0xFF)
176 | ( pSequence[1] & 0xFF ) << 8
177 | ( pSequence[2] & 0xFF ) << 16
178 | ( pSequence[3] & 0xFF ) << 24 );
180 return *this;
183 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */