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 <sal/log.hxx>
22 #include <com/sun/star/io/XSeekable.hpp>
23 #include <com/sun/star/io/XInputStream.hpp>
24
25 using namespace ::com::sun::star;
26
27 #if OSL_DEBUG_LEVEL > 0
28 #define THROW_WHERE SAL_WHERE
29 #else
30 #define THROW_WHERE ""
31 #endif
32
33 /** ByteGrabber implements the >> operators on an XOutputStream. This is
34 * potentially quite slow and may need to be optimised
35 */
36
ByteGrabber(uno::Reference<io::XInputStream> const & xIstream)37 ByteGrabber::ByteGrabber(uno::Reference < io::XInputStream > const & xIstream)
38 : xStream(xIstream)
39 , xSeek (xIstream, uno::UNO_QUERY )
40 , aSequence ( 4 )
41 {
42 pSequence = aSequence.getArray();
43 }
44
~ByteGrabber()45 ByteGrabber::~ByteGrabber()
46 {
47 }
48
setInputStream(const uno::Reference<io::XInputStream> & xNewStream)49 void ByteGrabber::setInputStream (const uno::Reference < io::XInputStream >& xNewStream)
50 {
51 ::osl::MutexGuard aGuard( m_aMutex );
52 xStream = xNewStream;
53 xSeek.set(xNewStream, uno::UNO_QUERY);
54 }
55
56 // XInputStream chained
readBytes(uno::Sequence<sal_Int8> & aData,sal_Int32 nBytesToRead)57 sal_Int32 ByteGrabber::readBytes( uno::Sequence< sal_Int8 >& aData,
58 sal_Int32 nBytesToRead )
59 {
60 ::osl::MutexGuard aGuard( m_aMutex );
61 return xStream->readBytes(aData, nBytesToRead );
62 }
63
64 // XSeekable chained...
seek(sal_Int64 location)65 void ByteGrabber::seek( sal_Int64 location )
66 {
67 ::osl::MutexGuard aGuard( m_aMutex );
68 if (!xSeek.is() )
69 throw io::IOException(THROW_WHERE );
70
71 sal_Int64 nLen = xSeek->getLength();
72 if ( location < 0 || location > nLen )
73 throw lang::IllegalArgumentException(THROW_WHERE, uno::Reference< uno::XInterface >(), 1 );
74 xSeek->seek( location );
75 }
76
getPosition()77 sal_Int64 ByteGrabber::getPosition( )
78 {
79 ::osl::MutexGuard aGuard( m_aMutex );
80 if (!xSeek.is() )
81 throw io::IOException(THROW_WHERE );
82
83 return xSeek->getPosition();
84 }
85
getLength()86 sal_Int64 ByteGrabber::getLength( )
87 {
88 ::osl::MutexGuard aGuard( m_aMutex );
89 if (!xSeek.is() )
90 throw io::IOException(THROW_WHERE );
91
92 return xSeek->getLength();
93 }
94
ReadUInt16()95 sal_uInt16 ByteGrabber::ReadUInt16()
96 {
97 ::osl::MutexGuard aGuard( m_aMutex );
98
99 if (xStream->readBytes(aSequence, 2) != 2)
100 return 0;
101
102 pSequence = aSequence.getConstArray();
103 return static_cast <sal_uInt16>
104 ( (pSequence[0] & 0xFF)
105 | (pSequence[1] & 0xFF) << 8);
106 }
107
ReadUInt32()108 sal_uInt32 ByteGrabber::ReadUInt32()
109 {
110 ::osl::MutexGuard aGuard( m_aMutex );
111
112 if (xStream->readBytes(aSequence, 4) != 4)
113 return 0;
114
115 pSequence = aSequence.getConstArray();
116 return static_cast < sal_uInt32 >
117 ( (pSequence[0] & 0xFF)
118 | ( pSequence[1] & 0xFF ) << 8
119 | ( pSequence[2] & 0xFF ) << 16
120 | ( pSequence[3] & 0xFF ) << 24 );
121 }
122
123 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
124