}
public byte[] toByteArray() {
try {
ByteArrayOutputStream collector = new ByteArrayOutputStream();
DEROutputStream der = new DEROutputStream(collector);
der.writeObject(new DERObjectIdentifier(
SpnegoConstants.SPNEGO_MECHANISM));
ASN1EncodableVector fields = new ASN1EncodableVector();
String[] mechanisms = getMechanisms();
if (mechanisms != null) {
ASN1EncodableVector vector = new ASN1EncodableVector();
for (int i = 0; i < mechanisms.length; i++) {
vector.add(new DERObjectIdentifier(mechanisms[i]));
}
fields.add(new DERTaggedObject(true, 0,
new DERSequence(vector)));
}
int contextFlags = getContextFlags();
if (contextFlags != 0) {
fields.add(new DERTaggedObject(true, 1,
new DERBitString(contextFlags)));
}
byte[] mechanismToken = getMechanismToken();
if (mechanismToken != null) {
fields.add(new DERTaggedObject(true, 2,
new DEROctetString(mechanismToken)));
}
byte[] mechanismListMIC = getMechanismListMIC();
if (mechanismListMIC != null) {
fields.add(new DERTaggedObject(true, 3,
new DEROctetString(mechanismListMIC)));
}
der.writeObject(new DERTaggedObject(true, 0,
new DERSequence(fields)));
DERObject token = new DERUnknownTag(DERTags.CONSTRUCTED |
DERTags.APPLICATION, collector.toByteArray());
der = new DEROutputStream(collector = new ByteArrayOutputStream());
der.writeObject(token);
return collector.toByteArray();
} catch (IOException ex) {
throw new IllegalStateException(ex.getMessage());
}
}