summaryrefslogtreecommitdiff
path: root/src/de/danoeh/antennapod/util/id3reader/ID3Reader.java
blob: 92f81736369878202a1e9086e6843b0e3925aa42 (plain)
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package de.danoeh.antennapod.util.id3reader;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;

import org.apache.commons.io.IOUtils;

import de.danoeh.antennapod.util.id3reader.model.FrameHeader;
import de.danoeh.antennapod.util.id3reader.model.TagHeader;

/**
 * Reads the ID3 Tag of a given file. In order to use this class, you should
 * create a subclass of it and overwrite the onStart* - or onEnd* - methods.
 */
public class ID3Reader {
	private static final int HEADER_LENGTH = 10;
	private static final int ID3_LENGTH = 3;
	private static final int FRAME_ID_LENGTH = 4;

	protected static final int ACTION_SKIP = 1;
	protected static final int ACTION_DONT_SKIP = 2;

	protected int readerPosition;

	private static final byte ENCODING_UTF16_WITH_BOM = 1;
    private static final byte ENCODING_UTF16_WITHOUT_BOM = 2;
    private static final byte ENCODING_UTF8 = 3;

    private TagHeader tagHeader;

	public ID3Reader() {
	}

	public final void readInputStream(InputStream input) throws IOException,
			ID3ReaderException {
		int rc;
		readerPosition = 0;
		char[] tagHeaderSource = readBytes(input, HEADER_LENGTH);
		tagHeader = createTagHeader(tagHeaderSource);
		if (tagHeader == null) {
			onNoTagHeaderFound();
		} else {
			rc = onStartTagHeader(tagHeader);
			if (rc == ACTION_SKIP) {
				onEndTag();
			} else {
				while (readerPosition < tagHeader.getSize()) {
					FrameHeader frameHeader = createFrameHeader(readBytes(
							input, HEADER_LENGTH));
					if (checkForNullString(frameHeader.getId())) {
						break;
					} else {
						rc = onStartFrameHeader(frameHeader, input);
						if (rc == ACTION_SKIP) {

							if (frameHeader.getSize() + readerPosition > tagHeader
									.getSize()) {
								break;
							} else {
								skipBytes(input, frameHeader.getSize());
							}
						}
					}
				}
				onEndTag();
			}
		}
	}

	/** Returns true if string only contains null-bytes. */
	private boolean checkForNullString(String s) {
		if (!s.isEmpty()) {
			int i = 0;
			if (s.charAt(i) == 0) {
				for (i = 1; i < s.length(); i++) {
					if (s.charAt(i) != 0) {
						return false;
					}
				}
				return true;
			}
			return false;
		} else {
			return true;
		}

	}

	/**
	 * Read a certain number of bytes from the given input stream. This method
	 * changes the readerPosition-attribute.
	 */
	protected char[] readBytes(InputStream input, int number)
			throws IOException, ID3ReaderException {
		char[] header = new char[number];
		for (int i = 0; i < number; i++) {
			int b = input.read();
			readerPosition++;
			if (b != -1) {
				header[i] = (char) b;
			} else {
				throw new ID3ReaderException("Unexpected end of stream");
			}
		}
		return header;
	}

	/**
	 * Skip a certain number of bytes on the given input stream. This method
	 * changes the readerPosition-attribute.
	 */
	protected void skipBytes(InputStream input, int number) throws IOException {
		if (number <= 0) {
			number = 1;
		}
		IOUtils.skipFully(input, number);

		readerPosition += number;
	}

	private TagHeader createTagHeader(char[] source) throws ID3ReaderException {
		boolean hasTag = (source[0] == 0x49) && (source[1] == 0x44)
				&& (source[2] == 0x33);
		if (source.length != HEADER_LENGTH) {
			throw new ID3ReaderException("Length of header must be "
					+ HEADER_LENGTH);
		}
		if (hasTag) {
			String id = new String(source, 0, ID3_LENGTH);
			char version = (char) ((source[3] << 8) | source[4]);
			byte flags = (byte) source[5];
			int size = (source[6] << 24) | (source[7] << 16) | (source[8] << 8)
					| source[9];
            size = unsynchsafe(size);
			return new TagHeader(id, size, version, flags);
		} else {
			return null;
		}
	}

	private FrameHeader createFrameHeader(char[] source)
			throws ID3ReaderException {
		if (source.length != HEADER_LENGTH) {
			throw new ID3ReaderException("Length of header must be "
					+ HEADER_LENGTH);
		}
		String id = new String(source, 0, FRAME_ID_LENGTH);

        int size = (((int) source[4]) << 24) | (((int) source[5]) << 16)
                    | (((int) source[6]) << 8) | source[7];
        if (tagHeader != null && tagHeader.getVersion() >= 0x0400) {
            size = unsynchsafe(size);
        }
		char flags = (char) ((source[8] << 8) | source[9]);
		return new FrameHeader(id, size, flags);
	}

    private int unsynchsafe(int in) {
        int out = 0;
        int mask = 0x7F000000;

        while (mask != 0) {
            out >>= 1;
            out |= in & mask;
            mask >>= 8;
        }

        return out;
    }

	protected int readString(StringBuffer buffer, InputStream input, int max) throws IOException,
			ID3ReaderException {
		if (max > 0) {
			char[] encoding = readBytes(input, 1);
			max--;
			
			if (encoding[0] == ENCODING_UTF16_WITH_BOM || encoding[0] == ENCODING_UTF16_WITHOUT_BOM) {
                return readUnicodeString(buffer, input, max, Charset.forName("UTF-16")) + 1; // take encoding byte into account
			} else if (encoding[0] == ENCODING_UTF8) {
                return readUnicodeString(buffer, input, max, Charset.forName("UTF-8")) + 1; // take encoding byte into account
            } else {
				return readISOString(buffer, input, max) + 1; // take encoding byte into account
			}
		} else {
            if (buffer != null) {
                buffer.append("");
            }
			return 0;
		}
	}

	protected int readISOString(StringBuffer buffer, InputStream input, int max)
			throws IOException, ID3ReaderException {

		int bytesRead = 0;
		char c;
		while (++bytesRead <= max && (c = (char) input.read()) > 0) {
            if (buffer != null) {
			    buffer.append(c);
            }
		}
		return bytesRead;
	}

	private int readUnicodeString(StringBuffer strBuffer, InputStream input, int max, Charset charset)
			throws IOException, ID3ReaderException {
		byte[] buffer = new byte[max];
        int c, cZero = -1;
        int i = 0;
        for (; i < max; i++) {
            c = input.read();
            if (c == -1) {
                break;
            } else if (c == 0) {
                if (cZero == 0) {
                    // termination character found
                    break;
                } else {
                    cZero = 0;
                }
            } else {
                buffer[i] = (byte) c;
                cZero = -1;
            }
        }
        if (strBuffer != null) {
		    strBuffer.append(charset.newDecoder().decode(ByteBuffer.wrap(buffer)).toString());
        }
        return i;
	}

	public int onStartTagHeader(TagHeader header) {
		return ACTION_SKIP;
	}

	public int onStartFrameHeader(FrameHeader header, InputStream input)
			throws IOException, ID3ReaderException {
		return ACTION_SKIP;
	}

	public void onEndTag() {

	}

	public void onNoTagHeaderFound() {

	}

}