001: /*
002: * This file is part of DrFTPD, Distributed FTP Daemon.
003: *
004: * DrFTPD is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * DrFTPD is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with DrFTPD; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018: package org.drftpd.id3;
019:
020: import java.io.File;
021: import java.io.FileNotFoundException;
022: import java.io.IOException;
023: import java.io.RandomAccessFile;
024:
025: /**
026: * @author Teflon-
027: * @version $Id: MP3File.java 1264 2005-09-12 05:22:57Z tdsoul $
028: */
029: public class MP3File extends RandomAccessFile {
030: final static long TAGLENGTH = 128;
031: public boolean hasID3v1Tag = false;
032:
033: /**
034: * @param file
035: * @param mode
036: * @throws java.io.FileNotFoundException
037: */
038: public MP3File(File file, String mode) throws FileNotFoundException {
039: super (file, mode);
040: hasID3v1Tag = existsID3v1Tag();
041: }
042:
043: /**
044: * @param file
045: * @param mode
046: * @throws java.io.FileNotFoundException
047: */
048: public MP3File(String file, String mode)
049: throws FileNotFoundException {
050: super (file, mode);
051: hasID3v1Tag = existsID3v1Tag();
052: }
053:
054: /** Determines, if an id3v1 tag exists.
055: * @return true if an id3v1 tag exists.
056: */
057: public boolean existsID3v1Tag() {
058: boolean hasTag = false;
059:
060: try {
061: seek(length() - TAGLENGTH);
062:
063: byte[] tag = new byte[128];
064: read(tag);
065:
066: String tagString = new String(tag);
067:
068: if (tagString.substring(0, 3).equals("TAG")) {
069: hasTag = true;
070: }
071: } catch (IOException e) {
072: System.out.println(e);
073: }
074:
075: return hasTag;
076: }
077:
078: /** Reads an ID3v1Tag
079: * @return The ID3v1Tag read from file or an empty ID3v1Tag if no tag was found.
080: */
081: public ID3Tag readID3v1Tag() {
082: ID3Tag id3tag = new ID3Tag();
083:
084: if (hasID3v1Tag) {
085: try {
086: seek(length() - TAGLENGTH);
087:
088: byte[] tag = new byte[128];
089: read(tag);
090:
091: String tagString = new String(tag);
092:
093: String title = tagString.substring(3, 33);
094: String artist = tagString.substring(33, 63);
095: String album = tagString.substring(63, 93);
096: String year = tagString.substring(93, 97);
097: String comment = tagString.substring(97, 125);
098: byte track = tag[126];
099: byte genre = tag[127];
100:
101: id3tag.setTitle(title);
102: id3tag.setArtist(artist);
103: id3tag.setAlbum(album);
104: id3tag.setYear(year);
105: id3tag.setComment(comment);
106: id3tag.setTrack(track);
107: id3tag.setGenre(genre);
108: } catch (IOException e) {
109: System.out.println(e);
110: } catch (StringIndexOutOfBoundsException e) {
111: System.out.println(e);
112: }
113: }
114:
115: return id3tag;
116: }
117:
118: /** Writes the given ID3v1Tag to the file.
119: * @throws IOException
120: */
121: public void writeID3v1Tag(ID3Tag tag) throws IOException {
122: if (hasID3v1Tag) {
123: seek(length() - TAGLENGTH);
124: write(tag.toByteArray());
125: } else {
126: setLength(this .length() + 128);
127: seek(length() - TAGLENGTH);
128: write(tag.toByteArray());
129: }
130: }
131: }
|