ICUResourceBundleReader.java 3.53 KB
/*
 * Decompiled with CFR 0_118.
 */
package com.adobe.agl.impl;

import com.adobe.agl.impl.ICUBinary;
import com.adobe.agl.impl.ICUData;
import com.adobe.agl.util.ULocale;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;

public final class ICUResourceBundleReader
implements ICUBinary.Authenticate {
    private static final byte[] DATA_FORMAT_ID = new byte[]{82, 101, 115, 66};
    private byte[] dataVersion;
    private int rootRes;
    private int[] indexes;
    private boolean noFallback;
    private byte[] data;

    private ICUResourceBundleReader(InputStream stream, String resolvedName) {
        BufferedInputStream bs = new BufferedInputStream(stream);
        try {
            this.dataVersion = ICUBinary.readHeader(bs, DATA_FORMAT_ID, this);
            this.readData(bs);
            stream.close();
        }
        catch (IOException ex) {
            throw new RuntimeException("Data file " + resolvedName + " is corrupt - " + ex.getMessage());
        }
    }

    public static ICUResourceBundleReader getReader(String baseName, String localeName, ClassLoader root) {
        String resolvedName = ICUResourceBundleReader.getFullName(baseName, localeName);
        InputStream stream = ICUData.getStream(root, resolvedName);
        if (stream == null) {
            return null;
        }
        ICUResourceBundleReader reader = new ICUResourceBundleReader(stream, resolvedName);
        return reader;
    }

    private static void writeInt(int i, byte[] bytes, int offset) {
        bytes[offset++] = (byte)(i >> 24);
        bytes[offset++] = (byte)(i >> 16);
        bytes[offset++] = (byte)(i >> 8);
        bytes[offset] = (byte)i;
    }

    private void readData(InputStream stream) throws IOException {
        DataInputStream ds = new DataInputStream(stream);
        this.rootRes = ds.readInt();
        int indexLength = ds.readInt();
        ds.mark((indexLength - 1) * 4);
        this.indexes = new int[indexLength];
        this.indexes[0] = indexLength;
        for (int i = 1; i < indexLength; ++i) {
            this.indexes[i] = ds.readInt();
        }
        this.noFallback = indexLength > 5 && (this.indexes[5] & 1) != 0;
        int length = this.indexes[3] * 4;
        this.data = new byte[length];
        ICUResourceBundleReader.writeInt(this.rootRes, this.data, 0);
        ICUResourceBundleReader.writeInt(indexLength, this.data, 4);
        ds.reset();
        ds.readFully(this.data, 8, length - 8);
    }

    public static String getFullName(String baseName, String localeName) {
        if (baseName == null || baseName.length() == 0) {
            if (localeName.length() == 0) {
                return ULocale.getDefault().toString() + ".res";
            }
            return localeName + ".res";
        }
        if (baseName.indexOf(46) == -1) {
            if (baseName.charAt(baseName.length() - 1) != '/') {
                return baseName + "/" + localeName + ".res";
            }
            return baseName + localeName + ".res";
        }
        baseName = baseName.replace('.', '/');
        if (localeName.length() == 0) {
            return baseName + ".res";
        }
        return baseName + "_" + localeName + ".res";
    }

    public boolean isDataVersionAcceptable(byte[] version) {
        return version[0] == 1 && version[1] >= 1;
    }

    public byte[] getData() {
        return this.data;
    }

    public int getRootResource() {
        return this.rootRes;
    }

    public boolean getNoFallback() {
        return this.noFallback;
    }
}