| 1 | |
package org.jaudiotagger.tag.mp4.field; |
| 2 | |
|
| 3 | |
import org.jaudiotagger.audio.generic.Utils; |
| 4 | |
import org.jaudiotagger.audio.mp4.atom.Mp4BoxHeader; |
| 5 | |
import org.jaudiotagger.tag.FieldDataInvalidException; |
| 6 | |
import org.jaudiotagger.tag.mp4.Mp4FieldKey; |
| 7 | |
import org.jaudiotagger.tag.mp4.atom.Mp4DataBox; |
| 8 | |
|
| 9 | |
import java.io.UnsupportedEncodingException; |
| 10 | |
import java.nio.ByteBuffer; |
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
public class Mp4TagByteField extends Mp4TagTextField |
| 18 | |
{ |
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
private int realDataLength; |
| 25 | |
|
| 26 | |
|
| 27 | |
private byte[] bytedata; |
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
public Mp4TagByteField(Mp4FieldKey id, String value) throws FieldDataInvalidException |
| 38 | |
{ |
| 39 | 0 | this(id, value, 1); |
| 40 | 0 | } |
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
public Mp4TagByteField(Mp4FieldKey id, String value, int realDataLength) throws FieldDataInvalidException |
| 49 | |
{ |
| 50 | 9 | super(id.getFieldName(), value); |
| 51 | 9 | this.realDataLength = realDataLength; |
| 52 | |
|
| 53 | |
|
| 54 | |
try |
| 55 | |
{ |
| 56 | 9 | Long.parseLong(value); |
| 57 | |
} |
| 58 | 0 | catch (NumberFormatException nfe) |
| 59 | |
{ |
| 60 | 0 | throw new FieldDataInvalidException("Value of:" + value + " is invalid for field:" + id); |
| 61 | 9 | } |
| 62 | 9 | } |
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
public Mp4TagByteField(String id, ByteBuffer raw) throws UnsupportedEncodingException |
| 72 | |
{ |
| 73 | 331 | super(id, raw); |
| 74 | 331 | } |
| 75 | |
|
| 76 | |
public Mp4FieldType getFieldType() |
| 77 | |
{ |
| 78 | 164 | return Mp4FieldType.BYTE; |
| 79 | |
} |
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
protected byte[] getDataBytes() throws UnsupportedEncodingException |
| 90 | |
{ |
| 91 | |
|
| 92 | |
|
| 93 | 164 | if (bytedata != null) |
| 94 | |
{ |
| 95 | 155 | return bytedata; |
| 96 | |
} |
| 97 | |
|
| 98 | |
|
| 99 | 9 | switch (realDataLength) |
| 100 | |
{ |
| 101 | |
case 2: |
| 102 | |
{ |
| 103 | |
|
| 104 | 8 | Short shortValue = new Short(content); |
| 105 | 8 | byte rawData[] = Utils.getSizeBEInt16(shortValue); |
| 106 | 8 | return rawData; |
| 107 | |
} |
| 108 | |
case 1: |
| 109 | |
{ |
| 110 | |
|
| 111 | 1 | Short shortValue = new Short(content); |
| 112 | 1 | byte rawData[] = new byte[1]; |
| 113 | 1 | rawData[0] = shortValue.byteValue(); |
| 114 | 1 | return rawData; |
| 115 | |
} |
| 116 | |
case 4: |
| 117 | |
{ |
| 118 | |
|
| 119 | 0 | Integer intValue = new Integer(content); |
| 120 | 0 | byte rawData[] = Utils.getSizeBEInt32(intValue); |
| 121 | 0 | return rawData; |
| 122 | |
} |
| 123 | |
default: |
| 124 | |
{ |
| 125 | |
|
| 126 | 0 | throw new RuntimeException(id + ":" + realDataLength + ":" + "Dont know how to write byte fields of this length"); |
| 127 | |
} |
| 128 | |
} |
| 129 | |
|
| 130 | |
} |
| 131 | |
|
| 132 | |
protected void build(ByteBuffer data) throws UnsupportedEncodingException |
| 133 | |
{ |
| 134 | |
|
| 135 | 331 | Mp4BoxHeader header = new Mp4BoxHeader(data); |
| 136 | 331 | Mp4DataBox databox = new Mp4DataBox(header, data); |
| 137 | 331 | dataSize = header.getDataLength(); |
| 138 | |
|
| 139 | 331 | realDataLength = dataSize - Mp4DataBox.PRE_DATA_LENGTH; |
| 140 | 331 | bytedata = databox.getByteData(); |
| 141 | 331 | content = databox.getContent(); |
| 142 | |
|
| 143 | 331 | } |
| 144 | |
} |