| 1 | package org.farng.mp3.lyrics3; |
| 2 | |
| 3 | import org.farng.mp3.AbstractMP3FragmentBody; |
| 4 | import org.farng.mp3.InvalidTagException; |
| 5 | import org.farng.mp3.TagOptionSingleton; |
| 6 | |
| 7 | import java.io.IOException; |
| 8 | import java.io.RandomAccessFile; |
| 9 | |
| 10 | /** |
| 11 | * Contains the actual text strings for a Lyrics3v2 field. |
| 12 | * |
| 13 | * @author Eric Farng |
| 14 | * @version $Revision: 1.4 $ |
| 15 | */ |
| 16 | public abstract class AbstractLyrics3v2FieldBody extends AbstractMP3FragmentBody { |
| 17 | |
| 18 | /** |
| 19 | * Creates a new AbstractLyrics3v2FieldBody object. |
| 20 | */ |
| 21 | public AbstractLyrics3v2FieldBody() { |
| 22 | super(); |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Creates a new AbstractLyrics3v2FieldBody object. |
| 27 | */ |
| 28 | public AbstractLyrics3v2FieldBody(final AbstractLyrics3v2FieldBody copyObject) { |
| 29 | super(copyObject); |
| 30 | } |
| 31 | |
| 32 | protected int readHeader(final RandomAccessFile file) throws InvalidTagException, IOException { |
| 33 | final int size; |
| 34 | final byte[] buffer = new byte[5]; |
| 35 | |
| 36 | // read the 5 character size |
| 37 | file.read(buffer, 0, 5); |
| 38 | size = Integer.parseInt(new String(buffer, 0, 5)); |
| 39 | if ((size == 0) && (TagOptionSingleton.getInstance().isLyrics3KeepEmptyFieldIfRead() == false)) { |
| 40 | throw new InvalidTagException("Lyircs3v2 Field has size of zero."); |
| 41 | } |
| 42 | return size; |
| 43 | } |
| 44 | |
| 45 | protected void writeHeader(final RandomAccessFile file, final int size) throws IOException { |
| 46 | final String str; |
| 47 | int offset = 0; |
| 48 | final byte[] buffer = new byte[5]; |
| 49 | |
| 50 | // todo change this to use pad String |
| 51 | str = Integer.toString(getSize()); |
| 52 | for (int i = 0; i < (5 - str.length()); i++) { |
| 53 | buffer[i] = (byte) '0'; |
| 54 | } |
| 55 | offset += (5 - str.length()); |
| 56 | for (int i = 0; i < str.length(); i++) { |
| 57 | buffer[i + offset] = (byte) str.charAt(i); |
| 58 | } |
| 59 | file.write(buffer); |
| 60 | } |
| 61 | } |