aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/files/FileTypeDetector.java
blob: d1a1892655715f14758f11d833ff57069de91424 (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
package cgeo.geocaching.files;

import cgeo.geocaching.utils.Log;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.CharEncoding;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.jdt.annotation.NonNull;

import android.content.ContentResolver;
import android.net.Uri;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class FileTypeDetector {

    private final ContentResolver contentResolver;
    private final Uri uri;

    public FileTypeDetector(final Uri uri, final ContentResolver contentResolver) {
        this.uri = uri;
        this.contentResolver = contentResolver;
    }

	public @NonNull FileType getFileType() {
        InputStream is = null;
        BufferedReader reader = null;
		FileType type = FileType.UNKNOWN;
		try {
            is = contentResolver.openInputStream(uri);
            if (is == null) {
                return FileType.UNKNOWN;
            }
            reader = new BufferedReader(new InputStreamReader(is, CharEncoding.UTF_8));
			type = detectHeader(reader);
            reader.close();
        } catch (final IOException e) {
			Log.e("FileTypeDetector", e);
        } finally {
            IOUtils.closeQuietly(reader);
            IOUtils.closeQuietly(is);
        }
		return type;
    }

	private static FileType detectHeader(final BufferedReader reader)
			throws IOException {
		String line = reader.readLine();
		if (isZip(line)) {
			return FileType.ZIP;
		}
		// scan at most 5 lines of a GPX file
		for (int i = 0; i < 5; i++) {
			line = StringUtils.trim(line);
			if (StringUtils.contains(line, "<loc")) {
				return FileType.LOC;
			}
			if (StringUtils.contains(line, "<gpx")) {
				return FileType.GPX;
			}
			line = reader.readLine();
		}
		return FileType.UNKNOWN;
	}

	private static boolean isZip(final String line) {
		return StringUtils.length(line) >= 4
				&& StringUtils.startsWith(line, "PK") && line.charAt(2) == 3
				&& line.charAt(3) == 4;
	}

}