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