FeatureNegotiation featureNegotiation = streamInitiation.getFeatureNegotiation();
// Get the stream method which has been chosen by the recipient.
String streamMethod = featureNegotiation.getDataForm().findField(STREAM_METHOD).getValues().get(0);
ByteStreamSession byteStreamSession;
// Choose the stream method to be used based on the recipient's choice.
switch (streamMethod) {
case Socks5ByteStream.NAMESPACE:
try {
byteStreamSession = socks5ByteStreamManager.initiateSession(receiver, sessionId);
} catch (Exception e) {
// As fallback, if SOCKS5 negotiation failed, try IBB.
byteStreamSession = inBandByteStreamManager.initiateSession(receiver, sessionId, 4096);
}
break;
case InBandByteStream.NAMESPACE:
byteStreamSession = inBandByteStreamManager.initiateSession(receiver, sessionId, 4096);
break;
default:
throw new IOException("Receiver returned unsupported stream method.");
}
return byteStreamSession.getOutputStream();
}