| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
package org.jaudiotagger.tag.mp4; |
| 20 | |
|
| 21 | |
import org.jaudiotagger.audio.generic.Utils; |
| 22 | |
import org.jaudiotagger.audio.mp4.atom.Mp4BoxHeader; |
| 23 | |
import org.jaudiotagger.tag.TagField; |
| 24 | |
import org.jaudiotagger.tag.mp4.atom.Mp4DataBox; |
| 25 | |
import org.jaudiotagger.tag.mp4.field.Mp4FieldType; |
| 26 | |
|
| 27 | |
import java.io.ByteArrayOutputStream; |
| 28 | |
import java.io.IOException; |
| 29 | |
import java.io.UnsupportedEncodingException; |
| 30 | |
import java.nio.ByteBuffer; |
| 31 | |
import java.util.logging.Logger; |
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
public abstract class Mp4TagField implements TagField |
| 44 | |
{ |
| 45 | |
|
| 46 | 4 | public static Logger logger = Logger.getLogger("org.jaudiotagger.tag.mp4"); |
| 47 | |
|
| 48 | |
|
| 49 | |
protected String id; |
| 50 | |
|
| 51 | |
|
| 52 | |
protected Mp4BoxHeader parentHeader; |
| 53 | |
|
| 54 | |
protected Mp4TagField(String id) |
| 55 | 7032 | { |
| 56 | 7032 | this.id = id; |
| 57 | 7032 | } |
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
protected Mp4TagField(ByteBuffer data) throws UnsupportedEncodingException |
| 66 | 0 | { |
| 67 | 0 | build(data); |
| 68 | 0 | } |
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
protected Mp4TagField(Mp4BoxHeader parentHeader, ByteBuffer data) throws UnsupportedEncodingException |
| 78 | 1790 | { |
| 79 | 1790 | this.parentHeader = parentHeader; |
| 80 | 1790 | build(data); |
| 81 | 1790 | } |
| 82 | |
|
| 83 | |
protected Mp4TagField(String id, ByteBuffer data) throws UnsupportedEncodingException |
| 84 | |
{ |
| 85 | 6076 | this(id); |
| 86 | 6076 | build(data); |
| 87 | 6076 | } |
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
public String getId() |
| 93 | |
{ |
| 94 | 30695 | return id; |
| 95 | |
} |
| 96 | |
|
| 97 | |
public void isBinary(boolean b) |
| 98 | |
{ |
| 99 | |
|
| 100 | 0 | } |
| 101 | |
|
| 102 | |
public boolean isCommon() |
| 103 | |
{ |
| 104 | 8402 | return id.equals(Mp4FieldKey.ARTIST.getFieldName()) || id.equals(Mp4FieldKey.ALBUM.getFieldName()) || id.equals(Mp4FieldKey.TITLE.getFieldName()) || id.equals(Mp4FieldKey.TRACK.getFieldName()) || id.equals(Mp4FieldKey.DAY.getFieldName()) || id.equals(Mp4FieldKey.COMMENT.getFieldName()) || id.equals(Mp4FieldKey.GENRE.getFieldName()); |
| 105 | |
} |
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
protected byte[] getIdBytes() |
| 111 | |
{ |
| 112 | 0 | return Utils.getDefaultBytes(getId(), "ISO-8859-1"); |
| 113 | |
} |
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
protected abstract byte[] getDataBytes() throws UnsupportedEncodingException; |
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
public abstract Mp4FieldType getFieldType(); |
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
protected abstract void build(ByteBuffer data) throws UnsupportedEncodingException; |
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
public byte[] getRawContent() throws UnsupportedEncodingException |
| 143 | |
{ |
| 144 | 2727 | logger.fine("Getting Raw data for:" + getId()); |
| 145 | |
try |
| 146 | |
{ |
| 147 | |
|
| 148 | 2727 | byte[] databox = getRawContentDataOnly(); |
| 149 | |
|
| 150 | |
|
| 151 | 2727 | ByteArrayOutputStream outerbaos = new ByteArrayOutputStream(); |
| 152 | 2727 | outerbaos.write(Utils.getSizeBEInt32(Mp4BoxHeader.HEADER_LENGTH + databox.length)); |
| 153 | 2727 | outerbaos.write(Utils.getDefaultBytes(getId(), "ISO-8859-1")); |
| 154 | 2727 | outerbaos.write(databox); |
| 155 | 2727 | return outerbaos.toByteArray(); |
| 156 | |
} |
| 157 | 0 | catch (IOException ioe) |
| 158 | |
{ |
| 159 | |
|
| 160 | 0 | throw new RuntimeException(ioe); |
| 161 | |
} |
| 162 | |
} |
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
public byte[] getRawContentDataOnly() throws UnsupportedEncodingException |
| 171 | |
{ |
| 172 | 2851 | logger.fine("Getting Raw data for:" + getId()); |
| 173 | |
try |
| 174 | |
{ |
| 175 | |
|
| 176 | 2851 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 177 | 2851 | byte[] data = getDataBytes(); |
| 178 | 2851 | baos.write(Utils.getSizeBEInt32(Mp4DataBox.DATA_HEADER_LENGTH + data.length)); |
| 179 | 2851 | baos.write(Utils.getDefaultBytes(Mp4DataBox.IDENTIFIER, "ISO-8859-1")); |
| 180 | 2851 | baos.write(new byte[]{0}); |
| 181 | 2851 | baos.write(new byte[]{0, 0, (byte) getFieldType().getFileClassId()}); |
| 182 | 2851 | baos.write(new byte[]{0, 0, 0, 0}); |
| 183 | 2851 | baos.write(data); |
| 184 | 2851 | return baos.toByteArray(); |
| 185 | |
} |
| 186 | 0 | catch (IOException ioe) |
| 187 | |
{ |
| 188 | |
|
| 189 | 0 | throw new RuntimeException(ioe); |
| 190 | |
} |
| 191 | |
} |
| 192 | |
} |