summaryrefslogtreecommitdiffstats
path: root/graphics/java/android/graphics/FontListParser.java
diff options
context:
space:
mode:
Diffstat (limited to 'graphics/java/android/graphics/FontListParser.java')
-rw-r--r--graphics/java/android/graphics/FontListParser.java62
1 files changed, 41 insertions, 21 deletions
diff --git a/graphics/java/android/graphics/FontListParser.java b/graphics/java/android/graphics/FontListParser.java
index 1ca464d..d789690 100644
--- a/graphics/java/android/graphics/FontListParser.java
+++ b/graphics/java/android/graphics/FontListParser.java
@@ -21,6 +21,7 @@ import android.util.Xml;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
+import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
@@ -76,30 +77,53 @@ public class FontListParser {
}
/* Parse fallback list (no names) */
- public static Config parse(File configFilename, File fontDir)
+ public static Config parse(File configFilename, String fontDir)
throws XmlPullParserException, IOException {
- FileInputStream in = new FileInputStream(configFilename);
- if (isLegacyFormat(configFilename)) {
- return parseLegacyFormat(in, fontDir.getAbsolutePath());
- } else {
- return parseNormalFormat(in, fontDir.getAbsolutePath());
- }
+ FileInputStream in = null;
+ in = new FileInputStream(configFilename);
+ return FontListParser.parse(in, fontDir);
}
- private static boolean isLegacyFormat(File configFilename)
+ /* Parse fallback list (no names) */
+ public static Config parse(InputStream in, String fontDir)
throws XmlPullParserException, IOException {
- FileInputStream in = new FileInputStream(configFilename);
- boolean isLegacy = false;
+ BufferedInputStream bis = null;
try {
- XmlPullParser parser = Xml.newPullParser();
- parser.setInput(in, null);
- parser.nextTag();
- parser.require(XmlPullParser.START_TAG, null, "familyset");
- String version = parser.getAttributeValue(null, "version");
- isLegacy = version == null;
+ // wrap input stream in a BufferedInputStream, if it's not already, for mark support
+ if (!(in instanceof BufferedInputStream)) {
+ bis = new BufferedInputStream(in);
+ } else {
+ bis = (BufferedInputStream) in;
+ }
+ // mark the beginning so we can reset to this position after checking format
+ bis.mark(in.available());
+ if (isLegacyFormat(bis)) {
+ return parseLegacyFormat(bis, fontDir);
+ } else {
+ return parseNormalFormat(bis, fontDir);
+ }
} finally {
- in.close();
+ if (bis != null) bis.close();
}
+ }
+
+ public static boolean isLegacyFormat(InputStream in)
+ throws XmlPullParserException, IOException {
+ if (!in.markSupported()) {
+ throw new IllegalArgumentException("InputStream does not support mark");
+ }
+ boolean isLegacy = false;
+
+ XmlPullParser parser = Xml.newPullParser();
+ parser.setInput(in, null);
+ parser.nextTag();
+ parser.require(XmlPullParser.START_TAG, null, "familyset");
+ String version = parser.getAttributeValue(null, "version");
+ isLegacy = version == null;
+
+ // reset the stream so we can read it
+ in.reset();
+
return isLegacy;
}
@@ -116,14 +140,10 @@ public class FontListParser {
public static Config parseNormalFormat(InputStream in, String dirName)
throws XmlPullParserException, IOException {
- try {
XmlPullParser parser = Xml.newPullParser();
parser.setInput(in, null);
parser.nextTag();
return readFamilies(parser, dirName);
- } finally {
- in.close();
- }
}
private static Config readFamilies(XmlPullParser parser, String dirPath)