4 * Copyright (C) 2011 Eric Butler
7 * Eric Butler <eric@codebutler.com>
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 package com.codebutler.farebot.card.desfire;
25 import org.apache.commons.lang3.ArrayUtils;
27 import android.os.Parcel;
28 import android.os.Parcelable;
30 import com.codebutler.farebot.card.desfire.DesfireFileSettings.RecordDesfireFileSettings;
32 public class DesfireFile implements Parcelable {
34 private DesfireFileSettings mSettings;
37 public static DesfireFile create (int fileId, DesfireFileSettings fileSettings, byte[] fileData) {
38 if (fileSettings instanceof RecordDesfireFileSettings)
39 return new RecordDesfireFile(fileId, fileSettings, fileData);
41 return new DesfireFile(fileId, fileSettings, fileData);
44 private DesfireFile (int fileId, DesfireFileSettings fileSettings, byte[] fileData) {
46 mSettings = fileSettings;
50 public DesfireFileSettings getFileSettings () {
58 public byte[] getData () {
62 public static final Parcelable.Creator<DesfireFile> CREATOR = new Parcelable.Creator<DesfireFile>() {
63 public DesfireFile createFromParcel(Parcel source) {
64 int fileId = source.readInt();
66 boolean isError = (source.readInt() == 1);
69 DesfireFileSettings fileSettings = (DesfireFileSettings) source.readParcelable(DesfireFileSettings.class.getClassLoader());
70 int dataLength = source.readInt();
71 byte[] fileData = new byte[dataLength];
72 source.readByteArray(fileData);
74 return DesfireFile.create(fileId, fileSettings, fileData);
76 return new InvalidDesfireFile(fileId, source.readString());
80 public DesfireFile[] newArray (int size) {
81 return new DesfireFile[size];
85 public void writeToParcel (Parcel parcel, int flags) {
87 if (this instanceof InvalidDesfireFile) {
89 parcel.writeString(((InvalidDesfireFile)this).getErrorMessage());
92 parcel.writeParcelable(mSettings, 0);
93 parcel.writeInt(mData.length);
94 parcel.writeByteArray(mData);
98 public int describeContents () {
102 public static class RecordDesfireFile extends DesfireFile {
103 private DesfireRecord[] mRecords;
105 private RecordDesfireFile (int fileId, DesfireFileSettings fileSettings, byte[] fileData) {
106 super(fileId, fileSettings, fileData);
108 RecordDesfireFileSettings settings = (RecordDesfireFileSettings) fileSettings;
110 DesfireRecord[] records = new DesfireRecord[settings.curRecords];
111 for (int i = 0; i < settings.curRecords; i++) {
112 int offset = settings.recordSize * i;
113 records[i] = new DesfireRecord(ArrayUtils.subarray(getData(), offset, offset + settings.recordSize));
118 public DesfireRecord[] getRecords () {
123 public static class InvalidDesfireFile extends DesfireFile {
124 private String mErrorMessage;
126 public InvalidDesfireFile (int fileId, String errorMessage) {
127 super(fileId, null, new byte[0]);
128 mErrorMessage = errorMessage;
131 public String getErrorMessage () {
132 return mErrorMessage;