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 : #ifndef _MEMORY_BYTE_GRABBER_HXX_
20 : #define _MEMORY_BYTE_GRABBER_HXX_
21 :
22 : #include <com/sun/star/io/XInputStream.hpp>
23 : #include <com/sun/star/io/XSeekable.hpp>
24 : #include <string.h>
25 :
26 420 : class MemoryByteGrabber
27 : {
28 : protected:
29 : const com::sun::star::uno::Sequence < sal_Int8 > maBuffer;
30 : const sal_Int8 *mpBuffer;
31 : sal_Int32 mnCurrent, mnEnd;
32 : public:
33 420 : MemoryByteGrabber ( const com::sun::star::uno::Sequence < sal_Int8 > & rBuffer )
34 : : maBuffer ( rBuffer )
35 420 : , mpBuffer ( rBuffer.getConstArray() )
36 : , mnCurrent ( 0 )
37 840 : , mnEnd ( rBuffer.getLength() )
38 : {
39 420 : }
40 : MemoryByteGrabber()
41 : {
42 : }
43 5654 : const sal_Int8 * getCurrentPos () { return mpBuffer + mnCurrent; }
44 :
45 : // XInputStream chained
46 : sal_Int32 SAL_CALL readBytes( com::sun::star::uno::Sequence< sal_Int8 >& aData,
47 : sal_Int32 nBytesToRead )
48 : throw(com::sun::star::io::NotConnectedException, com::sun::star::io::BufferSizeExceededException, com::sun::star::io::IOException, com::sun::star::uno::RuntimeException)
49 : {
50 : if ( nBytesToRead < 0)
51 : throw com::sun::star::io::BufferSizeExceededException();
52 :
53 : if (nBytesToRead + mnCurrent > mnEnd)
54 : nBytesToRead = mnEnd - mnCurrent;
55 :
56 : aData.realloc ( nBytesToRead );
57 : memcpy( aData.getArray(), mpBuffer + mnCurrent, nBytesToRead );
58 : mnCurrent += nBytesToRead;
59 : return nBytesToRead;
60 : }
61 :
62 : sal_Int32 SAL_CALL readSomeBytes( com::sun::star::uno::Sequence< sal_Int8 >& aData,
63 : sal_Int32 nMaxBytesToRead )
64 : throw(com::sun::star::io::NotConnectedException, com::sun::star::io::BufferSizeExceededException, com::sun::star::io::IOException, com::sun::star::uno::RuntimeException)
65 : {
66 : return readBytes( aData, nMaxBytesToRead );
67 : }
68 16962 : void SAL_CALL skipBytes( sal_Int32 nBytesToSkip )
69 : throw(com::sun::star::io::NotConnectedException, com::sun::star::io::BufferSizeExceededException, com::sun::star::io::IOException, com::sun::star::uno::RuntimeException)
70 : {
71 16962 : mnCurrent += nBytesToSkip;
72 16962 : }
73 : sal_Int32 SAL_CALL available( )
74 : throw(com::sun::star::io::NotConnectedException, com::sun::star::io::IOException, com::sun::star::uno::RuntimeException)
75 : {
76 : return mnEnd - mnCurrent;
77 : }
78 : void SAL_CALL closeInput( )
79 : throw(com::sun::star::io::NotConnectedException, com::sun::star::io::IOException, com::sun::star::uno::RuntimeException)
80 : {
81 : }
82 :
83 : // XSeekable chained...
84 : sal_Int64 SAL_CALL seek( sal_Int64 location )
85 : throw(com::sun::star::lang::IllegalArgumentException, com::sun::star::io::IOException, com::sun::star::uno::RuntimeException)
86 : {
87 : if ( location < 0 || location > mnEnd )
88 : throw com::sun::star::lang::IllegalArgumentException ();
89 : mnCurrent = static_cast < sal_Int32 > ( location );
90 : return mnCurrent;
91 : }
92 : sal_Int64 SAL_CALL getPosition( )
93 : throw(com::sun::star::io::IOException, com::sun::star::uno::RuntimeException)
94 : {
95 : return mnCurrent;
96 : }
97 : sal_Int64 SAL_CALL getLength( )
98 : throw(com::sun::star::io::IOException, com::sun::star::uno::RuntimeException)
99 : {
100 : return mnEnd;
101 : }
102 : MemoryByteGrabber& operator >> (sal_Int8& rInt8)
103 : {
104 : if (mnCurrent + 1 > mnEnd )
105 : rInt8 = 0;
106 : else
107 : rInt8 = mpBuffer [mnCurrent++] & 0xFF;
108 : return *this;
109 : }
110 33924 : MemoryByteGrabber& operator >> (sal_Int16& rInt16)
111 : {
112 33924 : if (mnCurrent + 2 > mnEnd )
113 0 : rInt16 = 0;
114 : else
115 : {
116 33924 : rInt16 = mpBuffer[mnCurrent++] & 0xFF;
117 33924 : rInt16 |= ( mpBuffer[mnCurrent++] & 0xFF ) << 8;
118 : }
119 33924 : return *this;
120 : }
121 16962 : MemoryByteGrabber& operator >> (sal_Int32& rInt32)
122 : {
123 16962 : if (mnCurrent + 4 > mnEnd )
124 0 : rInt32 = 0;
125 : else
126 : {
127 16962 : rInt32 = mpBuffer[mnCurrent++] & 0xFF;
128 16962 : rInt32 |= ( mpBuffer[mnCurrent++] & 0xFF ) << 8;
129 16962 : rInt32 |= ( mpBuffer[mnCurrent++] & 0xFF ) << 16;
130 16962 : rInt32 |= ( mpBuffer[mnCurrent++] & 0xFF ) << 24;
131 : }
132 16962 : return *this;
133 : }
134 :
135 : MemoryByteGrabber& operator >> (sal_uInt8& rInt8)
136 : {
137 : if (mnCurrent + 1 > mnEnd )
138 : rInt8 = 0;
139 : else
140 : rInt8 = mpBuffer [mnCurrent++] & 0xFF;
141 : return *this;
142 : }
143 : MemoryByteGrabber& operator >> (sal_uInt16& rInt16)
144 : {
145 : if (mnCurrent + 2 > mnEnd )
146 : rInt16 = 0;
147 : else
148 : {
149 : rInt16 = mpBuffer [mnCurrent++] & 0xFF;
150 : rInt16 |= ( mpBuffer [mnCurrent++] & 0xFF ) << 8;
151 : }
152 : return *this;
153 : }
154 16962 : MemoryByteGrabber& operator >> (sal_uInt32& rInt32)
155 : {
156 16962 : if (mnCurrent + 4 > mnEnd )
157 0 : rInt32 = 0;
158 : else
159 : {
160 16962 : rInt32 = mpBuffer [mnCurrent++] & 0xFF;
161 16962 : rInt32 |= ( mpBuffer [mnCurrent++] & 0xFF ) << 8;
162 16962 : rInt32 |= ( mpBuffer [mnCurrent++] & 0xFF ) << 16;
163 16962 : rInt32 |= ( mpBuffer [mnCurrent++] & 0xFF ) << 24;
164 : }
165 16962 : return *this;
166 : }
167 : };
168 :
169 : #endif
170 :
171 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|