| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
package org.jaudiotagger.audio.asf.data; |
| 20 | |
|
| 21 | |
import org.jaudiotagger.audio.asf.io.WriteableChunk; |
| 22 | |
import org.jaudiotagger.audio.asf.util.Utils; |
| 23 | |
|
| 24 | |
import java.io.IOException; |
| 25 | |
import java.io.OutputStream; |
| 26 | |
import java.math.BigInteger; |
| 27 | |
import java.util.*; |
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | 5 | public class ExtendedContentDescription extends Chunk implements WriteableChunk |
| 38 | |
{ |
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
private final Map<String, List<ContentDescriptor>> descriptors; |
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
public ExtendedContentDescription() |
| 49 | |
{ |
| 50 | 13 | this(BigInteger.valueOf(0)); |
| 51 | 13 | } |
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
public ExtendedContentDescription(BigInteger chunkLen) |
| 59 | |
{ |
| 60 | 61 | super(GUID.GUID_EXTENDED_CONTENT_DESCRIPTION, chunkLen); |
| 61 | 61 | this.descriptors = new LinkedHashMap<String, List<ContentDescriptor>>(); |
| 62 | 61 | } |
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
public void addDescriptor(ContentDescriptor toAdd) |
| 70 | |
{ |
| 71 | 1628 | assert toAdd != null : "Argument must not be null."; |
| 72 | 1628 | List<ContentDescriptor> list = getDescriptors(toAdd.getName()); |
| 73 | 1628 | if (list == null) |
| 74 | |
{ |
| 75 | 1628 | list = new ArrayList<ContentDescriptor>(); |
| 76 | 1628 | this.descriptors.put(toAdd.getName(), list); |
| 77 | |
} |
| 78 | 1628 | list.add(toAdd); |
| 79 | 1628 | } |
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
public void addOrReplace(ContentDescriptor descriptor) |
| 87 | |
{ |
| 88 | 365 | assert descriptor != null : "Argument must not be null"; |
| 89 | 365 | remove(descriptor.getName()); |
| 90 | 365 | addDescriptor(descriptor); |
| 91 | 365 | } |
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
public boolean containsDescriptor(final String fieldName) |
| 100 | |
{ |
| 101 | 365 | return this.descriptors.containsKey(fieldName); |
| 102 | |
} |
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
public long getCurrentAsfChunkSize() |
| 108 | |
{ |
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | 13 | long result = 26; |
| 113 | 13 | for (ContentDescriptor curr : getDescriptors()) |
| 114 | |
{ |
| 115 | 365 | result += curr.getCurrentAsfSize(); |
| 116 | |
} |
| 117 | 13 | return result; |
| 118 | |
} |
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
public int getDescriptorCount() |
| 124 | |
{ |
| 125 | 21 | int result = 0; |
| 126 | 21 | for (List<ContentDescriptor> curr : this.descriptors.values()) |
| 127 | |
{ |
| 128 | 720 | result += curr.size(); |
| 129 | |
} |
| 130 | 21 | return result; |
| 131 | |
} |
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
public List<ContentDescriptor> getDescriptors() |
| 140 | |
{ |
| 141 | 56 | final ArrayList<ContentDescriptor> result = new ArrayList<ContentDescriptor>(); |
| 142 | 56 | for (List<ContentDescriptor> curr : this.descriptors.values()) |
| 143 | |
{ |
| 144 | 1585 | result.addAll(curr); |
| 145 | |
} |
| 146 | 56 | return result; |
| 147 | |
} |
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
public List<ContentDescriptor> getDescriptors(String name) |
| 156 | |
{ |
| 157 | 1658 | return this.descriptors.get(name); |
| 158 | |
} |
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
public boolean isEmpty() |
| 164 | |
{ |
| 165 | 21 | return getDescriptorCount() == 0; |
| 166 | |
} |
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
public String prettyPrint(final String prefix) |
| 172 | |
{ |
| 173 | 0 | StringBuffer result = new StringBuffer(super.prettyPrint(prefix)); |
| 174 | 0 | List<ContentDescriptor> list = getDescriptors(); |
| 175 | 0 | Collections.sort(list); |
| 176 | 0 | for (ContentDescriptor curr : list) |
| 177 | |
{ |
| 178 | 0 | result.append(prefix + " |-> "); |
| 179 | 0 | result.append(curr); |
| 180 | 0 | result.append(Utils.LINE_SEPARATOR); |
| 181 | |
} |
| 182 | 0 | return result.toString(); |
| 183 | |
} |
| 184 | |
|
| 185 | |
|
| 186 | |
|
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
public List<ContentDescriptor> remove(String id) |
| 192 | |
{ |
| 193 | 398 | return this.descriptors.remove(id); |
| 194 | |
} |
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
public long writeInto(OutputStream out) throws IOException |
| 200 | |
{ |
| 201 | 13 | final long chunkSize = getCurrentAsfChunkSize(); |
| 202 | 13 | final List<ContentDescriptor> descriptorList = getDescriptors(); |
| 203 | 13 | out.write(getGuid().getBytes()); |
| 204 | 13 | Utils.writeUINT64(chunkSize, out); |
| 205 | 13 | Utils.writeUINT16(descriptorList.size(), out); |
| 206 | 13 | for (ContentDescriptor curr : descriptorList) |
| 207 | |
{ |
| 208 | 365 | curr.writeInto(out); |
| 209 | |
} |
| 210 | 13 | return chunkSize; |
| 211 | |
} |
| 212 | |
} |