ARSCDecoder: modified interface to be more flexible - added ARSCData class.

This commit is contained in:
Ryszard Wiśniewski 2010-06-02 14:51:33 +02:00
parent 58e7d6960f
commit 5fda5dbf00
2 changed files with 52 additions and 16 deletions

View File

@ -294,8 +294,8 @@ final public class AndrolibResources {
ResTable resTable) throws AndrolibException {
try {
return ARSCDecoder.decode(
apkFile.getDirectory().getFileInput("resources.arsc"),
resTable);
apkFile.getDirectory().getFileInput("resources.arsc"), false,
resTable).getPackages();
} catch (DirectoryException ex) {
throw new AndrolibException(
"Could not load resources.arsc from file: " + apkFile, ex);
@ -378,7 +378,8 @@ final public class AndrolibResources {
public void publicizeResources(byte[] arsc) throws AndrolibException {
for (FlagsOffset flags :
ARSCDecoder.findFlagsOffsets(new ByteArrayInputStream(arsc))) {
ARSCDecoder.decode(new ByteArrayInputStream(arsc), true)
.getFlagsOffsets()) {
int offset = flags.offset + 3;
int end = offset + 4 * flags.count;
while(offset < end) {

View File

@ -32,28 +32,28 @@ import org.apache.commons.io.input.CountingInputStream;
* @author Ryszard Wiśniewski <brut.alll@gmail.com>
*/
public class ARSCDecoder {
public static ResPackage[] decode(InputStream arscStream, ResTable resTable)
public static ARSCData decode(InputStream arscStream,
boolean findFlagsOffsets) throws AndrolibException {
return decode(arscStream, findFlagsOffsets, new ResTable());
}
public static ARSCData decode(InputStream arscStream,
boolean findFlagsOffsets, ResTable resTable)
throws AndrolibException {
try {
return new ARSCDecoder(arscStream, resTable, false).readTable();
ARSCDecoder decoder = new ARSCDecoder(arscStream, resTable,
findFlagsOffsets);
ResPackage[] pkgs = decoder.readTable();
return new ARSCData(
pkgs,
decoder.mFlagsOffsets == null ? null :
decoder.mFlagsOffsets.toArray(new FlagsOffset[0]),
resTable);
} catch (IOException ex) {
throw new AndrolibException("Could not decode arsc file", ex);
}
}
public static List<FlagsOffset> findFlagsOffsets(InputStream arscStream)
throws AndrolibException {
try {
ResTable resTable = new ResTable();
ARSCDecoder decoder = new ARSCDecoder(arscStream, resTable, true);
decoder.readTable();
return decoder.mFlagsOffsets;
} catch (IOException ex) {
throw new AndrolibException("Could not decode arsc file", ex);
}
}
private ARSCDecoder(InputStream arscStream, ResTable resTable,
boolean storeFlagsOffsets) {
if (storeFlagsOffsets) {
@ -357,4 +357,39 @@ public class ARSCDecoder {
private static final Logger LOGGER =
Logger.getLogger(ARSCDecoder.class.getName());
public static class ARSCData {
public ARSCData(ResPackage[] packages, FlagsOffset[] flagsOffsets,
ResTable resTable) {
mPackages = packages;
mFlagsOffsets = flagsOffsets;
mResTable = resTable;
}
public FlagsOffset[] getFlagsOffsets() {
return mFlagsOffsets;
}
public ResPackage[] getPackages() {
return mPackages;
}
public ResPackage getOnePackage() throws AndrolibException {
if (mPackages.length != 1) {
throw new AndrolibException(
"Arsc file contains zero or multiple packages");
}
return mPackages[0];
}
public ResTable getResTable() {
return mResTable;
}
private final ResPackage[] mPackages;
private final FlagsOffset[] mFlagsOffsets;
private final ResTable mResTable;
}
}