1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
)
32 , xSeek (xIstream
, uno::UNO_QUERY
)
35 pSequence
= aSequence
.getArray();
38 ByteGrabber::~ByteGrabber()
42 void ByteGrabber::setInputStream (uno::Reference
< io::XInputStream
> xNewStream
)
44 ::osl::MutexGuard
aGuard( m_aMutex
);
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
);
65 sal_Int64 nLen
= xSeek
->getLength();
66 if ( location
< 0 || location
> nLen
)
67 throw lang::IllegalArgumentException(OSL_LOG_PREFIX
, uno::Reference
< uno::XInterface
>(), 1 );
70 xSeek
->seek( location
);
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
);
82 return xSeek
->getPosition();
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
);
92 return xSeek
->getLength();
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)
103 rInt8
= aSequence
[0] & 0xFF;
107 ByteGrabber
& ByteGrabber::operator >> (sal_Int16
& rInt16
)
109 ::osl::MutexGuard
aGuard( m_aMutex
);
110 if (xStream
->readBytes ( aSequence
, 2) != 2)
114 pSequence
= aSequence
.getConstArray();
115 rInt16
= static_cast <sal_Int16
>
116 ( (pSequence
[0] & 0xFF)
117 | (pSequence
[1] & 0xFF) << 8);
122 ByteGrabber
& ByteGrabber::operator >> (sal_Int32
& rInt32
)
124 ::osl::MutexGuard
aGuard( m_aMutex
);
126 if (xStream
->readBytes(aSequence
, 4) != 4)
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 );
140 ByteGrabber
& ByteGrabber::operator >> (sal_uInt8
& rInt8
)
142 ::osl::MutexGuard
aGuard( m_aMutex
);
144 if (xStream
->readBytes(aSequence
,1) != 1)
147 rInt8
= static_cast < sal_uInt8
> (aSequence
[0] & 0xFF );
150 ByteGrabber
& ByteGrabber::operator >> (sal_uInt16
& rInt16
)
152 ::osl::MutexGuard
aGuard( m_aMutex
);
154 if (xStream
->readBytes(aSequence
, 2) != 2)
158 pSequence
= aSequence
.getConstArray();
159 rInt16
= static_cast <sal_uInt16
>
160 ( (pSequence
[0] & 0xFF)
161 | (pSequence
[1] & 0xFF) << 8);
165 ByteGrabber
& ByteGrabber::operator >> (sal_uInt32
& ruInt32
)
167 ::osl::MutexGuard
aGuard( m_aMutex
);
169 if (xStream
->readBytes(aSequence
, 4) != 4)
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 );
183 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */