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