View Javadoc

1   /*
2    * Entagged Audio Tag library
3    * Copyright (c) 2004-2005 Christian Laireiter <liree@web.de>
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.asf.data;
20  
21  import java.io.ByteArrayOutputStream;
22  import java.math.BigInteger;
23  
24  import entagged.audioformats.asf.util.Utils;
25  
26  /***
27   * This class represents the data of a chunk which contains title, author,
28   * copyright, description and the rating of the file. <br>
29   * It is optional whithin asf files. But if exists only once.
30   * 
31   * @author Christian Laireiter
32   */
33  public class ContentDescription extends Chunk {
34  
35      /***
36       * File artist.
37       */
38      private String author = null;
39  
40      /***
41       * File copyright.
42       */
43      private String copyRight = null;
44  
45      /***
46       * File comment.
47       */
48      private String description = null;
49  
50      /***
51       * File rating.
52       */
53      private String rating = null;
54  
55      /***
56       * File title.
57       */
58      private String title = null;
59  
60      /***
61       * Creates an instance. <br>
62       */
63      public ContentDescription() {
64          this(0, BigInteger.valueOf(0));
65      }
66  
67      /***
68       * Creates an instance.
69       * 
70       * @param pos
71       *                   Position of content description within file or stream
72       * @param chunkLen
73       *                   Length of content description.
74       */
75      public ContentDescription(long pos, BigInteger chunkLen) {
76          super(GUID.GUID_CONTENTDESCRIPTION, pos, chunkLen);
77      }
78  
79      /***
80       * @return Returns the author.
81       */
82      public String getAuthor() {
83          if (author == null)
84              return "";
85          return author;
86      }
87  
88      /***
89       * This method creates a byte array that could directly be written to an asf
90       * file. <br>
91       * 
92       * @return The asf chunk representation of a content description with the
93       *               values of the current object.
94       */
95      public byte[] getBytes() {
96          ByteArrayOutputStream result = new ByteArrayOutputStream();
97          try {
98              ByteArrayOutputStream tags = new ByteArrayOutputStream();
99              String[] toWrite = new String[] { getTitle(), getAuthor(),
100                     getCopyRight(), getComment(), getRating() };
101             byte[][] stringRepresentations = new byte[toWrite.length][];
102             // Create byte[] of UTF-16LE encodings
103             for (int i = 0; i < toWrite.length; i++) {
104                 stringRepresentations[i] = toWrite[i].getBytes("UTF-16LE");
105             }
106             // Write the amount of bytes needed to store the values.
107             for (int i = 0; i < stringRepresentations.length; i++) {
108                 tags.write(Utils.getBytes(stringRepresentations[i].length + 2,
109                         2));
110             }
111             // Write the values themselves.
112             for (int i = 0; i < toWrite.length; i++) {
113                 tags.write(stringRepresentations[i]);
114                 // Zero term character.
115                 tags.write(Utils.getBytes(0, 2));
116             }
117             // Now tags has got the values. The result just needs
118             // The GUID, length of the chunk and the tags.
119             byte[] tagContent = tags.toByteArray();
120             // The guid of the chunk
121             result.write(GUID.GUID_CONTENTDESCRIPTION.getBytes());
122             /*
123              * The length of the chunk. 16 Bytes guid 8 Bytes the length
124              * tagContent.length bytes.
125              */
126             result.write(Utils.getBytes(tagContent.length + 24, 8));
127             // The tags.
128             result.write(tagContent);
129         } catch (Exception e) {
130             e.printStackTrace();
131         }
132         return result.toByteArray();
133     }
134 
135     /***
136      * @return Returns the comment.
137      */
138     public String getComment() {
139         if (description == null)
140             return "";
141         return description;
142     }
143 
144     /***
145      * @return Returns the copyRight.
146      */
147     public String getCopyRight() {
148         if (copyRight == null)
149             return "";
150         return copyRight;
151     }
152 
153     /***
154      * @return returns the rating.
155      */
156     public String getRating() {
157         if (rating == null)
158             return "";
159         return rating;
160     }
161 
162     /***
163      * @return Returns the title.
164      */
165     public String getTitle() {
166         if (title == null)
167             return "";
168         return title;
169     }
170 
171     /***
172      * (overridden)
173      * 
174      * @see entagged.audioformats.asf.data.Chunk#prettyPrint()
175      */
176     public String prettyPrint() {
177         StringBuffer result = new StringBuffer(super.prettyPrint());
178         result.insert(0, Utils.LINE_SEPARATOR + "Content Description:"
179                 + Utils.LINE_SEPARATOR);
180         result.append("   Title      : " + getTitle() + Utils.LINE_SEPARATOR);
181         result.append("   Author     : " + getAuthor() + Utils.LINE_SEPARATOR);
182         result.append("   Copyright  : " + getCopyRight()
183                 + Utils.LINE_SEPARATOR);
184         result.append("   Description: " + getComment() + Utils.LINE_SEPARATOR);
185         result.append("   Rating     :" + getRating() + Utils.LINE_SEPARATOR);
186         return result.toString();
187     }
188 
189     /***
190      * @param fileAuthor
191      *                   The author to set.
192      * @throws IllegalArgumentException
193      *                    If "UTF-16LE"-byte-representation would take more than 65535
194      *                    bytes.
195      */
196     public void setAuthor(String fileAuthor) throws IllegalArgumentException {
197         Utils.checkStringLengthNullSafe(fileAuthor);
198         this.author = fileAuthor;
199     }
200 
201     /***
202      * @param tagComment
203      *                   The comment to set.
204      * @throws IllegalArgumentException
205      *                    If "UTF-16LE"-byte-representation would take more than 65535
206      *                    bytes.
207      */
208     public void setComment(String tagComment) throws IllegalArgumentException {
209         Utils.checkStringLengthNullSafe(tagComment);
210         this.description = tagComment;
211     }
212 
213     /***
214      * @param cpright
215      *                   The copyRight to set.
216      * @throws IllegalArgumentException
217      *                    If "UTF-16LE"-byte-representation would take more than 65535
218      *                    bytes.
219      */
220     public void setCopyRight(String cpright) throws IllegalArgumentException {
221         Utils.checkStringLengthNullSafe(cpright);
222         this.copyRight = cpright;
223     }
224 
225     /***
226      * @param ratingText
227      *                   The rating to be set.
228      * @throws IllegalArgumentException
229      *                    If "UTF-16LE"-byte-representation would take more than 65535
230      *                    bytes.
231      */
232     public void setRating(String ratingText) throws IllegalArgumentException {
233         Utils.checkStringLengthNullSafe(ratingText);
234         this.rating = ratingText;
235     }
236 
237     /***
238      * @param songTitle
239      *                   The title to set.
240      * @throws IllegalArgumentException
241      *                    If "UTF-16LE"-byte-representation would take more than 65535
242      *                    bytes.
243      */
244     public void setTitle(String songTitle) throws IllegalArgumentException {
245         Utils.checkStringLengthNullSafe(songTitle);
246         this.title = songTitle;
247     }
248 }