| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| NumberFixedLength |
|
| 3.2222222222222223;3.222 |
| 1 | /** | |
| 2 | * @author : Paul Taylor | |
| 3 | * @author : Eric Farng | |
| 4 | * | |
| 5 | * Version @version:$Id: NumberFixedLength.java 836 2009-11-12 15:44:07Z paultaylor $ | |
| 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 frameBody | |
| 46 | * @param size the number of significant places that the number is held to | |
| 47 | * @throws IllegalArgumentException | |
| 48 | */ | |
| 49 | public NumberFixedLength(String identifier, AbstractTagFrameBody frameBody, int size) | |
| 50 | { | |
| 51 | 9531 | super(identifier, frameBody); |
| 52 | 9531 | if (size < 0) |
| 53 | { | |
| 54 | 0 | throw new IllegalArgumentException("Length is less than zero: " + size); |
| 55 | } | |
| 56 | 9531 | this.size = size; |
| 57 | ||
| 58 | 9531 | } |
| 59 | ||
| 60 | public NumberFixedLength(NumberFixedLength copy) | |
| 61 | { | |
| 62 | 5433 | super(copy); |
| 63 | 5433 | this.size = copy.size; |
| 64 | 5433 | } |
| 65 | ||
| 66 | ||
| 67 | /** | |
| 68 | * Set Size in Bytes of this Object | |
| 69 | * | |
| 70 | * @param size in bytes that this number will be held as | |
| 71 | */ | |
| 72 | public void setSize(int size) | |
| 73 | { | |
| 74 | 0 | if (size > 0) |
| 75 | { | |
| 76 | 0 | this.size = size; |
| 77 | } | |
| 78 | 0 | } |
| 79 | ||
| 80 | /** | |
| 81 | * Return size | |
| 82 | * | |
| 83 | * @return the size of this number | |
| 84 | */ | |
| 85 | public int getSize() | |
| 86 | { | |
| 87 | 11066 | return size; |
| 88 | } | |
| 89 | ||
| 90 | public void setValue(Object value) | |
| 91 | { | |
| 92 | 56 | if (!(value instanceof Number)) |
| 93 | { | |
| 94 | 0 | throw new IllegalArgumentException("Invalid value type for NumberFixedLength:" + value.getClass()); |
| 95 | } | |
| 96 | 56 | super.setValue(value); |
| 97 | 56 | } |
| 98 | ||
| 99 | ||
| 100 | /** | |
| 101 | * @param obj | |
| 102 | * @return true if obj equivalent to this | |
| 103 | */ | |
| 104 | public boolean equals(Object obj) | |
| 105 | { | |
| 106 | 4 | if (!(obj instanceof NumberFixedLength)) |
| 107 | { | |
| 108 | 0 | return false; |
| 109 | } | |
| 110 | 4 | NumberFixedLength object = (NumberFixedLength) obj; |
| 111 | 4 | return this.size == object.size && super.equals(obj); |
| 112 | } | |
| 113 | ||
| 114 | /** | |
| 115 | * Read the number from the byte array | |
| 116 | * | |
| 117 | * @param arr | |
| 118 | * @param offset | |
| 119 | * @throws NullPointerException | |
| 120 | * @throws IndexOutOfBoundsException | |
| 121 | */ | |
| 122 | public void readByteArray(byte[] arr, int offset) throws InvalidDataTypeException | |
| 123 | { | |
| 124 | 6715 | if (arr == null) |
| 125 | { | |
| 126 | 0 | throw new NullPointerException("Byte array is null"); |
| 127 | } | |
| 128 | 6715 | if ((offset < 0) || (offset >= arr.length)) |
| 129 | { | |
| 130 | 1 | throw new InvalidDataTypeException("Offset to byte array is out of bounds: offset = " + offset + ", array.length = " + arr.length); |
| 131 | } | |
| 132 | ||
| 133 | 6714 | if(offset + size > arr.length) |
| 134 | { | |
| 135 | 0 | throw new InvalidDataTypeException("Offset plus size to byte array is out of bounds: offset = " |
| 136 | + offset + ", size = "+size +" + arr.length "+ arr.length ); | |
| 137 | } | |
| 138 | ||
| 139 | 6714 | long lvalue = 0; |
| 140 | 13428 | for (int i = offset; i < (offset + size); i++) |
| 141 | { | |
| 142 | 6714 | lvalue <<= 8; |
| 143 | 6714 | lvalue += (arr[i] & 0xff); |
| 144 | } | |
| 145 | 6714 | value = lvalue; |
| 146 | 6714 | logger.info("Read NumberFixedlength:" + value); |
| 147 | 6714 | } |
| 148 | ||
| 149 | ||
| 150 | /** | |
| 151 | * @return String representation of this datatype | |
| 152 | */ | |
| 153 | public String toString() | |
| 154 | { | |
| 155 | 0 | if (value == null) |
| 156 | { | |
| 157 | 0 | return ""; |
| 158 | } | |
| 159 | else | |
| 160 | { | |
| 161 | 0 | return value.toString(); |
| 162 | } | |
| 163 | } | |
| 164 | ||
| 165 | /** | |
| 166 | * Write data to byte array | |
| 167 | * | |
| 168 | * @return the datatype converted to a byte array | |
| 169 | */ | |
| 170 | public byte[] writeByteArray() | |
| 171 | { | |
| 172 | byte[] arr; | |
| 173 | 4364 | arr = new byte[size]; |
| 174 | 4364 | if (value != null) |
| 175 | { | |
| 176 | //Convert value to long | |
| 177 | 4336 | long temp = ID3Tags.getWholeNumber(value); |
| 178 | ||
| 179 | 8672 | for (int i = size - 1; i >= 0; i--) |
| 180 | { | |
| 181 | 4336 | arr[i] = (byte) (temp & 0xFF); |
| 182 | 4336 | temp >>= 8; |
| 183 | } | |
| 184 | } | |
| 185 | 4364 | return arr; |
| 186 | } | |
| 187 | } |