1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 #ifndef _MEMORY_BYTE_GRABBER_HXX_ 28 #define _MEMORY_BYTE_GRABBER_HXX_ 29 30 #include <com/sun/star/io/XInputStream.hpp> 31 #include <com/sun/star/io/XSeekable.hpp> 32 #include <string.h> 33 34 class MemoryByteGrabber 35 { 36 protected: 37 const com::sun::star::uno::Sequence < sal_Int8 > maBuffer; 38 const sal_Int8 *mpBuffer; 39 sal_Int32 mnCurrent, mnEnd; 40 public: 41 MemoryByteGrabber ( const com::sun::star::uno::Sequence < sal_Int8 > & rBuffer ) 42 : maBuffer ( rBuffer ) 43 , mpBuffer ( rBuffer.getConstArray() ) 44 , mnCurrent ( 0 ) 45 , mnEnd ( rBuffer.getLength() ) 46 { 47 } 48 MemoryByteGrabber() 49 { 50 } 51 const sal_Int8 * getCurrentPos () { return mpBuffer + mnCurrent; } 52 53 // XInputStream chained 54 sal_Int32 SAL_CALL readBytes( com::sun::star::uno::Sequence< sal_Int8 >& aData, 55 sal_Int32 nBytesToRead ) 56 throw(com::sun::star::io::NotConnectedException, com::sun::star::io::BufferSizeExceededException, com::sun::star::io::IOException, com::sun::star::uno::RuntimeException) 57 { 58 if ( nBytesToRead < 0) 59 throw com::sun::star::io::BufferSizeExceededException(); 60 61 if (nBytesToRead + mnCurrent > mnEnd) 62 nBytesToRead = mnEnd - mnCurrent; 63 64 aData.realloc ( nBytesToRead ); 65 rtl_copyMemory( aData.getArray(), mpBuffer + mnCurrent, nBytesToRead ); 66 mnCurrent += nBytesToRead; 67 return nBytesToRead; 68 } 69 70 sal_Int32 SAL_CALL readSomeBytes( com::sun::star::uno::Sequence< sal_Int8 >& aData, 71 sal_Int32 nMaxBytesToRead ) 72 throw(com::sun::star::io::NotConnectedException, com::sun::star::io::BufferSizeExceededException, com::sun::star::io::IOException, com::sun::star::uno::RuntimeException) 73 { 74 return readBytes( aData, nMaxBytesToRead ); 75 } 76 void SAL_CALL skipBytes( sal_Int32 nBytesToSkip ) 77 throw(com::sun::star::io::NotConnectedException, com::sun::star::io::BufferSizeExceededException, com::sun::star::io::IOException, com::sun::star::uno::RuntimeException) 78 { 79 mnCurrent += nBytesToSkip; 80 } 81 sal_Int32 SAL_CALL available( ) 82 throw(com::sun::star::io::NotConnectedException, com::sun::star::io::IOException, com::sun::star::uno::RuntimeException) 83 { 84 return mnEnd - mnCurrent; 85 } 86 void SAL_CALL closeInput( ) 87 throw(com::sun::star::io::NotConnectedException, com::sun::star::io::IOException, com::sun::star::uno::RuntimeException) 88 { 89 } 90 91 // XSeekable chained... 92 sal_Int64 SAL_CALL seek( sal_Int64 location ) 93 throw(com::sun::star::lang::IllegalArgumentException, com::sun::star::io::IOException, com::sun::star::uno::RuntimeException) 94 { 95 if ( location < 0 || location > mnEnd ) 96 throw com::sun::star::lang::IllegalArgumentException (); 97 mnCurrent = static_cast < sal_Int32 > ( location ); 98 return mnCurrent; 99 } 100 sal_Int64 SAL_CALL getPosition( ) 101 throw(com::sun::star::io::IOException, com::sun::star::uno::RuntimeException) 102 { 103 return mnCurrent; 104 } 105 sal_Int64 SAL_CALL getLength( ) 106 throw(com::sun::star::io::IOException, com::sun::star::uno::RuntimeException) 107 { 108 return mnEnd; 109 } 110 MemoryByteGrabber& operator >> (sal_Int8& rInt8) 111 { 112 if (mnCurrent + 1 > mnEnd ) 113 rInt8 = 0; 114 else 115 rInt8 = mpBuffer [mnCurrent++] & 0xFF; 116 return *this; 117 } 118 MemoryByteGrabber& operator >> (sal_Int16& rInt16) 119 { 120 if (mnCurrent + 2 > mnEnd ) 121 rInt16 = 0; 122 else 123 { 124 rInt16 = mpBuffer[mnCurrent++] & 0xFF; 125 rInt16 |= ( mpBuffer[mnCurrent++] & 0xFF ) << 8; 126 } 127 return *this; 128 } 129 MemoryByteGrabber& operator >> (sal_Int32& rInt32) 130 { 131 if (mnCurrent + 4 > mnEnd ) 132 rInt32 = 0; 133 else 134 { 135 rInt32 = mpBuffer[mnCurrent++] & 0xFF; 136 rInt32 |= ( mpBuffer[mnCurrent++] & 0xFF ) << 8; 137 rInt32 |= ( mpBuffer[mnCurrent++] & 0xFF ) << 16; 138 rInt32 |= ( mpBuffer[mnCurrent++] & 0xFF ) << 24; 139 } 140 return *this; 141 } 142 143 MemoryByteGrabber& operator >> (sal_uInt8& rInt8) 144 { 145 if (mnCurrent + 1 > mnEnd ) 146 rInt8 = 0; 147 else 148 rInt8 = mpBuffer [mnCurrent++] & 0xFF; 149 return *this; 150 } 151 MemoryByteGrabber& operator >> (sal_uInt16& rInt16) 152 { 153 if (mnCurrent + 2 > mnEnd ) 154 rInt16 = 0; 155 else 156 { 157 rInt16 = mpBuffer [mnCurrent++] & 0xFF; 158 rInt16 |= ( mpBuffer [mnCurrent++] & 0xFF ) << 8; 159 } 160 return *this; 161 } 162 MemoryByteGrabber& operator >> (sal_uInt32& rInt32) 163 { 164 if (mnCurrent + 4 > mnEnd ) 165 rInt32 = 0; 166 else 167 { 168 rInt32 = mpBuffer [mnCurrent++] & 0xFF; 169 rInt32 |= ( mpBuffer [mnCurrent++] & 0xFF ) << 8; 170 rInt32 |= ( mpBuffer [mnCurrent++] & 0xFF ) << 16; 171 rInt32 |= ( mpBuffer [mnCurrent++] & 0xFF ) << 24; 172 } 173 return *this; 174 } 175 }; 176 177 #endif 178