Code example for ByteArrayInputStream

Methods: read

0
    static final byte CYCLIC_RECORD_FILE = (byte) 0x04;
     
    public static DesfireFileSettings Create (byte[] data) throws Exception {
        byte fileType = (byte) data[0];
 
        ByteArrayInputStream stream = new ByteArrayInputStream(data);
 
        if (fileType == STANDARD_DATA_FILE || fileType == BACKUP_DATA_FILE)
            return new StandardDesfireFileSettings(stream);
        else if (fileType == LINEAR_RECORD_FILE || fileType == CYCLIC_RECORD_FILE)
            return new RecordDesfireFileSettings(stream);
        else if (fileType == VALUE_FILE)
            throw new UnsupportedOperationException("Value files not yet supported");
        else 
            throw new Exception("Unknown file type: " + Integer.toHexString(fileType));
    } 
 
    private DesfireFileSettings (ByteArrayInputStream stream) {
        fileType    = (byte) stream.read();
        commSetting = (byte) stream.read();
 
        accessRights = new byte[2];
        stream.read(accessRights, 0, accessRights.length);
    } 
 
    private DesfireFileSettings (byte fileType, byte commSetting, byte[] accessRights) {
        this.fileType     = fileType;
        this.commSetting  = commSetting;
        this.accessRights = accessRights;
    } 
 
    public String getFileTypeName () {
        switch (fileType) {
            case STANDARD_DATA_FILE:
                return "Standard"; 
            case BACKUP_DATA_FILE:
                return "Backup"; 
            case VALUE_FILE:
                return "Value"; 
            case LINEAR_RECORD_FILE:
                return "Linear Record"; 
            case CYCLIC_RECORD_FILE:
                return "Cyclic Record"; 
            default: 
                return "Unknown"; 
        } 
    } 
 
    public static final Parcelable.Creator<DesfireFileSettings> CREATOR = new Parcelable.Creator<DesfireFileSettings>() {
        public DesfireFileSettings createFromParcel(Parcel source) { 
            byte fileType       = source.readByte(); 
            byte commSetting    = source.readByte(); 
            byte[] accessRights = new byte[source.readInt()]; 
            source.readByteArray(accessRights); 
 
            if (fileType == STANDARD_DATA_FILE || fileType == BACKUP_DATA_FILE) { 
                int fileSize = source.readInt(); 
                return new StandardDesfireFileSettings(fileType, commSetting, accessRights, fileSize); 
            } else if (fileType == LINEAR_RECORD_FILE || fileType == CYCLIC_RECORD_FILE) { 
                int recordSize = source.readInt(); 
                int maxRecords = source.readInt(); 
                int curRecords = source.readInt(); 
                return new RecordDesfireFileSettings(fileType, commSetting, accessRights, recordSize, maxRecords, curRecords); 
            } else { 
                return new UnsupportedDesfireFileSettings(fileType); 
            } 
        } 
 
        public DesfireFileSettings[] newArray(int size) { 
            return new DesfireFileSettings[size]; 
        } 
    }; 
 
    public void writeToParcel (Parcel parcel, int flags) {
        parcel.writeByte(fileType);
        parcel.writeByte(commSetting);
        parcel.writeInt(accessRights.length);
        parcel.writeByteArray(accessRights);
    } 
 
    public int describeContents () { 
        return 0; 
    } 
 
    public static class StandardDesfireFileSettings extends DesfireFileSettings { 
        public final int fileSize;
 
        private StandardDesfireFileSettings (ByteArrayInputStream stream) {
            super(stream);
            byte[] buf = new byte[3];
            stream.read(buf, 0, buf.length);
            ArrayUtils.reverse(buf);
            fileSize = Utils.byteArrayToInt(buf);
        } 
 
        StandardDesfireFileSettings (byte fileType, byte commSetting, byte[] accessRights, int fileSize) {
            super(fileType, commSetting, accessRights);
            this.fileSize = fileSize;
        } 
 
        @Override 
        public void writeToParcel (Parcel parcel, int flags) {
            super.writeToParcel(parcel, flags);
            parcel.writeInt(fileSize);
        } 
    } 
 
    public static class RecordDesfireFileSettings extends DesfireFileSettings { 
        public final int recordSize;
        public final int maxRecords;
        public final int curRecords;
 
        public RecordDesfireFileSettings(ByteArrayInputStream stream) {
            super(stream);
 
            byte[] buf = new byte[3];
            stream.read(buf, 0, buf.length);
            ArrayUtils.reverse(buf);
            recordSize = Utils.byteArrayToInt(buf);
 
            buf = new byte[3];
            stream.read(buf, 0, buf.length);
            ArrayUtils.reverse(buf);
            maxRecords = Utils.byteArrayToInt(buf);
 
            buf = new byte[3];
            stream.read(buf, 0, buf.length);
            ArrayUtils.reverse(buf);
            curRecords = Utils.byteArrayToInt(buf);
        } 
 
        RecordDesfireFileSettings (byte fileType, byte commSetting, byte[] accessRights, int recordSize, int maxRecords, int curRecords) {