| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 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.valuepair.TextEncoding; |
| 29 | |
|
| 30 | |
import java.nio.ByteBuffer; |
| 31 | |
import java.nio.CharBuffer; |
| 32 | |
import java.nio.charset.CharacterCodingException; |
| 33 | |
import java.nio.charset.Charset; |
| 34 | |
import java.nio.charset.CharsetDecoder; |
| 35 | |
import java.nio.charset.CharsetEncoder; |
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
public class StringFixedLength extends AbstractString |
| 43 | |
{ |
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
public StringFixedLength(String identifier, AbstractTagFrameBody frameBody, int size) |
| 53 | |
{ |
| 54 | 865 | super(identifier, frameBody); |
| 55 | 865 | if (size < 0) |
| 56 | |
{ |
| 57 | 0 | throw new IllegalArgumentException("size is less than zero: " + size); |
| 58 | |
} |
| 59 | 865 | setSize(size); |
| 60 | 865 | } |
| 61 | |
|
| 62 | |
public StringFixedLength(StringFixedLength copyObject) |
| 63 | |
{ |
| 64 | 480 | super(copyObject); |
| 65 | 480 | this.size = copyObject.size; |
| 66 | 480 | } |
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
public boolean equals(Object obj) |
| 73 | |
{ |
| 74 | 4 | if (!(obj instanceof StringFixedLength)) |
| 75 | |
{ |
| 76 | 0 | return false; |
| 77 | |
} |
| 78 | 4 | StringFixedLength object = (StringFixedLength) obj; |
| 79 | 4 | return this.size == object.size && super.equals(obj); |
| 80 | |
} |
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
public void readByteArray(byte[] arr, int offset) throws InvalidDataTypeException |
| 89 | |
{ |
| 90 | 633 | logger.info("Reading from array from offset:" + offset); |
| 91 | |
try |
| 92 | |
{ |
| 93 | 633 | String charSetName = getTextEncodingCharSet(); |
| 94 | 633 | CharsetDecoder decoder = Charset.forName(charSetName).newDecoder(); |
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | 633 | logger.finest("Array length is:" + arr.length + "offset is:" + offset + "Size is:" + size); |
| 99 | |
|
| 100 | |
|
| 101 | 633 | if (arr.length - offset < size) |
| 102 | |
{ |
| 103 | 0 | throw new InvalidDataTypeException("byte array is to small to retrieve string of declared length:" + size); |
| 104 | |
} |
| 105 | 633 | String str = decoder.decode(ByteBuffer.wrap(arr, offset, size)).toString(); |
| 106 | 633 | if (str == null) |
| 107 | |
{ |
| 108 | 0 | throw new NullPointerException("String is null"); |
| 109 | |
} |
| 110 | 633 | value = str; |
| 111 | |
} |
| 112 | 0 | catch (CharacterCodingException ce) |
| 113 | |
{ |
| 114 | 0 | logger.severe(ce.getMessage()); |
| 115 | 0 | value = ""; |
| 116 | 633 | } |
| 117 | 633 | logger.info("Read StringFixedLength:" + value); |
| 118 | 633 | } |
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
public byte[] writeByteArray() |
| 130 | |
{ |
| 131 | |
ByteBuffer dataBuffer; |
| 132 | |
byte[] data; |
| 133 | |
|
| 134 | |
|
| 135 | 456 | if (value == null) |
| 136 | |
{ |
| 137 | 16 | logger.warning("Value of StringFixedlength Field is null using default value instead"); |
| 138 | 16 | data = new byte[size]; |
| 139 | 64 | for (int i = 0; i < size; i++) |
| 140 | |
{ |
| 141 | 48 | data[i] = ' '; |
| 142 | |
} |
| 143 | 16 | return data; |
| 144 | |
} |
| 145 | |
|
| 146 | |
try |
| 147 | |
{ |
| 148 | 440 | String charSetName = getTextEncodingCharSet(); |
| 149 | 440 | if (charSetName.equals(TextEncoding.CHARSET_UTF_16)) |
| 150 | |
{ |
| 151 | 0 | charSetName = TextEncoding.CHARSET_UTF_16_ENCODING_FORMAT; |
| 152 | 0 | CharsetEncoder encoder = Charset.forName(charSetName).newEncoder(); |
| 153 | |
|
| 154 | 0 | dataBuffer = encoder.encode(CharBuffer.wrap('\ufeff' + (String) value)); |
| 155 | 0 | } |
| 156 | |
else |
| 157 | |
{ |
| 158 | 440 | CharsetEncoder encoder = Charset.forName(charSetName).newEncoder(); |
| 159 | 440 | dataBuffer = encoder.encode(CharBuffer.wrap((String) value)); |
| 160 | |
} |
| 161 | |
} |
| 162 | 0 | catch (CharacterCodingException ce) |
| 163 | |
{ |
| 164 | 0 | logger.warning("There was a problem writing the following StringFixedlength Field:" + value + ":" + ce.getMessage() + "using default value instead"); |
| 165 | 0 | data = new byte[size]; |
| 166 | 0 | for (int i = 0; i < size; i++) |
| 167 | |
{ |
| 168 | 0 | data[i] = ' '; |
| 169 | |
} |
| 170 | 0 | return data; |
| 171 | 440 | } |
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | 440 | if (dataBuffer != null) |
| 176 | |
{ |
| 177 | |
|
| 178 | 440 | if (dataBuffer.limit() == size) |
| 179 | |
{ |
| 180 | 432 | data = new byte[dataBuffer.limit()]; |
| 181 | 432 | dataBuffer.get(data, 0, dataBuffer.limit()); |
| 182 | 432 | return data; |
| 183 | |
} |
| 184 | |
|
| 185 | 8 | else if (dataBuffer.limit() > size) |
| 186 | |
{ |
| 187 | 4 | logger.warning("There was a problem writing the following StringFixedlength Field:" + value + " when converted to bytes has length of:" + dataBuffer.limit() + " but field was defined with length of:" + size + " too long so stripping extra length"); |
| 188 | 4 | data = new byte[size]; |
| 189 | 4 | dataBuffer.get(data, 0, size); |
| 190 | 4 | return data; |
| 191 | |
} |
| 192 | |
|
| 193 | |
else |
| 194 | |
{ |
| 195 | 4 | logger.warning("There was a problem writing the following StringFixedlength Field:" + value + " when converted to bytes has length of:" + dataBuffer.limit() + " but field was defined with length of:" + size + " too short so padding with spaces to make up extra length"); |
| 196 | |
|
| 197 | 4 | data = new byte[size]; |
| 198 | 4 | dataBuffer.get(data, 0, dataBuffer.limit()); |
| 199 | |
|
| 200 | 8 | for (int i = dataBuffer.limit(); i < size; i++) |
| 201 | |
{ |
| 202 | 4 | data[i] = ' '; |
| 203 | |
} |
| 204 | 4 | return data; |
| 205 | |
} |
| 206 | |
} |
| 207 | |
else |
| 208 | |
{ |
| 209 | 0 | logger.warning("There was a serious problem writing the following StringFixedlength Field:" + value + ":" + "using default value instead"); |
| 210 | 0 | data = new byte[size]; |
| 211 | 0 | for (int i = 0; i < size; i++) |
| 212 | |
{ |
| 213 | 0 | data[i] = ' '; |
| 214 | |
} |
| 215 | 0 | return data; |
| 216 | |
} |
| 217 | |
} |
| 218 | |
|
| 219 | |
|
| 220 | |
|
| 221 | |
|
| 222 | |
protected String getTextEncodingCharSet() |
| 223 | |
{ |
| 224 | 61 | byte textEncoding = this.getBody().getTextEncoding(); |
| 225 | 61 | String charSetName = TextEncoding.getInstanceOf().getValueForId(textEncoding); |
| 226 | 61 | logger.finest("text encoding:" + textEncoding + " charset:" + charSetName); |
| 227 | 61 | return charSetName; |
| 228 | |
} |
| 229 | |
} |