So I’m writing a .json parser, and I’m giving my parser support for .json, .zip, and .gz. Both zip and gz would be compressed versions of the json file. When specifying input streams, it looks like this…
public JsonParser(String jsonFile)
{
builder = new StringBuilder();
try
{
// Determines file type
String fileType = jsonFile.substring(jsonFile.lastIndexOf('.')+1);
fileType = fileType.toLowerCase();
// If it is a .json file...
if(fileType.equals("json"))
in = new DataInputStream(new FileInputStream(jsonFile));
// Otherwise, if it is a .zip file...
else if(fileType.equals("zip"))
{
ZipInputStream zis = new ZipInputStream(new FileInputStream(jsonFile));
zis.getNextEntry();
in = new DataInputStream(zis);
}
// Otherwise, if it is a .gz file...
else if(fileType.equals("gz"))
{
in = new DataInputStream(new GZIPInputStream(new FileInputStream(jsonFile)));
}
// Otherwise...
else
throw new RuntimeException("Extension " + fileType + " not accepted by JsonParser");
}
catch(IOException e)
{
e.printStackTrace();
}
}
For SOME reason, when parsing the file when looking for a particular token, it is significantly faster to find it when using a GZIPInputStream or a ZipInputStream. In case you are interested, I am parsing a .json file generated by Tiled which is half of a megabyte in size uncompressed. Many nearly empty layers. Anyway, the file is always read byte by byte with a DataInputStream on the outside. Why would this happen?