package android.nfc;
import android.os.Parcel;
import android.os.Parcelable;
import java.io.IOException;
public final class TransceiveResult implements Parcelable {
public static final int RESULT_SUCCESS = 0;
public static final int RESULT_FAILURE = 1;
public static final int RESULT_TAGLOST = 2;
public static final int RESULT_EXCEEDED_LENGTH = 3;
final int mResult;
final byte[] mResponseData;
public TransceiveResult(final int result, final byte[] data) {
mResult = result;
mResponseData = data;
}
public byte[] getResponseOrThrow() throws IOException {
switch (mResult) {
case RESULT_SUCCESS:
return mResponseData;
case RESULT_TAGLOST:
throw new TagLostException("Tag was lost.");
case RESULT_EXCEEDED_LENGTH:
throw new IOException("Transceive length exceeds supported maximum");
default:
throw new IOException("Transceive failed");
}
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(mResult);
if (mResult == RESULT_SUCCESS) {
dest.writeInt(mResponseData.length);
dest.writeByteArray(mResponseData);
}
}
public static final Parcelable.Creator<TransceiveResult> CREATOR =
new Parcelable.Creator<TransceiveResult>() {
@Override
public TransceiveResult createFromParcel(Parcel in) {
int result = in.readInt();
byte[] responseData;
if (result == RESULT_SUCCESS) {
int responseLength = in.readInt();
responseData = new byte[responseLength];
in.readByteArray(responseData);
} else {
responseData = null;
}
return new TransceiveResult(result, responseData);
}
@Override
public TransceiveResult[] newArray(int size) {
return new TransceiveResult[size];
}
};
}