-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathBaseBuilder.java
More file actions
113 lines (100 loc) · 3.31 KB
/
BaseBuilder.java
File metadata and controls
113 lines (100 loc) · 3.31 KB
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
package com.larvalabs.svgandroid;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.util.Log;
import org.xml.sax.InputSource;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.GZIPInputStream;
/**
* Created by James on 02/05/2014.
*/
public abstract class BaseBuilder<T extends BaseBuilder> {
private InputStream data;
private boolean closeInputStream = true;
/**
* Parse SVG data from an input stream.
*
* @param svgData the input stream, with SVG XML data in UTF-8 character encoding.
* @return the parsed SVG.
*/
public T readFromInputStream(InputStream svgData) {
this.data = svgData;
return (T) this;
}
/**
* Parse SVG data from a string.
*
* @param svgData the string containing SVG XML data.
*/
public T readFromString(String svgData) {
this.data = new ByteArrayInputStream(svgData.getBytes());
return (T) this;
}
/**
* Parse SVG data from an Android application resource.
*
* @param resources the Android context resources.
* @param resId the ID of the raw resource SVG.
*/
public T readFromResource(Resources resources, int resId) {
this.data = resources.openRawResource(resId);
return (T) this;
}
/**
* Parse SVG data from an Android application asset.
*
* @param assetMngr the Android asset manager.
* @param svgPath the path to the SVG file in the application's assets.
* @throws java.io.IOException if there was a problem reading the file.
*/
public T readFromAsset(AssetManager assetMngr, String svgPath) throws IOException {
this.data = assetMngr.open(svgPath);
return (T) this;
}
/**
* Whether or not to close the input stream after reading (ie. after calling build).<br>
* <em>(default is true)</em>
*/
public T setCloseInputStreamWhenDone(boolean closeInputStream) {
this.closeInputStream = closeInputStream;
return (T) this;
}
protected boolean hasData() {
return data != null;
}
protected InputSource openData() {
// SVGZ support (based on https://github.com/josefpavlik/svg-android/commit/fc0522b2e1):
if (!data.markSupported()) {
data = new BufferedInputStream(data); // decorate stream so we can use mark/reset
}
try {
data.mark(4);
byte[] magic = new byte[2];
int r = data.read(magic, 0, 2);
int magicInt = (magic[0] + ((magic[1]) << 8)) & 0xffff;
data.reset();
if (r == 2 && magicInt == GZIPInputStream.GZIP_MAGIC) {
// Log.d(SVGParser.TAG, "SVG is gzipped");
GZIPInputStream gin = new GZIPInputStream(data);
data = gin;
}
}
catch (IOException ioe) {
throw new SVGParseException(ioe);
}
return new InputSource(data);
}
protected void closeData() {
if (closeInputStream) {
try {
data.close();
}
catch (IOException e) {
Log.e(SVGParser.TAG, "Error closing SVG input stream.", e);
}
}
}
}