View Javadoc

1   /*
2    * Entagged Audio Tag library
3    * Copyright (c) 2003-2005 Raphaël Slinckx <raphael@slinckx.net>
4    * 
5    * This library is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public
7    * License as published by the Free Software Foundation; either
8    * version 2.1 of the License, or (at your option) any later version.
9    *  
10   * This library is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   * Lesser General Public License for more details.
14   * 
15   * You should have received a copy of the GNU Lesser General Public
16   * License along with this library; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18   */
19  package entagged.audioformats.mp4.util;
20  
21  import java.io.UnsupportedEncodingException;
22  
23  import entagged.audioformats.generic.TagField;
24  import entagged.audioformats.generic.Utils;
25  
26  public class Mp4TagBinaryField extends Mp4TagField {
27      
28      protected byte[] dataBytes;
29      protected boolean isBinary = false;
30      
31      public Mp4TagBinaryField(String id) {
32          super(id);
33      }
34      
35      public Mp4TagBinaryField(String id, byte[] raw) throws UnsupportedEncodingException {
36          super(id, raw);
37      }
38  
39      public byte[] getRawContent() {
40          byte[] data = dataBytes;
41          byte[] b = new byte[4 + 4 + 4 + 4 + 4 + 4 + data.length];
42          
43          int offset = 0;
44  		Utils.copy(Utils.getSizeBigEndian(b.length), b, offset);
45  		offset += 4;
46  		
47  		Utils.copy(Utils.getDefaultBytes(getId()), b, offset);
48  		offset += 4;
49  		
50  		Utils.copy(Utils.getSizeBigEndian(4+4+4+4+data.length), b, offset);
51  		offset += 4;
52  		
53  		Utils.copy(Utils.getDefaultBytes("data"), b, offset);
54  		offset += 4;
55  		
56  		Utils.copy(new byte[] {0, 0, 0, (byte) (isBinary() ? 0 : 1) }, b, offset);
57  		offset += 4;
58  		
59  		Utils.copy(new byte[] {0, 0, 0, 0}, b, offset);
60  		offset += 4;
61  		
62  		Utils.copy(data, b, offset);
63  		offset += data.length;
64  		
65  		return b;
66      }
67      
68      protected void build(byte[] raw) {
69          int dataSize = Utils.getNumberBigEndian(raw, 0, 3);
70          
71          this.dataBytes = new byte[dataSize - 16];
72          for(int i = 16; i<dataSize; i++)
73              this.dataBytes[i-16] = raw[i];
74          
75          this.isBinary = ((raw[11]&0x01) == 0);
76      }
77      
78      public boolean isBinary() {
79          return isBinary;
80      }
81      
82      public boolean isEmpty() {
83          return this.dataBytes.length == 0;
84      }
85      
86      public byte[] getData() {
87          return this.dataBytes;
88      }
89      
90      public void setData(byte[] d) {
91          this.dataBytes = d;
92      }
93      
94      public void copyContent(TagField field) {
95          if(field instanceof Mp4TagBinaryField) {
96              this.dataBytes = ((Mp4TagBinaryField) field).getData();
97              this.isBinary = ((Mp4TagBinaryField) field).isBinary();
98          }
99      }
100 }