PageRenderTime 76ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/dependencies/jaudiotagger/src/main/java/org/jaudiotagger/tag/lyrics3/Lyrics3v2Field.java

http://github.com/tulskiy/musique
Java | 216 lines | 128 code | 15 blank | 73 comment | 52 complexity | f9ee20c7d825e03f7b9706f49fd2bab9 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-3.0, LGPL-2.1, BSD-3-Clause
  1. /**
  2. * @author : Paul Taylor
  3. * @author : Eric Farng
  4. *
  5. * Version @version:$Id: Lyrics3v2Field.java 836 2009-11-12 15:44:07Z paultaylor $
  6. *
  7. * MusicTag Copyright (C)2003,2004
  8. *
  9. * This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser
  10. * General Public License as published by the Free Software Foundation; either version 2.1 of the License,
  11. * or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
  14. * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. * See the GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License along with this library; if not,
  18. * you can get a copy from http://www.opensource.org/licenses/lgpl-license.php or write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. * Description:
  22. */
  23. package org.jaudiotagger.tag.lyrics3;
  24. import org.jaudiotagger.tag.InvalidTagException;
  25. import org.jaudiotagger.tag.TagException;
  26. import org.jaudiotagger.tag.TagOptionSingleton;
  27. import org.jaudiotagger.tag.id3.AbstractID3v2Frame;
  28. import org.jaudiotagger.tag.id3.AbstractTagFrame;
  29. import org.jaudiotagger.tag.id3.framebody.AbstractFrameBodyTextInfo;
  30. import org.jaudiotagger.tag.id3.framebody.FrameBodyCOMM;
  31. import org.jaudiotagger.tag.id3.framebody.FrameBodySYLT;
  32. import org.jaudiotagger.tag.id3.framebody.FrameBodyUSLT;
  33. import java.io.IOException;
  34. import java.io.RandomAccessFile;
  35. import java.nio.ByteBuffer;
  36. public class Lyrics3v2Field extends AbstractTagFrame {
  37. /**
  38. * Creates a new Lyrics3v2Field datatype.
  39. */
  40. public Lyrics3v2Field() {
  41. }
  42. public Lyrics3v2Field(Lyrics3v2Field copyObject) {
  43. super(copyObject);
  44. }
  45. /**
  46. * Creates a new Lyrics3v2Field datatype.
  47. *
  48. * @param body
  49. */
  50. public Lyrics3v2Field(AbstractLyrics3v2FieldFrameBody body) {
  51. this.frameBody = body;
  52. }
  53. /**
  54. * Creates a new Lyrics3v2Field datatype.
  55. *
  56. * @param frame
  57. * @throws TagException
  58. */
  59. public Lyrics3v2Field(AbstractID3v2Frame frame) throws TagException {
  60. AbstractFrameBodyTextInfo textFrame;
  61. String text;
  62. String frameIdentifier = frame.getIdentifier();
  63. if (frameIdentifier.startsWith("USLT")) {
  64. frameBody = new FieldFrameBodyLYR("");
  65. ((FieldFrameBodyLYR) frameBody).addLyric((FrameBodyUSLT) frame.getBody());
  66. } else if (frameIdentifier.startsWith("SYLT")) {
  67. frameBody = new FieldFrameBodyLYR("");
  68. ((FieldFrameBodyLYR) frameBody).addLyric((FrameBodySYLT) frame.getBody());
  69. } else if (frameIdentifier.startsWith("COMM")) {
  70. text = ((FrameBodyCOMM) frame.getBody()).getText();
  71. frameBody = new FieldFrameBodyINF(text);
  72. } else if (frameIdentifier.equals("TCOM")) {
  73. textFrame = (AbstractFrameBodyTextInfo) frame.getBody();
  74. frameBody = new FieldFrameBodyAUT("");
  75. if ((textFrame != null) && (textFrame.getText().length() > 0)) {
  76. frameBody = new FieldFrameBodyAUT(textFrame.getText());
  77. }
  78. } else if (frameIdentifier.equals("TALB")) {
  79. textFrame = (AbstractFrameBodyTextInfo) frame.getBody();
  80. if ((textFrame != null) && (textFrame.getText().length() > 0)) {
  81. frameBody = new FieldFrameBodyEAL(textFrame.getText());
  82. }
  83. } else if (frameIdentifier.equals("TPE1")) {
  84. textFrame = (AbstractFrameBodyTextInfo) frame.getBody();
  85. if ((textFrame != null) && (textFrame.getText().length() > 0)) {
  86. frameBody = new FieldFrameBodyEAR(textFrame.getText());
  87. }
  88. } else if (frameIdentifier.equals("TIT2")) {
  89. textFrame = (AbstractFrameBodyTextInfo) frame.getBody();
  90. if ((textFrame != null) && (textFrame.getText().length() > 0)) {
  91. frameBody = new FieldFrameBodyETT(textFrame.getText());
  92. }
  93. } else {
  94. throw new TagException("Cannot createField Lyrics3v2 field from given ID3v2 frame");
  95. }
  96. }
  97. /**
  98. * Creates a new Lyrics3v2Field datatype.
  99. *
  100. * @param file
  101. * @param byteBuffer
  102. * @throws InvalidTagException
  103. */
  104. public Lyrics3v2Field(ByteBuffer byteBuffer) throws InvalidTagException {
  105. this.read(byteBuffer);
  106. }
  107. /**
  108. * @return
  109. */
  110. public String getIdentifier() {
  111. if (frameBody == null) {
  112. return "";
  113. }
  114. return frameBody.getIdentifier();
  115. }
  116. /**
  117. * @return
  118. */
  119. public int getSize() {
  120. return frameBody.getSize() + 5 + getIdentifier().length();
  121. }
  122. /**
  123. * @param byteBuffer
  124. * @throws InvalidTagException
  125. * @throws IOException
  126. */
  127. public void read(ByteBuffer byteBuffer) throws InvalidTagException {
  128. byte[] buffer = new byte[6];
  129. // lets scan for a non-zero byte;
  130. long filePointer;
  131. byte b;
  132. do {
  133. b = byteBuffer.get();
  134. }
  135. while (b == 0);
  136. byteBuffer.position(byteBuffer.position() - 1);
  137. // read the 3 character ID
  138. byteBuffer.get(buffer, 0, 3);
  139. String identifier = new String(buffer, 0, 3);
  140. // is this a valid identifier?
  141. if (!Lyrics3v2Fields.isLyrics3v2FieldIdentifier(identifier)) {
  142. throw new InvalidTagException(identifier + " is not a valid ID3v2.4 frame");
  143. }
  144. frameBody = readBody(identifier, byteBuffer);
  145. }
  146. /**
  147. * @return
  148. */
  149. public String toString() {
  150. if (frameBody == null) {
  151. return "";
  152. }
  153. return frameBody.toString();
  154. }
  155. /**
  156. * @param file
  157. * @throws IOException
  158. */
  159. public void write(RandomAccessFile file) throws IOException {
  160. if ((frameBody.getSize() > 0) || TagOptionSingleton.getInstance().isLyrics3SaveEmptyField()) {
  161. byte[] buffer = new byte[3];
  162. String str = getIdentifier();
  163. for (int i = 0; i < str.length(); i++) {
  164. buffer[i] = (byte) str.charAt(i);
  165. }
  166. file.write(buffer, 0, str.length());
  167. //body.write(file);
  168. }
  169. }
  170. /**
  171. * Read a Lyrics3 Field from a file.
  172. *
  173. * @param identifier
  174. * @param byteBuffer
  175. * @return
  176. * @throws InvalidTagException
  177. */
  178. private AbstractLyrics3v2FieldFrameBody readBody(String identifier, ByteBuffer byteBuffer) throws InvalidTagException {
  179. AbstractLyrics3v2FieldFrameBody newBody;
  180. if (identifier.equals(Lyrics3v2Fields.FIELD_V2_AUTHOR)) {
  181. newBody = new FieldFrameBodyAUT(byteBuffer);
  182. } else if (identifier.equals(Lyrics3v2Fields.FIELD_V2_ALBUM)) {
  183. newBody = new FieldFrameBodyEAL(byteBuffer);
  184. } else if (identifier.equals(Lyrics3v2Fields.FIELD_V2_ARTIST)) {
  185. newBody = new FieldFrameBodyEAR(byteBuffer);
  186. } else if (identifier.equals(Lyrics3v2Fields.FIELD_V2_TRACK)) {
  187. newBody = new FieldFrameBodyETT(byteBuffer);
  188. } else if (identifier.equals(Lyrics3v2Fields.FIELD_V2_IMAGE)) {
  189. newBody = new FieldFrameBodyIMG(byteBuffer);
  190. } else if (identifier.equals(Lyrics3v2Fields.FIELD_V2_INDICATIONS)) {
  191. newBody = new FieldFrameBodyIND(byteBuffer);
  192. } else if (identifier.equals(Lyrics3v2Fields.FIELD_V2_ADDITIONAL_MULTI_LINE_TEXT)) {
  193. newBody = new FieldFrameBodyINF(byteBuffer);
  194. } else if (identifier.equals(Lyrics3v2Fields.FIELD_V2_LYRICS_MULTI_LINE_TEXT)) {
  195. newBody = new FieldFrameBodyLYR(byteBuffer);
  196. } else {
  197. newBody = new FieldFrameBodyUnsupported(byteBuffer);
  198. }
  199. return newBody;
  200. }
  201. }