Bump version to 5.0-14
[LibreOffice.git] / package / source / zipapi / ByteGrabber.cxx
blob65643bee865d72eb3bcedf62aa9eb9ff39f9df7d
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 #if OSL_DEBUG_LEVEL > 0
27 #define THROW_WHERE SAL_WHERE
28 #else
29 #define THROW_WHERE ""
30 #endif
32 /** ByteGrabber implements the >> operators on an XOutputStream. This is
33 * potentially quite slow and may need to be optimised
36 ByteGrabber::ByteGrabber(uno::Reference < io::XInputStream > xIstream)
37 : xStream(xIstream)
38 , xSeek (xIstream, uno::UNO_QUERY )
39 , aSequence ( 4 )
41 pSequence = aSequence.getArray();
44 ByteGrabber::~ByteGrabber()
48 void ByteGrabber::setInputStream (uno::Reference < io::XInputStream > xNewStream)
50 ::osl::MutexGuard aGuard( m_aMutex );
51 xStream = xNewStream;
52 xSeek = uno::Reference < io::XSeekable > (xNewStream, uno::UNO_QUERY);
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)
60 ::osl::MutexGuard aGuard( m_aMutex );
61 return xStream->readBytes(aData, nBytesToRead );
64 // XSeekable chained...
65 sal_Int64 SAL_CALL ByteGrabber::seek( sal_Int64 location )
66 throw(lang::IllegalArgumentException, io::IOException, uno::RuntimeException)
68 ::osl::MutexGuard aGuard( m_aMutex );
69 if (xSeek.is() )
71 sal_Int64 nLen = xSeek->getLength();
72 if ( location < 0 || location > nLen )
73 throw lang::IllegalArgumentException(THROW_WHERE, uno::Reference< uno::XInterface >(), 1 );
74 if (location > nLen )
75 location = nLen;
76 xSeek->seek( location );
77 return location;
79 else
80 throw io::IOException(THROW_WHERE );
83 sal_Int64 SAL_CALL ByteGrabber::getPosition( )
84 throw(io::IOException, uno::RuntimeException)
86 ::osl::MutexGuard aGuard( m_aMutex );
87 if (xSeek.is() )
88 return xSeek->getPosition();
89 else
90 throw io::IOException(THROW_WHERE );
93 sal_Int64 SAL_CALL ByteGrabber::getLength( )
94 throw(io::IOException, uno::RuntimeException)
96 ::osl::MutexGuard aGuard( m_aMutex );
97 if (xSeek.is() )
98 return xSeek->getLength();
99 else
100 throw io::IOException(THROW_WHERE );
103 sal_uInt16 ByteGrabber::ReadUInt16()
105 ::osl::MutexGuard aGuard( m_aMutex );
107 if (xStream->readBytes(aSequence, 2) != 2)
108 return 0;
110 pSequence = aSequence.getConstArray();
111 return static_cast <sal_uInt16>
112 ( (pSequence[0] & 0xFF)
113 | (pSequence[1] & 0xFF) << 8);
116 sal_uInt32 ByteGrabber::ReadUInt32()
118 ::osl::MutexGuard aGuard( m_aMutex );
120 if (xStream->readBytes(aSequence, 4) != 4)
121 return 0;
123 pSequence = aSequence.getConstArray();
124 return static_cast < sal_uInt32 >
125 ( (pSequence[0] & 0xFF)
126 | ( pSequence[1] & 0xFF ) << 8
127 | ( pSequence[2] & 0xFF ) << 16
128 | ( pSequence[3] & 0xFF ) << 24 );
131 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */