| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| NumberFixedLength |
|
| 3.0;3 |
| 1 | /** | |
| 2 | * @author : Paul Taylor | |
| 3 | * @author : Eric Farng | |
| 4 | * | |
| 5 | * Version @version:$Id: NumberFixedLength.java,v 1.12 2008/07/21 10:45:41 paultaylor Exp $ | |
| 6 | * | |
| 7 | * MusicTag Copyright (C)2003,2004 | |
| 8 | * | |
| 9 | * This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser | |
| 10 | * General Public License as published by the Free Software Foundation; either version 2.1 of the License, | |
| 11 | * or (at your option) any later version. | |
| 12 | * | |
| 13 | * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even | |
| 14 | * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
| 15 | * See the GNU Lesser General Public License for more details. | |
| 16 | * | |
| 17 | * You should have received a copy of the GNU Lesser General Public License along with this library; if not, | |
| 18 | * you can get a copy from http://www.opensource.org/licenses/lgpl-license.php or write to the Free Software | |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 20 | * | |
| 21 | * Description: | |
| 22 | * Represents a Number of a fixed number of decimal places. | |
| 23 | */ | |
| 24 | package org.jaudiotagger.tag.datatype; | |
| 25 | ||
| 26 | import org.jaudiotagger.tag.InvalidDataTypeException; | |
| 27 | import org.jaudiotagger.tag.id3.AbstractTagFrameBody; | |
| 28 | import org.jaudiotagger.tag.id3.ID3Tags; | |
| 29 | ||
| 30 | ||
| 31 | /** | |
| 32 | * Represents a number held as a fixed number of digits. | |
| 33 | * <p/> | |
| 34 | * The bitorder in ID3v2 is most significant bit first (MSB). The byteorder in multibyte numbers is most significant | |
| 35 | * byte first (e.g. $12345678 would be encoded $12 34 56 78), also known as big endian and network byte order. | |
| 36 | * <p/> | |
| 37 | * In ID3Specification would be denoted as $xx xx this denotes exactly two bytes required | |
| 38 | */ | |
| 39 | public class NumberFixedLength extends AbstractDataType | |
| 40 | { | |
| 41 | /** | |
| 42 | * Creates a new ObjectNumberFixedLength datatype. | |
| 43 | * | |
| 44 | * @param identifier | |
| 45 | * @param size the number of significant places that the number is held to | |
| 46 | * @throws IllegalArgumentException | |
| 47 | */ | |
| 48 | public NumberFixedLength(String identifier, AbstractTagFrameBody frameBody, int size) | |
| 49 | { | |
| 50 | 2077 | super(identifier, frameBody); |
| 51 | 2077 | if (size < 0) |
| 52 | { | |
| 53 | 0 | throw new IllegalArgumentException("Length is less than zero: " + size); |
| 54 | } | |
| 55 | 2077 | this.size = size; |
| 56 | ||
| 57 | 2077 | } |
| 58 | ||
| 59 | public NumberFixedLength(NumberFixedLength copy) | |
| 60 | { | |
| 61 | 1155 | super(copy); |
| 62 | 1155 | this.size = copy.size; |
| 63 | 1155 | } |
| 64 | ||
| 65 | ||
| 66 | /** | |
| 67 | * Set Size in Bytes of this Object | |
| 68 | * | |
| 69 | * @param size in bytes that this number will be held as | |
| 70 | */ | |
| 71 | public void setSize(int size) | |
| 72 | { | |
| 73 | 0 | if (size > 0) |
| 74 | { | |
| 75 | 0 | this.size = size; |
| 76 | } | |
| 77 | 0 | } |
| 78 | ||
| 79 | /** | |
| 80 | * Return size | |
| 81 | * | |
| 82 | * @return the size of this number | |
| 83 | */ | |
| 84 | public int getSize() | |
| 85 | { | |
| 86 | 2482 | return size; |
| 87 | } | |
| 88 | ||
| 89 | public void setValue(Object value) | |
| 90 | { | |
| 91 | 14 | if (!(value instanceof Number)) |
| 92 | { | |
| 93 | 0 | throw new IllegalArgumentException("Invalid value type for NumberFixedLength:" + value.getClass()); |
| 94 | } | |
| 95 | 14 | super.setValue(value); |
| 96 | 14 | } |
| 97 | ||
| 98 | ||
| 99 | /** | |
| 100 | * @param obj | |
| 101 | * @return true if obj equivalent to this | |
| 102 | */ | |
| 103 | public boolean equals(Object obj) | |
| 104 | { | |
| 105 | 1 | if ((obj instanceof NumberFixedLength) == false) |
| 106 | { | |
| 107 | 0 | return false; |
| 108 | } | |
| 109 | 1 | NumberFixedLength object = (NumberFixedLength) obj; |
| 110 | 1 | if (this.size != object.size) |
| 111 | { | |
| 112 | 0 | return false; |
| 113 | } | |
| 114 | 1 | return super.equals(obj); |
| 115 | } | |
| 116 | ||
| 117 | /** | |
| 118 | * Read the number from the byte array | |
| 119 | * | |
| 120 | * @param arr | |
| 121 | * @param offset | |
| 122 | * @throws NullPointerException | |
| 123 | * @throws IndexOutOfBoundsException | |
| 124 | */ | |
| 125 | public void readByteArray(byte[] arr, int offset) throws InvalidDataTypeException | |
| 126 | { | |
| 127 | 1531 | if (arr == null) |
| 128 | { | |
| 129 | 0 | throw new NullPointerException("Byte array is null"); |
| 130 | } | |
| 131 | 1531 | if ((offset < 0) || (offset >= arr.length)) |
| 132 | { | |
| 133 | 1 | throw new InvalidDataTypeException("Offset to byte array is out of bounds: offset = " + offset + ", array.length = " + arr.length); |
| 134 | } | |
| 135 | 1530 | long lvalue = 0; |
| 136 | 3060 | for (int i = offset; i < (offset + size); i++) |
| 137 | { | |
| 138 | 1530 | lvalue <<= 8; |
| 139 | 1530 | lvalue += (arr[i] & 0xff); |
| 140 | } | |
| 141 | 1530 | value = lvalue; |
| 142 | 1530 | logger.info("Read NumberFixedlength:" + value); |
| 143 | 1530 | } |
| 144 | ||
| 145 | ||
| 146 | /** | |
| 147 | * @return String representation of this datatype | |
| 148 | */ | |
| 149 | public String toString() | |
| 150 | { | |
| 151 | 0 | if (value == null) |
| 152 | { | |
| 153 | 0 | return ""; |
| 154 | } | |
| 155 | else | |
| 156 | { | |
| 157 | 0 | return value.toString(); |
| 158 | } | |
| 159 | } | |
| 160 | ||
| 161 | /** | |
| 162 | * Write data to byte array | |
| 163 | * | |
| 164 | * @return the datatype converted to a byte array | |
| 165 | */ | |
| 166 | public byte[] writeByteArray() | |
| 167 | { | |
| 168 | byte[] arr; | |
| 169 | 953 | arr = new byte[size]; |
| 170 | 953 | if (value != null) |
| 171 | { | |
| 172 | //Convert value to long | |
| 173 | 946 | long temp = ID3Tags.getWholeNumber(value); |
| 174 | ||
| 175 | 1892 | for (int i = size - 1; i >= 0; i--) |
| 176 | { | |
| 177 | 946 | arr[i] = (byte) (temp & 0xFF); |
| 178 | 946 | temp >>= 8; |
| 179 | } | |
| 180 | } | |
| 181 | 953 | return arr; |
| 182 | } | |
| 183 | } |