Bump version to 5.0-14
[LibreOffice.git] / package / source / zipapi / MemoryByteGrabber.hxx
blob0e2a7393de0de2ad138aa4735a979c8ae38a94a1
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 INCLUDED_PACKAGE_SOURCE_ZIPAPI_MEMORYBYTEGRABBER_HXX
20 #define INCLUDED_PACKAGE_SOURCE_ZIPAPI_MEMORYBYTEGRABBER_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;
79 // XSeekable chained...
80 sal_Int64 SAL_CALL seek( sal_Int64 location )
81 throw(com::sun::star::lang::IllegalArgumentException, com::sun::star::io::IOException, com::sun::star::uno::RuntimeException)
83 if ( location < 0 || location > mnEnd )
84 throw com::sun::star::lang::IllegalArgumentException ();
85 mnCurrent = static_cast < sal_Int32 > ( location );
86 return mnCurrent;
88 sal_Int64 SAL_CALL getPosition( )
89 throw(com::sun::star::io::IOException, com::sun::star::uno::RuntimeException)
91 return mnCurrent;
93 sal_Int64 SAL_CALL getLength( )
94 throw(com::sun::star::io::IOException, com::sun::star::uno::RuntimeException)
96 return mnEnd;
98 sal_Int8 ReadInt8()
100 if (mnCurrent + 1 > mnEnd )
101 return 0;
102 return mpBuffer [mnCurrent++] & 0xFF;
104 sal_Int16 ReadInt16()
106 if (mnCurrent + 2 > mnEnd )
107 return 0;
108 sal_Int16 nInt16 = mpBuffer[mnCurrent++] & 0xFF;
109 nInt16 |= ( mpBuffer[mnCurrent++] & 0xFF ) << 8;
110 return nInt16;
112 sal_Int32 ReadInt32()
114 if (mnCurrent + 4 > mnEnd )
115 return 0;
117 sal_Int32 nInt32 = mpBuffer[mnCurrent++] & 0xFF;
118 nInt32 |= ( mpBuffer[mnCurrent++] & 0xFF ) << 8;
119 nInt32 |= ( mpBuffer[mnCurrent++] & 0xFF ) << 16;
120 nInt32 |= ( mpBuffer[mnCurrent++] & 0xFF ) << 24;
121 return nInt32;
124 sal_uInt8 ReadUInt8()
126 if (mnCurrent + 1 > mnEnd )
127 return 0;
128 return mpBuffer [mnCurrent++] & 0xFF;
130 sal_uInt16 ReadUInt16()
132 if (mnCurrent + 2 > mnEnd )
133 return 0;
135 sal_uInt16 nInt16 = mpBuffer [mnCurrent++] & 0xFF;
136 nInt16 |= ( mpBuffer [mnCurrent++] & 0xFF ) << 8;
137 return nInt16;
139 sal_uInt32 ReadUInt32()
141 if (mnCurrent + 4 > mnEnd )
142 return 0;
144 sal_uInt32 nInt32 = mpBuffer [mnCurrent++] & 0xFF;
145 nInt32 |= ( mpBuffer [mnCurrent++] & 0xFF ) << 8;
146 nInt32 |= ( mpBuffer [mnCurrent++] & 0xFF ) << 16;
147 nInt32 |= ( mpBuffer [mnCurrent++] & 0xFF ) << 24;
148 return nInt32;
152 #endif
154 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */