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.TagTextField;
25  import entagged.audioformats.generic.Utils;
26  
27  public class Mp4TagTextField extends Mp4TagField implements TagTextField {
28  
29  	protected String content;
30  
31  	public Mp4TagTextField(String id, byte[] raw)
32  			throws UnsupportedEncodingException {
33  		super(id, raw);
34  	}
35  
36  	public Mp4TagTextField(String id, String content) {
37  		super(id);
38  		this.content = content;
39  	}
40  
41  	protected void build(byte[] raw) throws UnsupportedEncodingException {
42  		int dataSize = Utils.getNumberBigEndian(raw, 0, 3);
43  		this.content = Utils
44  				.getString(raw, 16, dataSize - 8 - 8, getEncoding());
45  	}
46  
47  
48  	public void copyContent(TagField field) {
49  		if (field instanceof Mp4TagTextField) {
50  			this.content = ((Mp4TagTextField) field).getContent();
51  		}
52  	}
53  
54  	public String getContent() {
55  		return content;
56  	}
57  
58  	// This is overriden in the number data box
59  	protected byte[] getDataBytes() throws UnsupportedEncodingException {
60  		return content.getBytes(getEncoding());
61  	}
62  
63  	public String getEncoding() {
64  		return "ISO-8859-1";
65  	}
66  
67  	public byte[] getRawContent() throws UnsupportedEncodingException {
68  		byte[] data = getDataBytes();
69  		byte[] b = new byte[4 + 4 + 4 + 4 + 4 + 4 + data.length];
70  
71  		int offset = 0;
72  		Utils.copy(Utils.getSizeBigEndian(b.length), b, offset);
73  		offset += 4;
74  
75  		Utils.copy(Utils.getDefaultBytes(getId()), b, offset);
76  		offset += 4;
77  
78  		Utils.copy(Utils.getSizeBigEndian(4 + 4 + 4 + 4 + data.length), b,
79  				offset);
80  		offset += 4;
81  
82  		Utils.copy(Utils.getDefaultBytes("data"), b, offset);
83  		offset += 4;
84  
85  		Utils.copy(new byte[] { 0, 0, 0, (byte) (isBinary() ? 0 : 1) }, b,
86  				offset);
87  		offset += 4;
88  
89  		Utils.copy(new byte[] { 0, 0, 0, 0 }, b, offset);
90  		offset += 4;
91  
92  		Utils.copy(data, b, offset);
93  		offset += data.length;
94  
95  		return b;
96  	}
97  
98  	public boolean isBinary() {
99  		return false;
100 	}
101 
102 	public boolean isEmpty() {
103 		return this.content.trim().equals("");
104 	}
105 
106 	public void setContent(String s) {
107 		this.content = s;
108 	}
109 
110 	public void setEncoding(String s) {
111 		/* Not allowed */
112 	}
113 
114 	public String toString() {
115 		return content;
116 	}
117 }