1 2 /* ==================================================================== 3 * The Apache Software License, Version 1.1 4 * 5 * Copyright (c) 2002 The Apache Software Foundation. All rights 6 * reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in 17 * the documentation and/or other materials provided with the 18 * distribution. 19 * 20 * 3. The end-user documentation included with the redistribution, 21 * if any, must include the following acknowledgment: 22 * "This product includes software developed by the 23 * Apache Software Foundation (http://www.apache.org/)." 24 * Alternately, this acknowledgment may appear in the software itself, 25 * if and wherever such third-party acknowledgments normally appear. 26 * 27 * 4. The names "Apache" and "Apache Software Foundation" and 28 * "Apache POI" must not be used to endorse or promote products 29 * derived from this software without prior written permission. For 30 * written permission, please contact apache@apache.org. 31 * 32 * 5. Products derived from this software may not be called "Apache", 33 * "Apache POI", nor may "Apache" appear in their name, without 34 * prior written permission of the Apache Software Foundation. 35 * 36 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 37 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 38 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 39 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 42 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 43 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 44 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 45 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 46 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 47 * SUCH DAMAGE. 48 * ==================================================================== 49 * 50 * This software consists of voluntary contributions made by many 51 * individuals on behalf of the Apache Software Foundation. For more 52 * information on the Apache Software Foundation, please see 53 * <http://www.apache.org/>. 54 */ 55 56 package org.apache.poi.util; 57 58 import org.apache.poi.util.LittleEndian.BufferUnderrunException; 59 60 import java.io.*; 61 62 /** 63 * representation of an integer (32-bit) field at a fixed location 64 * within a byte array 65 * 66 * @author Marc Johnson (mjohnson at apache dot org 67 */ 68 69 public class IntegerField 70 implements FixedField 71 { 72 private int _value; 73 private final int _offset; 74 75 /** 76 * construct the IntegerField with its offset into its containing 77 * byte array 78 * 79 * @param offset of the field within its byte array 80 * 81 * @exception ArrayIndexOutOfBoundsException if the offset is 82 * negative 83 */ 84 85 public IntegerField(final int offset) 86 throws ArrayIndexOutOfBoundsException 87 { 88 if (offset < 0) 89 { 90 throw new ArrayIndexOutOfBoundsException("negative offset"); 91 } 92 _offset = offset; 93 } 94 95 /** 96 * construct the IntegerField with its offset into its containing 97 * byte array and initialize its value 98 * 99 * @param offset of the field within its byte array 100 * @param value the initial value 101 * 102 * @exception ArrayIndexOutOfBoundsException if the offset is 103 * negative 104 */ 105 106 public IntegerField(final int offset, final int value) 107 throws ArrayIndexOutOfBoundsException 108 { 109 this(offset); 110 set(value); 111 } 112 113 /** 114 * Construct the IntegerField with its offset into its containing 115 * byte array and initialize its value from its byte array 116 * 117 * @param offset of the field within its byte array 118 * @param data the byte array to read the value from 119 * 120 * @exception ArrayIndexOutOfBoundsException if the offset is not 121 * within the range of 0..(data.length - 1) 122 */ 123 124 public IntegerField(final int offset, final byte [] data) 125 throws ArrayIndexOutOfBoundsException 126 { 127 this(offset); 128 readFromBytes(data); 129 } 130 131 /** 132 * construct the IntegerField with its offset into its containing 133 * byte array, initialize its value, and write the value to a byte 134 * array 135 * 136 * @param offset of the field within its byte array 137 * @param value the initial value 138 * @param data the byte array to write the value to 139 * 140 * @exception ArrayIndexOutOfBoundsException if the offset is 141 * negative or too large 142 */ 143 144 public IntegerField(final int offset, final int value, final byte [] data) 145 throws ArrayIndexOutOfBoundsException 146 { 147 this(offset); 148 set(value, data); 149 } 150 151 /** 152 * get the IntegerField's current value 153 * 154 * @return current value 155 */ 156 157 public int get() 158 { 159 return _value; 160 } 161 162 /** 163 * set the IntegerField's current value 164 * 165 * @param value to be set 166 */ 167 168 public void set(final int value) 169 { 170 _value = value; 171 } 172 173 /** 174 * set the IntegerField's current value and write it to a byte 175 * array 176 * 177 * @param value to be set 178 * @param data the byte array to write the value to 179 * 180 * @exception ArrayIndexOutOfBoundsException if the offset is too 181 * large 182 */ 183 184 public void set(final int value, final byte [] data) 185 throws ArrayIndexOutOfBoundsException 186 { 187 _value = value; 188 writeToBytes(data); 189 } 190 191 /* ********** START implementation of FixedField ********** */ 192 193 /** 194 * set the value from its offset into an array of bytes 195 * 196 * @param data the byte array from which the value is to be read 197 * 198 * @exception ArrayIndexOutOfBoundsException if the offset is too 199 * large 200 */ 201 202 public void readFromBytes(final byte [] data) 203 throws ArrayIndexOutOfBoundsException 204 { 205 _value = LittleEndian.getInt(data, _offset); 206 } 207 208 /** 209 * set the value from an InputStream 210 * 211 * @param stream the InputStream from which the value is to be 212 * read 213 * 214 * @exception BufferUnderrunException if there is not enough data 215 * available from the InputStream 216 * @exception IOException if an IOException is thrown from reading 217 * the InputStream 218 */ 219 220 public void readFromStream(final InputStream stream) 221 throws IOException, BufferUnderrunException 222 { 223 _value = LittleEndian.readInt(stream); 224 } 225 226 /** 227 * write the value out to an array of bytes at the appropriate 228 * offset 229 * 230 * @param data the array of bytes to which the value is to be 231 * written 232 * 233 * @exception ArrayIndexOutOfBoundsException if the offset is too 234 * large 235 */ 236 237 public void writeToBytes(final byte [] data) 238 throws ArrayIndexOutOfBoundsException 239 { 240 LittleEndian.putInt(data, _offset, _value); 241 } 242 243 /** 244 * return the value as a String 245 * 246 * @return the value as a String 247 */ 248 249 public String toString() 250 { 251 return String.valueOf(_value); 252 } 253 254 /* ********** END implementation of FixedField ********** */ 255 } // end public class IntegerField 256 257