public ArrayList < STUNService > loadSTUNServers ( java . io . InputStream stunConfigStream ) { ArrayList < STUNService > serversList = new ArrayList <> ( ) ; String serverName ; int serverPort ; try { XmlPullParser parser = XmlPullParserFactory . newInstance ( ) . newPullParser ( ) ; parser . setFeature ( XmlPullParser . FEATURE_PROCESS_NAMESPACES , true ) ; parser . setInput ( stunConfigStream , "UTF-8" ) ; int eventType = parser . getEventType ( ) ; do { if ( eventType == XmlPullParser . START_TAG ) { // Parse a STUN server definition if ( parser . getName ( ) . equals ( "stunServer" ) ) { serverName = null ; serverPort = - 1 ; // Parse the hostname parser . next ( ) ; parser . next ( ) ; serverName = parser . nextText ( ) ; // Parse the port parser . next ( ) ; parser . next ( ) ; try { serverPort = Integer . parseInt ( parser . nextText ( ) ) ; } catch ( Exception e ) { } // If we have a valid hostname and port, add // it to the list. if ( serverName != null && serverPort != - 1 ) { STUNService service = new STUNService ( serverName , serverPort ) ; serversList . add ( service ) ; } } } eventType = parser . next ( ) ; } while ( eventType != XmlPullParser . END_DOCUMENT ) ; } catch ( XmlPullParserException e ) { LOGGER . log ( Level . SEVERE , "Exception" , e ) ; } catch ( IOException e ) { LOGGER . log ( Level . SEVERE , "Exception" , e ) ; } currentServer = bestSTUNServer ( serversList ) ; return serversList ; }
Load the STUN configuration from a stream .