| 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.logging.ErrorMessage; |
| 6 | |
import org.jaudiotagger.tag.TagField; |
| 7 | |
import org.jaudiotagger.tag.TagTextField; |
| 8 | |
import org.jaudiotagger.tag.mp4.Mp4FieldKey; |
| 9 | |
import org.jaudiotagger.tag.mp4.Mp4TagField; |
| 10 | |
import org.jaudiotagger.tag.mp4.atom.Mp4DataBox; |
| 11 | |
import org.jaudiotagger.tag.mp4.atom.Mp4MeanBox; |
| 12 | |
import org.jaudiotagger.tag.mp4.atom.Mp4NameBox; |
| 13 | |
|
| 14 | |
import java.io.ByteArrayOutputStream; |
| 15 | |
import java.io.IOException; |
| 16 | |
import java.io.UnsupportedEncodingException; |
| 17 | |
import java.nio.ByteBuffer; |
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
public class Mp4TagReverseDnsField extends Mp4TagField implements TagTextField |
| 34 | |
{ |
| 35 | |
public static final String IDENTIFIER = "----"; |
| 36 | |
|
| 37 | |
protected int dataSize; |
| 38 | |
|
| 39 | |
|
| 40 | |
private String issuer; |
| 41 | |
|
| 42 | |
|
| 43 | |
private String descriptor; |
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
protected String content; |
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
public Mp4TagReverseDnsField(Mp4BoxHeader parentHeader, ByteBuffer data) throws UnsupportedEncodingException |
| 57 | |
{ |
| 58 | 1790 | super(parentHeader, data); |
| 59 | 1790 | } |
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
public Mp4TagReverseDnsField(Mp4FieldKey id, String content) |
| 68 | |
{ |
| 69 | 294 | super(id.getFieldName()); |
| 70 | 294 | this.issuer = id.getIssuer(); |
| 71 | 294 | this.descriptor = id.getIdentifier(); |
| 72 | 294 | this.content = content; |
| 73 | 294 | } |
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
public Mp4TagReverseDnsField(final String fieldName, final String issuer, final String identifier, final String content) |
| 83 | |
{ |
| 84 | 0 | super(fieldName); |
| 85 | 0 | this.issuer = issuer; |
| 86 | 0 | this.descriptor = identifier; |
| 87 | 0 | this.content = content; |
| 88 | 0 | } |
| 89 | |
|
| 90 | |
public Mp4FieldType getFieldType() |
| 91 | |
{ |
| 92 | |
|
| 93 | |
|
| 94 | 866 | return Mp4FieldType.TEXT; |
| 95 | |
} |
| 96 | |
|
| 97 | |
protected void build(ByteBuffer data) throws UnsupportedEncodingException |
| 98 | |
{ |
| 99 | |
|
| 100 | 1790 | Mp4BoxHeader meanBoxHeader = new Mp4BoxHeader(data); |
| 101 | 1790 | Mp4MeanBox meanBox = new Mp4MeanBox(meanBoxHeader, data); |
| 102 | 1790 | setIssuer(meanBox.getIssuer()); |
| 103 | 1790 | data.position(data.position() + meanBoxHeader.getDataLength()); |
| 104 | |
|
| 105 | |
|
| 106 | 1790 | Mp4BoxHeader nameBoxHeader = new Mp4BoxHeader(data); |
| 107 | 1790 | Mp4NameBox nameBox = new Mp4NameBox(nameBoxHeader, data); |
| 108 | 1790 | setDescriptor(nameBox.getName()); |
| 109 | 1790 | data.position(data.position() + nameBoxHeader.getDataLength()); |
| 110 | |
|
| 111 | |
|
| 112 | 1790 | if (parentHeader.getDataLength() == meanBoxHeader.getLength() + nameBoxHeader.getLength()) |
| 113 | |
{ |
| 114 | 5 | id = IDENTIFIER + ":" + issuer + ":" + descriptor; |
| 115 | 5 | setContent(""); |
| 116 | 5 | logger.warning(ErrorMessage.MP4_REVERSE_DNS_FIELD_HAS_NO_DATA.getMsg(id)); |
| 117 | |
} |
| 118 | |
|
| 119 | |
else |
| 120 | |
{ |
| 121 | |
|
| 122 | 1785 | Mp4BoxHeader dataBoxHeader = new Mp4BoxHeader(data); |
| 123 | 1785 | Mp4DataBox dataBox = new Mp4DataBox(dataBoxHeader, data); |
| 124 | 1785 | setContent(dataBox.getContent()); |
| 125 | 1785 | data.position(data.position() + dataBoxHeader.getDataLength()); |
| 126 | |
|
| 127 | |
|
| 128 | 1785 | id = IDENTIFIER + ":" + issuer + ":" + descriptor; |
| 129 | |
} |
| 130 | 1790 | } |
| 131 | |
|
| 132 | |
|
| 133 | |
public void copyContent(TagField field) |
| 134 | |
{ |
| 135 | 0 | if (field instanceof Mp4TagReverseDnsField) |
| 136 | |
{ |
| 137 | 0 | this.issuer = ((Mp4TagReverseDnsField) field).getIssuer(); |
| 138 | 0 | this.descriptor = ((Mp4TagReverseDnsField) field).getDescriptor(); |
| 139 | 0 | this.content = ((Mp4TagReverseDnsField) field).getContent(); |
| 140 | |
} |
| 141 | 0 | } |
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
public String getContent() |
| 147 | |
{ |
| 148 | 60 | return content; |
| 149 | |
} |
| 150 | |
|
| 151 | |
|
| 152 | |
protected byte[] getDataBytes() throws UnsupportedEncodingException |
| 153 | |
{ |
| 154 | 0 | return content.getBytes(getEncoding()); |
| 155 | |
} |
| 156 | |
|
| 157 | |
public String getEncoding() |
| 158 | |
{ |
| 159 | 2600 | return Mp4BoxHeader.CHARSET_UTF_8; |
| 160 | |
} |
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
public byte[] getRawContent() throws UnsupportedEncodingException |
| 169 | |
{ |
| 170 | |
try |
| 171 | |
{ |
| 172 | 867 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 173 | |
|
| 174 | |
|
| 175 | 867 | byte[] issuerRawData = issuer.getBytes(getEncoding()); |
| 176 | 867 | baos.write(Utils.getSizeBEInt32(Mp4BoxHeader.HEADER_LENGTH + Mp4MeanBox.PRE_DATA_LENGTH + issuerRawData.length)); |
| 177 | 867 | baos.write(Utils.getDefaultBytes(Mp4MeanBox.IDENTIFIER, "ISO-8859-1")); |
| 178 | 867 | baos.write(new byte[]{0, 0, 0, 0}); |
| 179 | 867 | baos.write(issuerRawData); |
| 180 | |
|
| 181 | |
|
| 182 | 867 | byte[] nameRawData = descriptor.getBytes(getEncoding()); |
| 183 | 867 | baos.write(Utils.getSizeBEInt32(Mp4BoxHeader.HEADER_LENGTH + Mp4NameBox.PRE_DATA_LENGTH + nameRawData.length)); |
| 184 | 867 | baos.write(Utils.getDefaultBytes(Mp4NameBox.IDENTIFIER, "ISO-8859-1")); |
| 185 | 867 | baos.write(new byte[]{0, 0, 0, 0}); |
| 186 | 867 | baos.write(nameRawData); |
| 187 | |
|
| 188 | |
|
| 189 | 867 | if (content.length() > 0) |
| 190 | |
{ |
| 191 | 866 | baos.write(getRawContentDataOnly()); |
| 192 | |
} |
| 193 | |
|
| 194 | 867 | ByteArrayOutputStream outerbaos = new ByteArrayOutputStream(); |
| 195 | 867 | outerbaos.write(Utils.getSizeBEInt32(Mp4BoxHeader.HEADER_LENGTH + baos.size())); |
| 196 | 867 | outerbaos.write(Utils.getDefaultBytes(IDENTIFIER, "ISO-8859-1")); |
| 197 | 867 | outerbaos.write(baos.toByteArray()); |
| 198 | 867 | return outerbaos.toByteArray(); |
| 199 | |
|
| 200 | |
} |
| 201 | 0 | catch (IOException ioe) |
| 202 | |
{ |
| 203 | |
|
| 204 | 0 | throw new RuntimeException(ioe); |
| 205 | |
} |
| 206 | |
} |
| 207 | |
|
| 208 | |
public byte[] getRawContentDataOnly() throws UnsupportedEncodingException |
| 209 | |
{ |
| 210 | 866 | logger.fine("Getting Raw data for:" + getId()); |
| 211 | |
try |
| 212 | |
{ |
| 213 | |
|
| 214 | 866 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 215 | 866 | byte[] dataRawData = content.getBytes(getEncoding()); |
| 216 | 866 | baos.write(Utils.getSizeBEInt32(Mp4BoxHeader.HEADER_LENGTH + Mp4DataBox.PRE_DATA_LENGTH + dataRawData.length)); |
| 217 | 866 | baos.write(Utils.getDefaultBytes(Mp4DataBox.IDENTIFIER, "ISO-8859-1")); |
| 218 | 866 | baos.write(new byte[]{0}); |
| 219 | 866 | baos.write(new byte[]{0, 0, (byte) getFieldType().getFileClassId()}); |
| 220 | 866 | baos.write(new byte[]{0, 0, 0, 0}); |
| 221 | 866 | baos.write(dataRawData); |
| 222 | 866 | return baos.toByteArray(); |
| 223 | |
} |
| 224 | 0 | catch (IOException ioe) |
| 225 | |
{ |
| 226 | |
|
| 227 | 0 | throw new RuntimeException(ioe); |
| 228 | |
} |
| 229 | |
} |
| 230 | |
|
| 231 | |
public boolean isBinary() |
| 232 | |
{ |
| 233 | 0 | return false; |
| 234 | |
} |
| 235 | |
|
| 236 | |
public boolean isEmpty() |
| 237 | |
{ |
| 238 | 0 | return this.content.trim().equals(""); |
| 239 | |
} |
| 240 | |
|
| 241 | |
public void setContent(String s) |
| 242 | |
{ |
| 243 | 1790 | this.content = s; |
| 244 | 1790 | } |
| 245 | |
|
| 246 | |
public void setEncoding(String s) |
| 247 | |
{ |
| 248 | |
|
| 249 | 0 | } |
| 250 | |
|
| 251 | |
public String toString() |
| 252 | |
{ |
| 253 | 1820 | return content; |
| 254 | |
} |
| 255 | |
|
| 256 | |
|
| 257 | |
|
| 258 | |
|
| 259 | |
public String getIssuer() |
| 260 | |
{ |
| 261 | 60 | return issuer; |
| 262 | |
} |
| 263 | |
|
| 264 | |
|
| 265 | |
|
| 266 | |
|
| 267 | |
public String getDescriptor() |
| 268 | |
{ |
| 269 | 60 | return descriptor; |
| 270 | |
} |
| 271 | |
|
| 272 | |
|
| 273 | |
|
| 274 | |
|
| 275 | |
|
| 276 | |
|
| 277 | |
public void setIssuer(String issuer) |
| 278 | |
{ |
| 279 | 1790 | this.issuer = issuer; |
| 280 | 1790 | } |
| 281 | |
|
| 282 | |
|
| 283 | |
|
| 284 | |
|
| 285 | |
|
| 286 | |
|
| 287 | |
public void setDescriptor(String descriptor) |
| 288 | |
{ |
| 289 | 1790 | this.descriptor = descriptor; |
| 290 | 1790 | } |
| 291 | |
} |