1 package org.apache.maven.wagon.providers.ssh.knownhost;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.BufferedReader;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.StringReader;
26 import java.util.HashSet;
27 import java.util.Set;
28
29 import org.codehaus.plexus.util.IOUtil;
30 import org.codehaus.plexus.util.StringOutputStream;
31 import org.codehaus.plexus.util.StringUtils;
32
33
34
35
36
37
38
39 public class StreamKnownHostsProvider
40 extends AbstractKnownHostsProvider
41 {
42
43 public StreamKnownHostsProvider( InputStream stream )
44 throws IOException
45 {
46 try
47 {
48 StringOutputStream stringOutputStream = new StringOutputStream();
49 IOUtil.copy( stream, stringOutputStream );
50
51 stream.close();
52 stream = null;
53
54 this.contents = stringOutputStream.toString();
55
56 this.knownHosts = this.loadKnownHosts( this.contents );
57 }
58 finally
59 {
60 IOUtil.close( stream );
61 }
62 }
63
64 protected Set<KnownHostEntry> loadKnownHosts( String contents )
65 throws IOException
66 {
67 Set<KnownHostEntry> hosts = new HashSet<KnownHostEntry>();
68
69 BufferedReader br = new BufferedReader( new StringReader( contents ) );
70
71 String line;
72
73 do
74 {
75 line = br.readLine();
76 if ( line != null )
77 {
78 String tokens[] = StringUtils.split( line );
79 if ( tokens.length == 3 )
80 {
81 hosts.add( new KnownHostEntry( tokens[0], tokens[1], tokens[2] ) );
82 }
83 }
84
85 }
86 while ( line != null );
87
88 return hosts;
89 }
90 }