@@ -838,11 +838,7 @@ public void run()
838
838
public synchronized LocalPortForwarder createLocalPortForwarder (int local_port , String host_to_connect ,
839
839
int port_to_connect ) throws IOException
840
840
{
841
- if (tm == null )
842
- throw new IllegalStateException ("Cannot forward ports, you need to establish a connection first." );
843
-
844
- if (!authenticated )
845
- throw new IllegalStateException ("Cannot forward ports, connection is not authenticated." );
841
+ this .checkConnection ();
846
842
847
843
return new LocalPortForwarder (cm , local_port , host_to_connect , port_to_connect );
848
844
}
@@ -865,11 +861,7 @@ public synchronized LocalPortForwarder createLocalPortForwarder(int local_port,
865
861
public synchronized LocalPortForwarder createLocalPortForwarder (InetSocketAddress addr , String host_to_connect ,
866
862
int port_to_connect ) throws IOException
867
863
{
868
- if (tm == null )
869
- throw new IllegalStateException ("Cannot forward ports, you need to establish a connection first." );
870
-
871
- if (!authenticated )
872
- throw new IllegalStateException ("Cannot forward ports, connection is not authenticated." );
864
+ this .checkConnection ();
873
865
874
866
return new LocalPortForwarder (cm , addr , host_to_connect , port_to_connect );
875
867
}
@@ -888,11 +880,7 @@ public synchronized LocalPortForwarder createLocalPortForwarder(InetSocketAddres
888
880
public synchronized LocalStreamForwarder createLocalStreamForwarder (String host_to_connect , int port_to_connect )
889
881
throws IOException
890
882
{
891
- if (tm == null )
892
- throw new IllegalStateException ("Cannot forward, you need to establish a connection first." );
893
-
894
- if (!authenticated )
895
- throw new IllegalStateException ("Cannot forward, connection is not authenticated." );
883
+ this .checkConnection ();
896
884
897
885
return new LocalStreamForwarder (cm , host_to_connect , port_to_connect );
898
886
}
@@ -911,11 +899,7 @@ public synchronized LocalStreamForwarder createLocalStreamForwarder(String host_
911
899
*/
912
900
public synchronized SCPClient createSCPClient () throws IOException
913
901
{
914
- if (tm == null )
915
- throw new IllegalStateException ("Cannot create SCP client, you need to establish a connection first." );
916
-
917
- if (!authenticated )
918
- throw new IllegalStateException ("Cannot create SCP client, connection is not authenticated." );
902
+ this .checkConnection ();
919
903
920
904
return new SCPClient (this );
921
905
}
@@ -935,8 +919,7 @@ public synchronized SCPClient createSCPClient() throws IOException
935
919
*/
936
920
public synchronized void forceKeyExchange () throws IOException
937
921
{
938
- if (tm == null )
939
- throw new IllegalStateException ("You need to establish a connection first." );
922
+ this .checkConnection ();
940
923
941
924
tm .forceKeyExchange (cryptoWishList , dhgexpara , null , null );
942
925
}
@@ -972,9 +955,8 @@ public synchronized int getPort()
972
955
*/
973
956
public synchronized ConnectionInfo getConnectionInfo () throws IOException
974
957
{
975
- if (tm == null )
976
- throw new IllegalStateException (
977
- "Cannot get details of connection, you need to establish a connection first." );
958
+ this .checkConnection ();
959
+
978
960
return tm .getConnectionInfo (1 );
979
961
}
980
962
@@ -1074,11 +1056,11 @@ public synchronized boolean isAuthMethodAvailable(String user, String method) th
1074
1056
1075
1057
String methods [] = getRemainingAuthMethods (user );
1076
1058
1077
- for ( int i = 0 ; i < methods . length ; i ++)
1078
- {
1079
- if ( methods [ i ]. compareTo ( method ) == 0 )
1080
- return true ;
1081
- }
1059
+ for ( final String m : methods ) {
1060
+ if ( m . compareTo ( method ) == 0 ) {
1061
+ return true ;
1062
+ }
1063
+ }
1082
1064
1083
1065
return false ;
1084
1066
}
@@ -1101,11 +1083,7 @@ private SecureRandom getOrCreateSecureRND()
1101
1083
*/
1102
1084
public synchronized Session openSession () throws IOException
1103
1085
{
1104
- if (tm == null )
1105
- throw new IllegalStateException ("Cannot open session, you need to establish a connection first." );
1106
-
1107
- if (!authenticated )
1108
- throw new IllegalStateException ("Cannot open session, connection is not authenticated." );
1086
+ this .checkConnection ();
1109
1087
1110
1088
return new Session (cm , getOrCreateSecureRND ());
1111
1089
}
@@ -1137,12 +1115,7 @@ public synchronized void sendIgnorePacket() throws IOException
1137
1115
*/
1138
1116
public synchronized void sendIgnorePacket (byte [] data ) throws IOException
1139
1117
{
1140
- if (data == null )
1141
- throw new IllegalArgumentException ("data argument must not be null." );
1142
-
1143
- if (tm == null )
1144
- throw new IllegalStateException (
1145
- "Cannot send SSH_MSG_IGNORE packet, you need to establish a connection first." );
1118
+ this .checkConnection ();
1146
1119
1147
1120
PacketIgnore pi = new PacketIgnore ();
1148
1121
pi .setData (data );
@@ -1167,26 +1140,21 @@ private String[] removeDuplicates(String[] list)
1167
1140
1168
1141
int count = 0 ;
1169
1142
1170
- for (int i = 0 ; i < list .length ; i ++)
1171
- {
1172
- boolean duplicate = false ;
1173
-
1174
- String element = list [i ];
1143
+ for (final String element : list ) {
1144
+ boolean duplicate = false ;
1145
+ for (int j = 0 ; j < count ; j ++) {
1146
+ if (((element == null ) && (list2 [j ] == null )) || ((element != null ) && (element .equals (list2 [j ])))) {
1147
+ duplicate = true ;
1148
+ break ;
1149
+ }
1150
+ }
1175
1151
1176
- for (int j = 0 ; j < count ; j ++)
1177
- {
1178
- if (((element == null ) && (list2 [j ] == null )) || ((element != null ) && (element .equals (list2 [j ]))))
1179
- {
1180
- duplicate = true ;
1181
- break ;
1182
- }
1183
- }
1152
+ if (duplicate ) {
1153
+ continue ;
1154
+ }
1184
1155
1185
- if (duplicate )
1186
- continue ;
1187
-
1188
- list2 [count ++] = list [i ];
1189
- }
1156
+ list2 [count ++] = element ;
1157
+ }
1190
1158
1191
1159
if (count == list2 .length )
1192
1160
return list2 ;
@@ -1370,11 +1338,7 @@ public synchronized void setProxyData(ProxyData proxyData)
1370
1338
public synchronized void requestRemotePortForwarding (String bindAddress , int bindPort , String targetAddress ,
1371
1339
int targetPort ) throws IOException
1372
1340
{
1373
- if (tm == null )
1374
- throw new IllegalStateException ("You need to establish a connection first." );
1375
-
1376
- if (!authenticated )
1377
- throw new IllegalStateException ("The connection is not authenticated." );
1341
+ this .checkConnection ();
1378
1342
1379
1343
if ((bindAddress == null ) || (targetAddress == null ) || (bindPort <= 0 ) || (targetPort <= 0 ))
1380
1344
throw new IllegalArgumentException ();
@@ -1394,11 +1358,7 @@ public synchronized void requestRemotePortForwarding(String bindAddress, int bin
1394
1358
*/
1395
1359
public synchronized void cancelRemotePortForwarding (int bindPort ) throws IOException
1396
1360
{
1397
- if (tm == null )
1398
- throw new IllegalStateException ("You need to establish a connection first." );
1399
-
1400
- if (!authenticated )
1401
- throw new IllegalStateException ("The connection is not authenticated." );
1361
+ this .checkConnection ();
1402
1362
1403
1363
cm .requestCancelGlobalForward (bindPort );
1404
1364
}
@@ -1419,4 +1379,13 @@ public synchronized void setSecureRandom(SecureRandom rnd)
1419
1379
1420
1380
this .generator = rnd ;
1421
1381
}
1382
+
1383
+ private void checkConnection () throws IllegalStateException {
1384
+ if (tm == null ) {
1385
+ throw new IllegalStateException ("You need to establish a connection first." );
1386
+ }
1387
+ if (!authenticated ) {
1388
+ throw new IllegalStateException ("The connection is not authenticated." );
1389
+ }
1390
+ }
1422
1391
}
0 commit comments