public static CipherRegistry registerCiphers ( InputStream source ) { try { if ( source == null ) { throw new IllegalArgumentException ( "Cipher source not found." ) ; } List < String > lines = IOUtils . readLines ( source ) ; int start = - 1 ; int index = 0 ; boolean error = false ; while ( index < lines . size ( ) ) { String line = lines . get ( index ) ; if ( line . trim ( ) . isEmpty ( ) ) { lines . remove ( index ) ; continue ; } if ( line . equals ( "-----BEGIN CIPHER-----" ) ) { if ( start == - 1 ) { start = index + 1 ; } else { error = true ; } } else if ( line . equals ( "-----END CIPHER-----" ) ) { if ( start > 0 ) { int length = index - start ; String [ ] cipher = new String [ length ] ; registerCipher ( lines . subList ( start , index ) . toArray ( cipher ) ) ; start = - 1 ; } else { error = true ; } } if ( error ) { throw new IllegalArgumentException ( "Unexpected text in cipher: " + line ) ; } index ++ ; } if ( start > 0 ) { throw new IllegalArgumentException ( "Missing end cipher token." ) ; } } catch ( IOException e ) { throw MiscUtil . toUnchecked ( e ) ; } finally { IOUtils . closeQuietly ( source ) ; } return instance ; }
Register one or more ciphers from an input stream .