public static ARSCFile read(IntReader reader) throws IOException { ARSCFile arsc = new ARSCFile(); ReadUtil.readCheckType(reader, ARSC_CHUNK_TYPE); /* size */ reader.readInt(); int groupCount = reader.readInt(); if (groupCount != 1) { throw new IOException("Only one package per file is supported."); } arsc.strings = StringBlock.read(reader); arsc.pkges = new Pkge[1]; arsc.pkges[0] = readPackage(arsc, reader); return arsc; }
private static Pkge readPackage(ARSCFile file, IntReader reader) throws IOException { Pkge pkge = new Pkge(); pkge.file = file; ReadUtil.readCheckType(reader, PACKAGE_CHUNK_TYPE); /* size */ reader.skipInt(); pkge.id = reader.readInt(); { final int nameLength = 128; StringBuilder name = new StringBuilder(16); int i = 0; for (; i != nameLength; ) { ++i; int ch = reader.readShort(); if (ch == 0) { break; } name.append((char) ch); } reader.skip((nameLength - i) * 2); reader.skip((nameLength * 2) % 4); pkge.name = name.toString(); } /* signature? */ reader.skipInt(); int assetCount = reader.readInt(); /* idNamesOffset */ reader.skipInt(); /* idNamesCount */ reader.skipInt(); pkge.assetNames = StringBlock.read(reader); pkge.resourceNames = StringBlock.read(reader); pkge.assets = new Asset[assetCount]; ArrayList contents = new ArrayList(); int assetsRead = 0; Asset currentAsset = null; while (reader.available() != 0) { int chunkType = reader.readInt(); if (chunkType != CONTENT_CHUNK_TYPE) { if (currentAsset != null) { currentAsset.contents = new Content[contents.size()]; contents.toArray(currentAsset.contents); contents.clear(); } } if (chunkType == ASSET_CHUNK_TYPE) { currentAsset = readAsset(reader, pkge); pkge.assets[assetsRead] = currentAsset; assetsRead += 1; } else if (chunkType == CONTENT_CHUNK_TYPE) { if (currentAsset == null) { throw new IOException("Invalid chunk sequence: content read before asset."); } contents.add(readContent(reader, currentAsset)); } else { throw new IOException("Unexpected chunk type (" + chunkType + ")."); } } if (currentAsset != null) { currentAsset.contents = new Content[contents.size()]; contents.toArray(currentAsset.contents); contents.clear(); } if (assetsRead != assetCount) { throw new IOException("Not all assets where read (" + (assetCount - assetsRead) + " left)."); } return pkge; }