bump product version to 4.1.6.2
[LibreOffice.git] / package / source / zipapi / MemoryByteGrabber.hxx
blob90f542db4a7aae8776504f201c249478acb9be1a
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 .
19 #ifndef _MEMORY_BYTE_GRABBER_HXX_
20 #define _MEMORY_BYTE_GRABBER_HXX_
22 #include <com/sun/star/io/XInputStream.hpp>
23 #include <com/sun/star/io/XSeekable.hpp>
24 #include <string.h>
26 class MemoryByteGrabber
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 MemoryByteGrabber ( const com::sun::star::uno::Sequence < sal_Int8 > & rBuffer )
34 : maBuffer ( rBuffer )
35 , mpBuffer ( rBuffer.getConstArray() )
36 , mnCurrent ( 0 )
37 , mnEnd ( rBuffer.getLength() )
40 MemoryByteGrabber()
43 const sal_Int8 * getCurrentPos () { return mpBuffer + mnCurrent; }
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)
50 if ( nBytesToRead < 0)
51 throw com::sun::star::io::BufferSizeExceededException();
53 if (nBytesToRead + mnCurrent > mnEnd)
54 nBytesToRead = mnEnd - mnCurrent;
56 aData.realloc ( nBytesToRead );
57 memcpy( aData.getArray(), mpBuffer + mnCurrent, nBytesToRead );
58 mnCurrent += nBytesToRead;
59 return nBytesToRead;
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)
66 return readBytes( aData, nMaxBytesToRead );
68 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)
71 mnCurrent += nBytesToSkip;
73 sal_Int32 SAL_CALL available( )
74 throw(com::sun::star::io::NotConnectedException, com::sun::star::io::IOException, com::sun::star::uno::RuntimeException)
76 return mnEnd - mnCurrent;
78 void SAL_CALL closeInput( )
79 throw(com::sun::star::io::NotConnectedException, com::sun::star::io::IOException, com::sun::star::uno::RuntimeException)
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)
87 if ( location < 0 || location > mnEnd )
88 throw com::sun::star::lang::IllegalArgumentException ();
89 mnCurrent = static_cast < sal_Int32 > ( location );
90 return mnCurrent;
92 sal_Int64 SAL_CALL getPosition( )
93 throw(com::sun::star::io::IOException, com::sun::star::uno::RuntimeException)
95 return mnCurrent;
97 sal_Int64 SAL_CALL getLength( )
98 throw(com::sun::star::io::IOException, com::sun::star::uno::RuntimeException)
100 return mnEnd;
102 MemoryByteGrabber& operator >> (sal_Int8& rInt8)
104 if (mnCurrent + 1 > mnEnd )
105 rInt8 = 0;
106 else
107 rInt8 = mpBuffer [mnCurrent++] & 0xFF;
108 return *this;
110 MemoryByteGrabber& operator >> (sal_Int16& rInt16)
112 if (mnCurrent + 2 > mnEnd )
113 rInt16 = 0;
114 else
116 rInt16 = mpBuffer[mnCurrent++] & 0xFF;
117 rInt16 |= ( mpBuffer[mnCurrent++] & 0xFF ) << 8;
119 return *this;
121 MemoryByteGrabber& operator >> (sal_Int32& rInt32)
123 if (mnCurrent + 4 > mnEnd )
124 rInt32 = 0;
125 else
127 rInt32 = mpBuffer[mnCurrent++] & 0xFF;
128 rInt32 |= ( mpBuffer[mnCurrent++] & 0xFF ) << 8;
129 rInt32 |= ( mpBuffer[mnCurrent++] & 0xFF ) << 16;
130 rInt32 |= ( mpBuffer[mnCurrent++] & 0xFF ) << 24;
132 return *this;
135 MemoryByteGrabber& operator >> (sal_uInt8& rInt8)
137 if (mnCurrent + 1 > mnEnd )
138 rInt8 = 0;
139 else
140 rInt8 = mpBuffer [mnCurrent++] & 0xFF;
141 return *this;
143 MemoryByteGrabber& operator >> (sal_uInt16& rInt16)
145 if (mnCurrent + 2 > mnEnd )
146 rInt16 = 0;
147 else
149 rInt16 = mpBuffer [mnCurrent++] & 0xFF;
150 rInt16 |= ( mpBuffer [mnCurrent++] & 0xFF ) << 8;
152 return *this;
154 MemoryByteGrabber& operator >> (sal_uInt32& rInt32)
156 if (mnCurrent + 4 > mnEnd )
157 rInt32 = 0;
158 else
160 rInt32 = mpBuffer [mnCurrent++] & 0xFF;
161 rInt32 |= ( mpBuffer [mnCurrent++] & 0xFF ) << 8;
162 rInt32 |= ( mpBuffer [mnCurrent++] & 0xFF ) << 16;
163 rInt32 |= ( mpBuffer [mnCurrent++] & 0xFF ) << 24;
165 return *this;
169 #endif
171 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */