| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| AbstractLyrics3v2FieldFrameBody |
|
| 0.0;0 |
| 1 | /** | |
| 2 | * @author : Paul Taylor | |
| 3 | * @author : Eric Farng | |
| 4 | * | |
| 5 | * Version @version:$Id: AbstractLyrics3v2FieldFrameBody.java,v 1.11 2008/07/21 10:45:49 paultaylor Exp $ | |
| 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 | ||
| 25 | import org.jaudiotagger.tag.InvalidTagException; | |
| 26 | import org.jaudiotagger.tag.TagOptionSingleton; | |
| 27 | import org.jaudiotagger.tag.datatype.AbstractDataType; | |
| 28 | import org.jaudiotagger.tag.id3.AbstractTagFrameBody; | |
| 29 | ||
| 30 | import java.io.IOException; | |
| 31 | import java.io.RandomAccessFile; | |
| 32 | import java.nio.ByteBuffer; | |
| 33 | import java.util.Iterator; | |
| 34 | ||
| 35 | public abstract class AbstractLyrics3v2FieldFrameBody extends AbstractTagFrameBody | |
| 36 | { | |
| 37 | public AbstractLyrics3v2FieldFrameBody() | |
| 38 | 0 | { |
| 39 | 0 | } |
| 40 | ||
| 41 | public AbstractLyrics3v2FieldFrameBody(AbstractLyrics3v2FieldFrameBody copyObject) | |
| 42 | { | |
| 43 | 0 | super(copyObject); |
| 44 | 0 | } |
| 45 | ||
| 46 | /** | |
| 47 | * This is called by superclass when attempt to read data from file. | |
| 48 | * | |
| 49 | * @param file | |
| 50 | * @return | |
| 51 | * @throws InvalidTagException | |
| 52 | * @throws IOException | |
| 53 | */ | |
| 54 | protected int readHeader(RandomAccessFile file) throws InvalidTagException, IOException | |
| 55 | { | |
| 56 | int size; | |
| 57 | 0 | byte[] buffer = new byte[5]; |
| 58 | ||
| 59 | // read the 5 character size | |
| 60 | 0 | file.read(buffer, 0, 5); |
| 61 | 0 | size = Integer.parseInt(new String(buffer, 0, 5)); |
| 62 | ||
| 63 | 0 | if ((size == 0) && (TagOptionSingleton.getInstance().isLyrics3KeepEmptyFieldIfRead() == false)) |
| 64 | { | |
| 65 | 0 | throw new InvalidTagException("Lyircs3v2 Field has size of zero."); |
| 66 | } | |
| 67 | ||
| 68 | 0 | return size; |
| 69 | } | |
| 70 | ||
| 71 | /** | |
| 72 | * This is called by superclass when attempt to write data from file. | |
| 73 | * | |
| 74 | * @param file | |
| 75 | * @param size | |
| 76 | * @throws IOException | |
| 77 | */ | |
| 78 | protected void writeHeader(RandomAccessFile file, int size) throws IOException | |
| 79 | { | |
| 80 | String str; | |
| 81 | 0 | int offset = 0; |
| 82 | 0 | byte[] buffer = new byte[5]; |
| 83 | ||
| 84 | /** | |
| 85 | * @todo change this to use pad String | |
| 86 | */ | |
| 87 | 0 | str = Integer.toString(getSize()); |
| 88 | ||
| 89 | 0 | for (int i = 0; i < (5 - str.length()); i++) |
| 90 | { | |
| 91 | 0 | buffer[i] = (byte) '0'; |
| 92 | } | |
| 93 | ||
| 94 | 0 | offset += (5 - str.length()); |
| 95 | ||
| 96 | 0 | for (int i = 0; i < str.length(); i++) |
| 97 | { | |
| 98 | 0 | buffer[i + offset] = (byte) str.charAt(i); |
| 99 | } | |
| 100 | ||
| 101 | 0 | file.write(buffer); |
| 102 | 0 | } |
| 103 | ||
| 104 | /** | |
| 105 | * This reads a frame body from its file into the appropriate FrameBody class | |
| 106 | * Read the data from the given file into this datatype. The file needs to | |
| 107 | * have its file pointer in the correct location. The size as indicated in the | |
| 108 | * header is passed to the frame constructor when reading from file. | |
| 109 | * | |
| 110 | * @param byteBuffer file to read | |
| 111 | * @throws IOException on any I/O error | |
| 112 | * @throws InvalidTagException if there is any error in the data format. | |
| 113 | */ | |
| 114 | public void read(ByteBuffer byteBuffer) throws InvalidTagException | |
| 115 | { | |
| 116 | 0 | int size = getSize(); |
| 117 | //Allocate a buffer to the size of the Frame Body and read from file | |
| 118 | 0 | byte[] buffer = new byte[size]; |
| 119 | 0 | byteBuffer.get(buffer); |
| 120 | //Offset into buffer, incremented by length of previous MP3Object | |
| 121 | 0 | int offset = 0; |
| 122 | ||
| 123 | //Go through the ObjectList of the Frame reading the data into the | |
| 124 | //correct datatype. | |
| 125 | AbstractDataType object; | |
| 126 | 0 | Iterator<AbstractDataType> iterator = objectList.listIterator(); |
| 127 | 0 | while (iterator.hasNext()) |
| 128 | { | |
| 129 | //The read has extended further than the defined frame size | |
| 130 | 0 | if (offset > (size - 1)) |
| 131 | { | |
| 132 | 0 | throw new InvalidTagException("Invalid size for Frame Body"); |
| 133 | } | |
| 134 | ||
| 135 | //Get next Object and load it with data from the Buffer | |
| 136 | 0 | object = iterator.next(); |
| 137 | 0 | object.readByteArray(buffer, offset); |
| 138 | //Increment Offset to start of next datatype. | |
| 139 | 0 | offset += object.getSize(); |
| 140 | } | |
| 141 | 0 | } |
| 142 | ||
| 143 | /** | |
| 144 | * Write the contents of this datatype to the file at the position it is | |
| 145 | * currently at. | |
| 146 | * | |
| 147 | * @param file destination file | |
| 148 | * @throws IOException on any I/O error | |
| 149 | */ | |
| 150 | public void write(RandomAccessFile file) throws IOException | |
| 151 | { | |
| 152 | //Write the various fields to file in order | |
| 153 | byte[] buffer; | |
| 154 | AbstractDataType object; | |
| 155 | 0 | Iterator<AbstractDataType> iterator = objectList.listIterator(); |
| 156 | 0 | while (iterator.hasNext()) |
| 157 | { | |
| 158 | 0 | object = iterator.next(); |
| 159 | 0 | buffer = object.writeByteArray(); |
| 160 | 0 | file.write(buffer); |
| 161 | } | |
| 162 | 0 | } |
| 163 | ||
| 164 | } |