-
Notifications
You must be signed in to change notification settings - Fork 1
/
Id3.java
188 lines (171 loc) · 4.83 KB
/
Id3.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package com.dusbabek.lib.id3;
import java.io.*;
/**
* One-stop shopping for editing a tag in a file. Simple values only.
*/
public class Id3
{
public static final int NO_STATE = 0;
public static final int MISSING = 1;
public static final int CORRUPT = 2;
public static final int INVALID_VERSION = 3;
public static final int OTHER_ERROR = 4;
public static final int OK = 99;
private File file = null;
private Tag tag = null;
private int state = NO_STATE;
public Id3(File file)
{
this.file = file;
}
public void setTitle(String title)
{
ensureTag();
tag.putFrame(FrameType.SongName,title);
}
public void setArtist(String artist)
{
ensureTag();
tag.putFrame(FrameType.Artist,artist);
}
public void setAlbum(String album)
{
ensureTag();
tag.putFrame(FrameType.Album,album);
}
public void setYear(String year)
{
ensureTag();
tag.putFrame(FrameType.Year,year);
}
public void setGenre(String genre)
{
ensureTag();
tag.putFrame(FrameType.Genre,genre);
}
public void setTrack(String track)
{
ensureTag();
tag.putFrame(FrameType.Track,track);
}
public void rewrite()
throws IOException
{
InputStream in = new FileInputStream(file);
File outFile = new File(file.getParentFile(),file.getName() + ".tmp");
OutputStream out = new FileOutputStream(outFile,false);
long bytesToRead = 0;
// we need to read past the old tag.
switch (state)
{
case NO_STATE: throw new IOException("NO STATE");
case INVALID_VERSION:
case MISSING: // write in front
bytesToRead = file.length();
break;
case CORRUPT: // rip out everything before the first sync signal.
in.close();
bytesToRead = file.length() - skipToSync(file);
in = new FileInputStream(file);
in.skip(file.length() - bytesToRead);
break;
case OK:
// proceed normally.
in.skip(tag.originalTagLength());
bytesToRead = file.length() - tag.originalTagLength();
break;
default: throw new IOException("Invalid state " + state);
}
long bytesRead = 0;
while (bytesRead < bytesToRead)
{
int avail = in.available();
if (avail < 0)
throw new IOException("expected some bytes.");
else if (avail == 0)
continue;
byte[] buf = new byte[avail];
int read = in.read(buf);
if (read == 0)
continue;
if (read < 0)
throw new IOException("expected some bytes.");
bytesRead += read;
out.write(buf);
}
out.flush();
in.close();
out.close();
if (!file.delete())
throw new IOException("Could not delete original file.");
if (!outFile.renameTo(file))
throw new IOException("Could not rename temp file.");
}
private long skipToSync(File f)
throws IOException
{
FileInputStream in = new FileInputStream(f);
byte[] b = new byte[1];
long size = f.length();
int pos = 0;
boolean found = false;
while (pos < size && !found)
{
pos += in.read(b);
if (b[0] == 0xff)
{
pos +=in.read(b);
if ((b[0] & 0xe0) > 0)
{
found = true;
break;
}
}
}
in.close();
if (!found)
throw new IOException("sync signal not found.");
return pos-2;
}
// precondition: state != NO_STATE and read() has been called.
private void ensureTag()
{
if (tag == null)
{
try
{
tag = new Tag();
}
catch (IOException ex)
{
throw new RuntimeException(ex);
}
}
}
public void read()
throws IOException
{
if (file == null)
throw new IOException("file not set.");
Reader reader = new Reader();
InputStream in = new FileInputStream(file);
state = OTHER_ERROR; // catch all in case generic IOEx is thrown.
try
{
tag = reader.read(in);
state = OK;
}
catch (NotATagException ex)
{
state = MISSING;
}
catch (InvalidVersionException ex)
{
state = INVALID_VERSION;
}
catch (CorruptTagException ex)
{
state = CORRUPT;
}
}
}