1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
|
/*
* Copyright (c) 2009, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.net.ftp.impl;
import java.net.*;
import java.io.*;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.TimeZone;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import sun.net.ftp.*;
import sun.util.logging.PlatformLogger;
public class FtpClient extends sun.net.ftp.FtpClient {
private static int defaultSoTimeout;
private static int defaultConnectTimeout;
private static final PlatformLogger logger =
PlatformLogger.getLogger("sun.net.ftp.FtpClient");
private Proxy proxy;
private Socket server;
private PrintStream out;
private InputStream in;
private int readTimeout = -1;
private int connectTimeout = -1;
/* Name of encoding to use for output */
private static String encoding = "ISO8859_1";
/** remember the ftp server name because we may need it */
private InetSocketAddress serverAddr;
private boolean replyPending = false;
private boolean loggedIn = false;
private boolean useCrypto = false;
private SSLSocketFactory sslFact;
private Socket oldSocket;
/** Array of strings (usually 1 entry) for the last reply from the server. */
private Vector<String> serverResponse = new Vector<String>(1);
/** The last reply code from the ftp daemon. */
private FtpReplyCode lastReplyCode = null;
/** Welcome message from the server, if any. */
private String welcomeMsg;
/**
* Only passive mode used in JDK. See Bug 8010784.
*/
private final boolean passiveMode = true;
private TransferType type = TransferType.BINARY;
private long restartOffset = 0;
private long lastTransSize = -1; // -1 means 'unknown size'
private String lastFileName;
/**
* Static members used by the parser
*/
private static String[] patStrings = {
// drwxr-xr-x 1 user01 ftp 512 Jan 29 23:32 prog
"([\\-ld](?:[r\\-][w\\-][x\\-]){3})\\s*\\d+ (\\w+)\\s*(\\w+)\\s*(\\d+)\\s*([A-Z][a-z][a-z]\\s*\\d+)\\s*(\\d\\d:\\d\\d)\\s*(\\p{Print}*)",
// drwxr-xr-x 1 user01 ftp 512 Jan 29 1997 prog
"([\\-ld](?:[r\\-][w\\-][x\\-]){3})\\s*\\d+ (\\w+)\\s*(\\w+)\\s*(\\d+)\\s*([A-Z][a-z][a-z]\\s*\\d+)\\s*(\\d{4})\\s*(\\p{Print}*)",
// 04/28/2006 09:12a 3,563 genBuffer.sh
"(\\d{2}/\\d{2}/\\d{4})\\s*(\\d{2}:\\d{2}[ap])\\s*((?:[0-9,]+)|(?:<DIR>))\\s*(\\p{Graph}*)",
// 01-29-97 11:32PM <DIR> prog
"(\\d{2}-\\d{2}-\\d{2})\\s*(\\d{2}:\\d{2}[AP]M)\\s*((?:[0-9,]+)|(?:<DIR>))\\s*(\\p{Graph}*)"
};
private static int[][] patternGroups = {
// 0 - file, 1 - size, 2 - date, 3 - time, 4 - year, 5 - permissions,
// 6 - user, 7 - group
{7, 4, 5, 6, 0, 1, 2, 3},
{7, 4, 5, 0, 6, 1, 2, 3},
{4, 3, 1, 2, 0, 0, 0, 0},
{4, 3, 1, 2, 0, 0, 0, 0}};
private static Pattern[] patterns;
private static Pattern linkp = Pattern.compile("(\\p{Print}+) \\-\\> (\\p{Print}+)$");
private DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, java.util.Locale.US);
static {
final int vals[] = {0, 0};
final String encs[] = {null};
AccessController.doPrivileged(
new PrivilegedAction<Object>() {
public Object run() {
vals[0] = Integer.getInteger("sun.net.client.defaultReadTimeout", 0).intValue();
vals[1] = Integer.getInteger("sun.net.client.defaultConnectTimeout", 0).intValue();
encs[0] = System.getProperty("file.encoding", "ISO8859_1");
return null;
}
});
if (vals[0] == 0) {
defaultSoTimeout = -1;
} else {
defaultSoTimeout = vals[0];
}
if (vals[1] == 0) {
defaultConnectTimeout = -1;
} else {
defaultConnectTimeout = vals[1];
}
encoding = encs[0];
try {
if (!isASCIISuperset(encoding)) {
encoding = "ISO8859_1";
}
} catch (Exception e) {
encoding = "ISO8859_1";
}
patterns = new Pattern[patStrings.length];
for (int i = 0; i < patStrings.length; i++) {
patterns[i] = Pattern.compile(patStrings[i]);
}
}
/**
* Test the named character encoding to verify that it converts ASCII
* characters correctly. We have to use an ASCII based encoding, or else
* the NetworkClients will not work correctly in EBCDIC based systems.
* However, we cannot just use ASCII or ISO8859_1 universally, because in
* Asian locales, non-ASCII characters may be embedded in otherwise
* ASCII based protocols (eg. HTTP). The specifications (RFC2616, 2398)
* are a little ambiguous in this matter. For instance, RFC2398 [part 2.1]
* says that the HTTP request URI should be escaped using a defined
* mechanism, but there is no way to specify in the escaped string what
* the original character set is. It is not correct to assume that
* UTF-8 is always used (as in URLs in HTML 4.0). For this reason,
* until the specifications are updated to deal with this issue more
* comprehensively, and more importantly, HTTP servers are known to
* support these mechanisms, we will maintain the current behavior
* where it is possible to send non-ASCII characters in their original
* unescaped form.
*/
private static boolean isASCIISuperset(String encoding) throws Exception {
String chkS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
"abcdefghijklmnopqrstuvwxyz-_.!~*'();/?:@&=+$,";
// Expected byte sequence for string above
byte[] chkB = {48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, 71, 72,
73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, 98, 99,
100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114,
115, 116, 117, 118, 119, 120, 121, 122, 45, 95, 46, 33, 126, 42, 39, 40, 41, 59,
47, 63, 58, 64, 38, 61, 43, 36, 44};
byte[] b = chkS.getBytes(encoding);
return java.util.Arrays.equals(b, chkB);
}
private class DefaultParser implements FtpDirParser {
/**
* Possible patterns:
*
* drwxr-xr-x 1 user01 ftp 512 Jan 29 23:32 prog
* drwxr-xr-x 1 user01 ftp 512 Jan 29 1997 prog
* drwxr-xr-x 1 1 1 512 Jan 29 23:32 prog
* lrwxr-xr-x 1 user01 ftp 512 Jan 29 23:32 prog -> prog2000
* drwxr-xr-x 1 username ftp 512 Jan 29 23:32 prog
* -rw-r--r-- 1 jcc staff 105009 Feb 3 15:05 test.1
*
* 01-29-97 11:32PM <DIR> prog
* 04/28/2006 09:12a 3,563 genBuffer.sh
*
* drwxr-xr-x folder 0 Jan 29 23:32 prog
*
* 0 DIR 01-29-97 23:32 PROG
*/
private DefaultParser() {
}
public FtpDirEntry parseLine(String line) {
String fdate = null;
String fsize = null;
String time = null;
String filename = null;
String permstring = null;
String username = null;
String groupname = null;
boolean dir = false;
Calendar now = Calendar.getInstance();
int year = now.get(Calendar.YEAR);
Matcher m = null;
for (int j = 0; j < patterns.length; j++) {
m = patterns[j].matcher(line);
if (m.find()) {
// 0 - file, 1 - size, 2 - date, 3 - time, 4 - year,
// 5 - permissions, 6 - user, 7 - group
filename = m.group(patternGroups[j][0]);
fsize = m.group(patternGroups[j][1]);
fdate = m.group(patternGroups[j][2]);
if (patternGroups[j][4] > 0) {
fdate += (", " + m.group(patternGroups[j][4]));
} else if (patternGroups[j][3] > 0) {
fdate += (", " + String.valueOf(year));
}
if (patternGroups[j][3] > 0) {
time = m.group(patternGroups[j][3]);
}
if (patternGroups[j][5] > 0) {
permstring = m.group(patternGroups[j][5]);
dir = permstring.startsWith("d");
}
if (patternGroups[j][6] > 0) {
username = m.group(patternGroups[j][6]);
}
if (patternGroups[j][7] > 0) {
groupname = m.group(patternGroups[j][7]);
}
// Old DOS format
if ("<DIR>".equals(fsize)) {
dir = true;
fsize = null;
}
}
}
if (filename != null) {
Date d;
try {
d = df.parse(fdate);
} catch (Exception e) {
d = null;
}
if (d != null && time != null) {
int c = time.indexOf(":");
now.setTime(d);
now.set(Calendar.HOUR, Integer.parseInt(time.substring(0, c)));
now.set(Calendar.MINUTE, Integer.parseInt(time.substring(c + 1)));
d = now.getTime();
}
// see if it's a symbolic link, i.e. the name if followed
// by a -> and a path
Matcher m2 = linkp.matcher(filename);
if (m2.find()) {
// Keep only the name then
filename = m2.group(1);
}
boolean[][] perms = new boolean[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
perms[i][j] = (permstring.charAt((i * 3) + j) != '-');
}
}
FtpDirEntry file = new FtpDirEntry(filename);
file.setUser(username).setGroup(groupname);
file.setSize(Long.parseLong(fsize)).setLastModified(d);
file.setPermissions(perms);
file.setType(dir ? FtpDirEntry.Type.DIR : (line.charAt(0) == 'l' ? FtpDirEntry.Type.LINK : FtpDirEntry.Type.FILE));
return file;
}
return null;
}
}
private class MLSxParser implements FtpDirParser {
private SimpleDateFormat df = new SimpleDateFormat("yyyyMMddhhmmss");
public FtpDirEntry parseLine(String line) {
String name = null;
int i = line.lastIndexOf(";");
if (i > 0) {
name = line.substring(i + 1).trim();
line = line.substring(0, i);
} else {
name = line.trim();
line = "";
}
FtpDirEntry file = new FtpDirEntry(name);
while (!line.isEmpty()) {
String s;
i = line.indexOf(";");
if (i > 0) {
s = line.substring(0, i);
line = line.substring(i + 1);
} else {
s = line;
line = "";
}
i = s.indexOf("=");
if (i > 0) {
String fact = s.substring(0, i);
String value = s.substring(i + 1);
file.addFact(fact, value);
}
}
String s = file.getFact("Size");
if (s != null) {
file.setSize(Long.parseLong(s));
}
s = file.getFact("Modify");
if (s != null) {
Date d = null;
try {
d = df.parse(s);
} catch (ParseException ex) {
}
if (d != null) {
file.setLastModified(d);
}
}
s = file.getFact("Create");
if (s != null) {
Date d = null;
try {
d = df.parse(s);
} catch (ParseException ex) {
}
if (d != null) {
file.setCreated(d);
}
}
s = file.getFact("Type");
if (s != null) {
if (s.equalsIgnoreCase("file")) {
file.setType(FtpDirEntry.Type.FILE);
}
if (s.equalsIgnoreCase("dir")) {
file.setType(FtpDirEntry.Type.DIR);
}
if (s.equalsIgnoreCase("cdir")) {
file.setType(FtpDirEntry.Type.CDIR);
}
if (s.equalsIgnoreCase("pdir")) {
file.setType(FtpDirEntry.Type.PDIR);
}
}
return file;
}
};
private FtpDirParser parser = new DefaultParser();
private FtpDirParser mlsxParser = new MLSxParser();
private static Pattern transPat = null;
private void getTransferSize() {
lastTransSize = -1;
/**
* If it's a start of data transfer response, let's try to extract
* the size from the response string. Usually it looks like that:
*
* 150 Opening BINARY mode data connection for foo (6701 bytes).
*/
String response = getLastResponseString();
if (transPat == null) {
transPat = Pattern.compile("150 Opening .*\\((\\d+) bytes\\).");
}
Matcher m = transPat.matcher(response);
if (m.find()) {
String s = m.group(1);
lastTransSize = Long.parseLong(s);
}
}
/**
* extract the created file name from the response string:
* 226 Transfer complete (unique file name:toto.txt.1).
* Usually happens when a STOU (store unique) command had been issued.
*/
private void getTransferName() {
lastFileName = null;
String response = getLastResponseString();
int i = response.indexOf("unique file name:");
int e = response.lastIndexOf(')');
if (i >= 0) {
i += 17; // Length of "unique file name:"
lastFileName = response.substring(i, e);
}
}
/**
* Pulls the response from the server and returns the code as a
* number. Returns -1 on failure.
*/
private int readServerResponse() throws IOException {
StringBuffer replyBuf = new StringBuffer(32);
int c;
int continuingCode = -1;
int code;
String response;
serverResponse.setSize(0);
while (true) {
while ((c = in.read()) != -1) {
if (c == '\r') {
if ((c = in.read()) != '\n') {
replyBuf.append('\r');
}
}
replyBuf.append((char) c);
if (c == '\n') {
break;
}
}
response = replyBuf.toString();
replyBuf.setLength(0);
if (logger.isLoggable(PlatformLogger.Level.FINEST)) {
logger.finest("Server [" + serverAddr + "] --> " + response);
}
if (response.length() == 0) {
code = -1;
} else {
try {
code = Integer.parseInt(response.substring(0, 3));
} catch (NumberFormatException e) {
code = -1;
} catch (StringIndexOutOfBoundsException e) {
/* this line doesn't contain a response code, so
we just completely ignore it */
continue;
}
}
serverResponse.addElement(response);
if (continuingCode != -1) {
/* we've seen a ###- sequence */
if (code != continuingCode ||
(response.length() >= 4 && response.charAt(3) == '-')) {
continue;
} else {
/* seen the end of code sequence */
continuingCode = -1;
break;
}
} else if (response.length() >= 4 && response.charAt(3) == '-') {
continuingCode = code;
continue;
} else {
break;
}
}
return code;
}
/** Sends command <i>cmd</i> to the server. */
private void sendServer(String cmd) {
out.print(cmd);
if (logger.isLoggable(PlatformLogger.Level.FINEST)) {
logger.finest("Server [" + serverAddr + "] <-- " + cmd);
}
}
/** converts the server response into a string. */
private String getResponseString() {
return serverResponse.elementAt(0);
}
/** Returns all server response strings. */
private Vector<String> getResponseStrings() {
return serverResponse;
}
/**
* Read the reply from the FTP server.
*
* @return <code>true</code> if the command was successful
* @throws IOException if an error occurred
*/
private boolean readReply() throws IOException {
lastReplyCode = FtpReplyCode.find(readServerResponse());
if (lastReplyCode.isPositivePreliminary()) {
replyPending = true;
return true;
}
if (lastReplyCode.isPositiveCompletion() || lastReplyCode.isPositiveIntermediate()) {
if (lastReplyCode == FtpReplyCode.CLOSING_DATA_CONNECTION) {
getTransferName();
}
return true;
}
return false;
}
/**
* Sends a command to the FTP server and returns the error code
* (which can be a "success") sent by the server.
*
* @param cmd
* @return <code>true</code> if the command was successful
* @throws IOException
*/
// Android-changed: Integrate upstream fix to guard against '\n'.
// Integrates OpenJDK's "8170222: Better transfers of files".
// See http://b/35784677 .
// private boolean issueCommand(String cmd) throws IOException {
private boolean issueCommand(String cmd) throws IOException,
sun.net.ftp.FtpProtocolException {
if (!isConnected()) {
throw new IllegalStateException("Not connected");
}
if (replyPending) {
try {
completePending();
} catch (sun.net.ftp.FtpProtocolException e) {
// ignore...
}
}
// BEGIN Android-added: Integrate upstream fix to guard against '\n'.
if (cmd.indexOf('\n') != -1) {
sun.net.ftp.FtpProtocolException ex
= new sun.net.ftp.FtpProtocolException("Illegal FTP command");
ex.initCause(new IllegalArgumentException("Illegal carriage return"));
throw ex;
}
// END Android-added: Integrate upstream fix to guard against '\n'.
sendServer(cmd + "\r\n");
return readReply();
}
/**
* Send a command to the FTP server and check for success.
*
* @param cmd String containing the command
*
* @throws FtpProtocolException if an error occurred
*/
private void issueCommandCheck(String cmd) throws sun.net.ftp.FtpProtocolException, IOException {
if (!issueCommand(cmd)) {
throw new sun.net.ftp.FtpProtocolException(cmd + ":" + getResponseString(), getLastReplyCode());
}
}
private static Pattern epsvPat = null;
private static Pattern pasvPat = null;
/**
* Opens a "PASSIVE" connection with the server and returns the connected
* <code>Socket</code>.
*
* @return the connected <code>Socket</code>
* @throws IOException if the connection was unsuccessful.
*/
private Socket openPassiveDataConnection(String cmd) throws sun.net.ftp.FtpProtocolException, IOException {
String serverAnswer;
int port;
InetSocketAddress dest = null;
/**
* Here is the idea:
*
* - First we want to try the new (and IPv6 compatible) EPSV command
* But since we want to be nice with NAT software, we'll issue the
* EPSV ALL command first.
* EPSV is documented in RFC2428
* - If EPSV fails, then we fall back to the older, yet ok, PASV
* - If PASV fails as well, then we throw an exception and the calling
* method will have to try the EPRT or PORT command
*/
if (issueCommand("EPSV ALL")) {
// We can safely use EPSV commands
issueCommandCheck("EPSV");
serverAnswer = getResponseString();
// The response string from a EPSV command will contain the port number
// the format will be :
// 229 Entering Extended PASSIVE Mode (|||58210|)
//
// So we'll use the regular expresions package to parse the output.
if (epsvPat == null) {
epsvPat = Pattern.compile("^229 .* \\(\\|\\|\\|(\\d+)\\|\\)");
}
Matcher m = epsvPat.matcher(serverAnswer);
if (!m.find()) {
throw new sun.net.ftp.FtpProtocolException("EPSV failed : " + serverAnswer);
}
// Yay! Let's extract the port number
String s = m.group(1);
port = Integer.parseInt(s);
InetAddress add = server.getInetAddress();
if (add != null) {
dest = new InetSocketAddress(add, port);
} else {
// This means we used an Unresolved address to connect in
// the first place. Most likely because the proxy is doing
// the name resolution for us, so let's keep using unresolved
// address.
dest = InetSocketAddress.createUnresolved(serverAddr.getHostName(), port);
}
} else {
// EPSV ALL failed, so Let's try the regular PASV cmd
issueCommandCheck("PASV");
serverAnswer = getResponseString();
// Let's parse the response String to get the IP & port to connect
// to. The String should be in the following format :
//
// 227 Entering PASSIVE Mode (A1,A2,A3,A4,p1,p2)
//
// Note that the two parenthesis are optional
//
// The IP address is A1.A2.A3.A4 and the port is p1 * 256 + p2
//
// The regular expression is a bit more complex this time, because
// the parenthesis are optionals and we have to use 3 groups.
if (pasvPat == null) {
pasvPat = Pattern.compile("227 .* \\(?(\\d{1,3},\\d{1,3},\\d{1,3},\\d{1,3}),(\\d{1,3}),(\\d{1,3})\\)?");
}
Matcher m = pasvPat.matcher(serverAnswer);
if (!m.find()) {
throw new sun.net.ftp.FtpProtocolException("PASV failed : " + serverAnswer);
}
// Get port number out of group 2 & 3
port = Integer.parseInt(m.group(3)) + (Integer.parseInt(m.group(2)) << 8);
// IP address is simple
String s = m.group(1).replace(',', '.');
dest = new InetSocketAddress(s, port);
}
// Got everything, let's open the socket!
Socket s;
if (proxy != null) {
if (proxy.type() == Proxy.Type.SOCKS) {
s = AccessController.doPrivileged(
new PrivilegedAction<Socket>() {
public Socket run() {
return new Socket(proxy);
}
});
} else {
s = new Socket(Proxy.NO_PROXY);
}
} else {
s = new Socket();
}
InetAddress serverAddress = AccessController.doPrivileged(
new PrivilegedAction<InetAddress>() {
@Override
public InetAddress run() {
return server.getLocalAddress();
}
});
// Bind the socket to the same address as the control channel. This
// is needed in case of multi-homed systems.
s.bind(new InetSocketAddress(serverAddress, 0));
if (connectTimeout >= 0) {
s.connect(dest, connectTimeout);
} else {
if (defaultConnectTimeout > 0) {
s.connect(dest, defaultConnectTimeout);
} else {
s.connect(dest);
}
}
if (readTimeout >= 0) {
s.setSoTimeout(readTimeout);
} else if (defaultSoTimeout > 0) {
s.setSoTimeout(defaultSoTimeout);
}
if (useCrypto) {
try {
s = sslFact.createSocket(s, dest.getHostName(), dest.getPort(), true);
} catch (Exception e) {
throw new sun.net.ftp.FtpProtocolException("Can't open secure data channel: " + e);
}
}
if (!issueCommand(cmd)) {
s.close();
if (getLastReplyCode() == FtpReplyCode.FILE_UNAVAILABLE) {
// Ensure backward compatibility
throw new FileNotFoundException(cmd);
}
throw new sun.net.ftp.FtpProtocolException(cmd + ":" + getResponseString(), getLastReplyCode());
}
return s;
}
/**
* Opens a data connection with the server according to the set mode
* (ACTIVE or PASSIVE) then send the command passed as an argument.
*
* @param cmd the <code>String</code> containing the command to execute
* @return the connected <code>Socket</code>
* @throws IOException if the connection or command failed
*/
private Socket openDataConnection(String cmd) throws sun.net.ftp.FtpProtocolException, IOException {
Socket clientSocket;
if (passiveMode) {
try {
return openPassiveDataConnection(cmd);
} catch (sun.net.ftp.FtpProtocolException e) {
// If Passive mode failed, fall back on PORT
// Otherwise throw exception
String errmsg = e.getMessage();
if (!errmsg.startsWith("PASV") && !errmsg.startsWith("EPSV")) {
throw e;
}
}
}
ServerSocket portSocket;
InetAddress myAddress;
String portCmd;
if (proxy != null && proxy.type() == Proxy.Type.SOCKS) {
// We're behind a firewall and the passive mode fail,
// since we can't accept a connection through SOCKS (yet)
// throw an exception
throw new sun.net.ftp.FtpProtocolException("Passive mode failed");
}
// Bind the ServerSocket to the same address as the control channel
// This is needed for multi-homed systems
portSocket = new ServerSocket(0, 1, server.getLocalAddress());
try {
myAddress = portSocket.getInetAddress();
if (myAddress.isAnyLocalAddress()) {
myAddress = server.getLocalAddress();
}
// Let's try the new, IPv6 compatible EPRT command
// See RFC2428 for specifics
// Some FTP servers (like the one on Solaris) are bugged, they
// will accept the EPRT command but then, the subsequent command
// (e.g. RETR) will fail, so we have to check BOTH results (the
// EPRT cmd then the actual command) to decide whether we should
// fall back on the older PORT command.
portCmd = "EPRT |" + ((myAddress instanceof Inet6Address) ? "2" : "1") + "|" +
myAddress.getHostAddress() + "|" + portSocket.getLocalPort() + "|";
if (!issueCommand(portCmd) || !issueCommand(cmd)) {
// The EPRT command failed, let's fall back to good old PORT
portCmd = "PORT ";
byte[] addr = myAddress.getAddress();
/* append host addr */
for (int i = 0; i < addr.length; i++) {
portCmd = portCmd + (addr[i] & 0xFF) + ",";
}
/* append port number */
portCmd = portCmd + ((portSocket.getLocalPort() >>> 8) & 0xff) + "," + (portSocket.getLocalPort() & 0xff);
issueCommandCheck(portCmd);
issueCommandCheck(cmd);
}
// Either the EPRT or the PORT command was successful
// Let's create the client socket
if (connectTimeout >= 0) {
portSocket.setSoTimeout(connectTimeout);
} else {
if (defaultConnectTimeout > 0) {
portSocket.setSoTimeout(defaultConnectTimeout);
}
}
clientSocket = portSocket.accept();
if (readTimeout >= 0) {
clientSocket.setSoTimeout(readTimeout);
} else {
if (defaultSoTimeout > 0) {
clientSocket.setSoTimeout(defaultSoTimeout);
}
}
} finally {
portSocket.close();
}
if (useCrypto) {
try {
clientSocket = sslFact.createSocket(clientSocket, serverAddr.getHostName(), serverAddr.getPort(), true);
} catch (Exception ex) {
throw new IOException(ex.getLocalizedMessage());
}
}
return clientSocket;
}
private InputStream createInputStream(InputStream in) {
if (type == TransferType.ASCII) {
return new sun.net.TelnetInputStream(in, false);
}
return in;
}
private OutputStream createOutputStream(OutputStream out) {
if (type == TransferType.ASCII) {
return new sun.net.TelnetOutputStream(out, false);
}
return out;
}
/**
* Creates an instance of FtpClient. The client is not connected to any
* server yet.
*
*/
protected FtpClient() {
}
/**
* Creates an instance of FtpClient. The client is not connected to any
* server yet.
*
*/
public static sun.net.ftp.FtpClient create() {
return new FtpClient();
}
/**
* Set the transfer mode to <I>passive</I>. In that mode, data connections
* are established by having the client connect to the server.
* This is the recommended default mode as it will work best through
* firewalls and NATs.
*
* @return This FtpClient
* @see #setActiveMode()
*/
public sun.net.ftp.FtpClient enablePassiveMode(boolean passive) {
// Only passive mode used in JDK. See Bug 8010784.
// passiveMode = passive;
return this;
}
/**
* Gets the current transfer mode.
*
* @return the current <code>FtpTransferMode</code>
*/
public boolean isPassiveModeEnabled() {
return passiveMode;
}
/**
* Sets the timeout value to use when connecting to the server,
*
* @param timeout the timeout value, in milliseconds, to use for the connect
* operation. A value of zero or less, means use the default timeout.
*
* @return This FtpClient
*/
public sun.net.ftp.FtpClient setConnectTimeout(int timeout) {
connectTimeout = timeout;
return this;
}
/**
* Returns the current connection timeout value.
*
* @return the value, in milliseconds, of the current connect timeout.
* @see #setConnectTimeout(int)
*/
public int getConnectTimeout() {
return connectTimeout;
}
/**
* Sets the timeout value to use when reading from the server,
*
* @param timeout the timeout value, in milliseconds, to use for the read
* operation. A value of zero or less, means use the default timeout.
* @return This FtpClient
*/
public sun.net.ftp.FtpClient setReadTimeout(int timeout) {
readTimeout = timeout;
return this;
}
/**
* Returns the current read timeout value.
*
* @return the value, in milliseconds, of the current read timeout.
* @see #setReadTimeout(int)
*/
public int getReadTimeout() {
return readTimeout;
}
public sun.net.ftp.FtpClient setProxy(Proxy p) {
proxy = p;
return this;
}
/**
* Get the proxy of this FtpClient
*
* @return the <code>Proxy</code>, this client is using, or <code>null</code>
* if none is used.
* @see #setProxy(Proxy)
*/
public Proxy getProxy() {
return proxy;
}
/**
* Connects to the specified destination.
*
* @param dest the <code>InetSocketAddress</code> to connect to.
* @throws IOException if the connection fails.
*/
private void tryConnect(InetSocketAddress dest, int timeout) throws IOException {
if (isConnected()) {
disconnect();
}
server = doConnect(dest, timeout);
try {
out = new PrintStream(new BufferedOutputStream(server.getOutputStream()),
true, encoding);
} catch (UnsupportedEncodingException e) {
throw new InternalError(encoding + "encoding not found", e);
}
in = new BufferedInputStream(server.getInputStream());
}
private Socket doConnect(InetSocketAddress dest, int timeout) throws IOException {
Socket s;
if (proxy != null) {
if (proxy.type() == Proxy.Type.SOCKS) {
s = AccessController.doPrivileged(
new PrivilegedAction<Socket>() {
public Socket run() {
return new Socket(proxy);
}
});
} else {
s = new Socket(Proxy.NO_PROXY);
}
} else {
s = new Socket();
}
// Instance specific timeouts do have priority, that means
// connectTimeout & readTimeout (-1 means not set)
// Then global default timeouts
// Then no timeout.
if (timeout >= 0) {
s.connect(dest, timeout);
} else {
if (connectTimeout >= 0) {
s.connect(dest, connectTimeout);
} else {
if (defaultConnectTimeout > 0) {
s.connect(dest, defaultConnectTimeout);
} else {
s.connect(dest);
}
}
}
if (readTimeout >= 0) {
s.setSoTimeout(readTimeout);
} else if (defaultSoTimeout > 0) {
s.setSoTimeout(defaultSoTimeout);
}
return s;
}
private void disconnect() throws IOException {
if (isConnected()) {
server.close();
}
server = null;
in = null;
out = null;
lastTransSize = -1;
lastFileName = null;
restartOffset = 0;
welcomeMsg = null;
lastReplyCode = null;
serverResponse.setSize(0);
}
/**
* Tests whether this client is connected or not to a server.
*
* @return <code>true</code> if the client is connected.
*/
public boolean isConnected() {
return server != null;
}
public SocketAddress getServerAddress() {
return server == null ? null : server.getRemoteSocketAddress();
}
public sun.net.ftp.FtpClient connect(SocketAddress dest) throws sun.net.ftp.FtpProtocolException, IOException {
return connect(dest, -1);
}
/**
* Connects the FtpClient to the specified destination.
*
* @param dest the address of the destination server
* @throws IOException if connection failed.
*/
public sun.net.ftp.FtpClient connect(SocketAddress dest, int timeout) throws sun.net.ftp.FtpProtocolException, IOException {
if (!(dest instanceof InetSocketAddress)) {
throw new IllegalArgumentException("Wrong address type");
}
serverAddr = (InetSocketAddress) dest;
tryConnect(serverAddr, timeout);
if (!readReply()) {
throw new sun.net.ftp.FtpProtocolException("Welcome message: " +
getResponseString(), lastReplyCode);
}
welcomeMsg = getResponseString().substring(4);
return this;
}
private void tryLogin(String user, char[] password) throws sun.net.ftp.FtpProtocolException, IOException {
issueCommandCheck("USER " + user);
/*
* Checks for "331 User name okay, need password." answer
*/
if (lastReplyCode == FtpReplyCode.NEED_PASSWORD) {
if ((password != null) && (password.length > 0)) {
issueCommandCheck("PASS " + String.valueOf(password));
}
}
}
/**
* Attempts to log on the server with the specified user name and password.
*
* @param user The user name
* @param password The password for that user
* @return <code>true</code> if the login was successful.
* @throws IOException if an error occurred during the transmission
*/
public sun.net.ftp.FtpClient login(String user, char[] password) throws sun.net.ftp.FtpProtocolException, IOException {
if (!isConnected()) {
throw new sun.net.ftp.FtpProtocolException("Not connected yet", FtpReplyCode.BAD_SEQUENCE);
}
if (user == null || user.length() == 0) {
throw new IllegalArgumentException("User name can't be null or empty");
}
tryLogin(user, password);
// keep the welcome message around so we can
// put it in the resulting HTML page.
String l;
StringBuffer sb = new StringBuffer();
for (int i = 0; i < serverResponse.size(); i++) {
l = serverResponse.elementAt(i);
if (l != null) {
if (l.length() >= 4 && l.startsWith("230")) {
// get rid of the "230-" prefix
l = l.substring(4);
}
sb.append(l);
}
}
welcomeMsg = sb.toString();
loggedIn = true;
return this;
}
/**
* Attempts to log on the server with the specified user name, password and
* account name.
*
* @param user The user name
* @param password The password for that user.
* @param account The account name for that user.
* @return <code>true</code> if the login was successful.
* @throws IOException if an error occurs during the transmission.
*/
public sun.net.ftp.FtpClient login(String user, char[] password, String account) throws sun.net.ftp.FtpProtocolException, IOException {
if (!isConnected()) {
throw new sun.net.ftp.FtpProtocolException("Not connected yet", FtpReplyCode.BAD_SEQUENCE);
}
if (user == null || user.length() == 0) {
throw new IllegalArgumentException("User name can't be null or empty");
}
tryLogin(user, password);
/*
* Checks for "332 Need account for login." answer
*/
if (lastReplyCode == FtpReplyCode.NEED_ACCOUNT) {
issueCommandCheck("ACCT " + account);
}
// keep the welcome message around so we can
// put it in the resulting HTML page.
StringBuffer sb = new StringBuffer();
if (serverResponse != null) {
for (String l : serverResponse) {
if (l != null) {
if (l.length() >= 4 && l.startsWith("230")) {
// get rid of the "230-" prefix
l = l.substring(4);
}
sb.append(l);
}
}
}
welcomeMsg = sb.toString();
loggedIn = true;
return this;
}
/**
* Logs out the current user. This is in effect terminates the current
* session and the connection to the server will be closed.
*
*/
public void close() throws IOException {
if (isConnected()) {
try {
issueCommand("QUIT");
} catch (FtpProtocolException e) {
}
loggedIn = false;
}
disconnect();
}
/**
* Checks whether the client is logged in to the server or not.
*
* @return <code>true</code> if the client has already completed a login.
*/
public boolean isLoggedIn() {
return loggedIn;
}
/**
* Changes to a specific directory on a remote FTP server
*
* @param remoteDirectory path of the directory to CD to.
* @return <code>true</code> if the operation was successful.
* @exception <code>FtpProtocolException</code>
*/
public sun.net.ftp.FtpClient changeDirectory(String remoteDirectory) throws sun.net.ftp.FtpProtocolException, IOException {
if (remoteDirectory == null || "".equals(remoteDirectory)) {
throw new IllegalArgumentException("directory can't be null or empty");
}
issueCommandCheck("CWD " + remoteDirectory);
return this;
}
/**
* Changes to the parent directory, sending the CDUP command to the server.
*
* @return <code>true</code> if the command was successful.
* @throws IOException
*/
public sun.net.ftp.FtpClient changeToParentDirectory() throws sun.net.ftp.FtpProtocolException, IOException {
issueCommandCheck("CDUP");
return this;
}
/**
* Returns the server current working directory, or <code>null</code> if
* the PWD command failed.
*
* @return a <code>String</code> containing the current working directory,
* or <code>null</code>
* @throws IOException
*/
public String getWorkingDirectory() throws sun.net.ftp.FtpProtocolException, IOException {
issueCommandCheck("PWD");
/*
* answer will be of the following format :
*
* 257 "/" is current directory.
*/
String answ = getResponseString();
if (!answ.startsWith("257")) {
return null;
}
return answ.substring(5, answ.lastIndexOf('"'));
}
/**
* Sets the restart offset to the specified value. That value will be
* sent through a <code>REST</code> command to server before a file
* transfer and has the effect of resuming a file transfer from the
* specified point. After a transfer the restart offset is set back to
* zero.
*
* @param offset the offset in the remote file at which to start the next
* transfer. This must be a value greater than or equal to zero.
* @throws IllegalArgumentException if the offset is negative.
*/
public sun.net.ftp.FtpClient setRestartOffset(long offset) {
if (offset < 0) {
throw new IllegalArgumentException("offset can't be negative");
}
restartOffset = offset;
return this;
}
/**
* Retrieves a file from the ftp server and writes it to the specified
* <code>OutputStream</code>.
* If the restart offset was set, then a <code>REST</code> command will be
* sent before the RETR in order to restart the tranfer from the specified
* offset.
* The <code>OutputStream</code> is not closed by this method at the end
* of the transfer.
*
* @param name a <code>String<code> containing the name of the file to
* retreive from the server.
* @param local the <code>OutputStream</code> the file should be written to.
* @throws IOException if the transfer fails.
*/
public sun.net.ftp.FtpClient getFile(String name, OutputStream local) throws sun.net.ftp.FtpProtocolException, IOException {
int mtu = 1500;
if (restartOffset > 0) {
Socket s;
try {
s = openDataConnection("REST " + restartOffset);
} finally {
restartOffset = 0;
}
issueCommandCheck("RETR " + name);
getTransferSize();
InputStream remote = createInputStream(s.getInputStream());
byte[] buf = new byte[mtu * 10];
int l;
while ((l = remote.read(buf)) >= 0) {
if (l > 0) {
local.write(buf, 0, l);
}
}
remote.close();
} else {
Socket s = openDataConnection("RETR " + name);
getTransferSize();
InputStream remote = createInputStream(s.getInputStream());
byte[] buf = new byte[mtu * 10];
int l;
while ((l = remote.read(buf)) >= 0) {
if (l > 0) {
local.write(buf, 0, l);
}
}
remote.close();
}
return completePending();
}
/**
* Retrieves a file from the ftp server, using the RETR command, and
* returns the InputStream from* the established data connection.
* {@link #completePending()} <b>has</b> to be called once the application
* is done reading from the returned stream.
*
* @param name the name of the remote file
* @return the {@link java.io.InputStream} from the data connection, or
* <code>null</code> if the command was unsuccessful.
* @throws IOException if an error occurred during the transmission.
*/
public InputStream getFileStream(String name) throws sun.net.ftp.FtpProtocolException, IOException {
Socket s;
if (restartOffset > 0) {
try {
s = openDataConnection("REST " + restartOffset);
} finally {
restartOffset = 0;
}
if (s == null) {
return null;
}
issueCommandCheck("RETR " + name);
getTransferSize();
return createInputStream(s.getInputStream());
}
s = openDataConnection("RETR " + name);
if (s == null) {
return null;
}
getTransferSize();
return createInputStream(s.getInputStream());
}
/**
* Transfers a file from the client to the server (aka a <I>put</I>)
* by sending the STOR or STOU command, depending on the
* <code>unique</code> argument, and returns the <code>OutputStream</code>
* from the established data connection.
* {@link #completePending()} <b>has</b> to be called once the application
* is finished writing to the stream.
*
* A new file is created at the server site if the file specified does
* not already exist.
*
* If <code>unique</code> is set to <code>true</code>, the resultant file
* is to be created under a name unique to that directory, meaning
* it will not overwrite an existing file, instead the server will
* generate a new, unique, file name.
* The name of the remote file can be retrieved, after completion of the
* transfer, by calling {@link #getLastFileName()}.
*
* @param name the name of the remote file to write.
* @param unique <code>true</code> if the remote files should be unique,
* in which case the STOU command will be used.
* @return the {@link java.io.OutputStream} from the data connection or
* <code>null</code> if the command was unsuccessful.
* @throws IOException if an error occurred during the transmission.
*/
public OutputStream putFileStream(String name, boolean unique)
throws sun.net.ftp.FtpProtocolException, IOException
{
String cmd = unique ? "STOU " : "STOR ";
Socket s = openDataConnection(cmd + name);
if (s == null) {
return null;
}
boolean bm = (type == TransferType.BINARY);
return new sun.net.TelnetOutputStream(s.getOutputStream(), bm);
}
/**
* Transfers a file from the client to the server (aka a <I>put</I>)
* by sending the STOR command. The content of the <code>InputStream</code>
* passed in argument is written into the remote file, overwriting any
* existing data.
*
* A new file is created at the server site if the file specified does
* not already exist.
*
* @param name the name of the remote file to write.
* @param local the <code>InputStream</code> that points to the data to
* transfer.
* @param unique <code>true</code> if the remote file should be unique
* (i.e. not already existing), <code>false</code> otherwise.
* @return <code>true</code> if the transfer was successful.
* @throws IOException if an error occurred during the transmission.
* @see #getLastFileName()
*/
public sun.net.ftp.FtpClient putFile(String name, InputStream local, boolean unique) throws sun.net.ftp.FtpProtocolException, IOException {
String cmd = unique ? "STOU " : "STOR ";
int mtu = 1500;
if (type == TransferType.BINARY) {
Socket s = openDataConnection(cmd + name);
OutputStream remote = createOutputStream(s.getOutputStream());
byte[] buf = new byte[mtu * 10];
int l;
while ((l = local.read(buf)) >= 0) {
if (l > 0) {
remote.write(buf, 0, l);
}
}
remote.close();
}
return completePending();
}
/**
* Sends the APPE command to the server in order to transfer a data stream
* passed in argument and append it to the content of the specified remote
* file.
*
* @param name A <code>String</code> containing the name of the remote file
* to append to.
* @param local The <code>InputStream</code> providing access to the data
* to be appended.
* @return <code>true</code> if the transfer was successful.
* @throws IOException if an error occurred during the transmission.
*/
public sun.net.ftp.FtpClient appendFile(String name, InputStream local) throws sun.net.ftp.FtpProtocolException, IOException {
int mtu = 1500;
Socket s = openDataConnection("APPE " + name);
OutputStream remote = createOutputStream(s.getOutputStream());
byte[] buf = new byte[mtu * 10];
int l;
while ((l = local.read(buf)) >= 0) {
if (l > 0) {
remote.write(buf, 0, l);
}
}
remote.close();
return completePending();
}
/**
* Renames a file on the server.
*
* @param from the name of the file being renamed
* @param to the new name for the file
* @throws IOException if the command fails
*/
public sun.net.ftp.FtpClient rename(String from, String to) throws sun.net.ftp.FtpProtocolException, IOException {
issueCommandCheck("RNFR " + from);
issueCommandCheck("RNTO " + to);
return this;
}
/**
* Deletes a file on the server.
*
* @param name a <code>String</code> containing the name of the file
* to delete.
* @return <code>true</code> if the command was successful
* @throws IOException if an error occurred during the exchange
*/
public sun.net.ftp.FtpClient deleteFile(String name) throws sun.net.ftp.FtpProtocolException, IOException {
issueCommandCheck("DELE " + name);
return this;
}
/**
* Creates a new directory on the server.
*
* @param name a <code>String</code> containing the name of the directory
* to create.
* @return <code>true</code> if the operation was successful.
* @throws IOException if an error occurred during the exchange
*/
public sun.net.ftp.FtpClient makeDirectory(String name) throws sun.net.ftp.FtpProtocolException, IOException {
issueCommandCheck("MKD " + name);
return this;
}
/**
* Removes a directory on the server.
*
* @param name a <code>String</code> containing the name of the directory
* to remove.
*
* @return <code>true</code> if the operation was successful.
* @throws IOException if an error occurred during the exchange.
*/
public sun.net.ftp.FtpClient removeDirectory(String name) throws sun.net.ftp.FtpProtocolException, IOException {
issueCommandCheck("RMD " + name);
return this;
}
/**
* Sends a No-operation command. It's useful for testing the connection
* status or as a <I>keep alive</I> mechanism.
*
* @throws FtpProtocolException if the command fails
*/
public sun.net.ftp.FtpClient noop() throws sun.net.ftp.FtpProtocolException, IOException {
issueCommandCheck("NOOP");
return this;
}
/**
* Sends the STAT command to the server.
* This can be used while a data connection is open to get a status
* on the current transfer, in that case the parameter should be
* <code>null</code>.
* If used between file transfers, it may have a pathname as argument
* in which case it will work as the LIST command except no data
* connection will be created.
*
* @param name an optional <code>String</code> containing the pathname
* the STAT command should apply to.
* @return the response from the server or <code>null</code> if the
* command failed.
* @throws IOException if an error occurred during the transmission.
*/
public String getStatus(String name) throws sun.net.ftp.FtpProtocolException, IOException {
issueCommandCheck((name == null ? "STAT" : "STAT " + name));
/*
* A typical response will be:
* 213-status of t32.gif:
* -rw-r--r-- 1 jcc staff 247445 Feb 17 1998 t32.gif
* 213 End of Status
*
* or
*
* 211-jsn FTP server status:
* Version wu-2.6.2+Sun
* Connected to localhost (::1)
* Logged in as jccollet
* TYPE: ASCII, FORM: Nonprint; STRUcture: File; transfer MODE: Stream
* No data connection
* 0 data bytes received in 0 files
* 0 data bytes transmitted in 0 files
* 0 data bytes total in 0 files
* 53 traffic bytes received in 0 transfers
* 485 traffic bytes transmitted in 0 transfers
* 587 traffic bytes total in 0 transfers
* 211 End of status
*
* So we need to remove the 1st and last line
*/
Vector<String> resp = getResponseStrings();
StringBuffer sb = new StringBuffer();
for (int i = 1; i < resp.size() - 1; i++) {
sb.append(resp.get(i));
}
return sb.toString();
}
/**
* Sends the FEAT command to the server and returns the list of supported
* features in the form of strings.
*
* The features are the supported commands, like AUTH TLS, PROT or PASV.
* See the RFCs for a complete list.
*
* Note that not all FTP servers support that command, in which case
* the method will return <code>null</code>
*
* @return a <code>List</code> of <code>Strings</code> describing the
* supported additional features, or <code>null</code>
* if the command is not supported.
* @throws IOException if an error occurs during the transmission.
*/
public List<String> getFeatures() throws sun.net.ftp.FtpProtocolException, IOException {
/*
* The FEAT command, when implemented will return something like:
*
* 211-Features:
* AUTH TLS
* PBSZ
* PROT
* EPSV
* EPRT
* PASV
* REST STREAM
* 211 END
*/
ArrayList<String> features = new ArrayList<String>();
issueCommandCheck("FEAT");
Vector<String> resp = getResponseStrings();
// Note that we start at index 1 to skip the 1st line (211-...)
// and we stop before the last line.
for (int i = 1; i < resp.size() - 1; i++) {
String s = resp.get(i);
// Get rid of leading space and trailing newline
features.add(s.substring(1, s.length() - 1));
}
return features;
}
/**
* sends the ABOR command to the server.
* It tells the server to stop the previous command or transfer.
*
* @return <code>true</code> if the command was successful.
* @throws IOException if an error occurred during the transmission.
*/
public sun.net.ftp.FtpClient abort() throws sun.net.ftp.FtpProtocolException, IOException {
issueCommandCheck("ABOR");
// TODO: Must check the ReplyCode:
/*
* From the RFC:
* There are two cases for the server upon receipt of this
* command: (1) the FTP service command was already completed,
* or (2) the FTP service command is still in progress.
* In the first case, the server closes the data connection
* (if it is open) and responds with a 226 reply, indicating
* that the abort command was successfully processed.
* In the second case, the server aborts the FTP service in
* progress and closes the data connection, returning a 426
* reply to indicate that the service request terminated
* abnormally. The server then sends a 226 reply,
* indicating that the abort command was successfully
* processed.
*/
return this;
}
/**
* Some methods do not wait until completion before returning, so this
* method can be called to wait until completion. This is typically the case
* with commands that trigger a transfer like {@link #getFileStream(String)}.
* So this method should be called before accessing information related to
* such a command.
* <p>This method will actually block reading on the command channel for a
* notification from the server that the command is finished. Such a
* notification often carries extra information concerning the completion
* of the pending action (e.g. number of bytes transfered).</p>
* <p>Note that this will return true immediately if no command or action
* is pending</p>
* <p>It should be also noted that most methods issuing commands to the ftp
* server will call this method if a previous command is pending.
* <p>Example of use:
* <pre>
* InputStream in = cl.getFileStream("file");
* ...
* cl.completePending();
* long size = cl.getLastTransferSize();
* </pre>
* On the other hand, it's not necessary in a case like:
* <pre>
* InputStream in = cl.getFileStream("file");
* // read content
* ...
* cl.logout();
* </pre>
* <p>Since {@link #logout()} will call completePending() if necessary.</p>
* @return <code>true</code> if the completion was successful or if no
* action was pending.
* @throws IOException
*/
public sun.net.ftp.FtpClient completePending() throws sun.net.ftp.FtpProtocolException, IOException {
while (replyPending) {
replyPending = false;
if (!readReply()) {
throw new sun.net.ftp.FtpProtocolException(getLastResponseString(), lastReplyCode);
}
}
return this;
}
/**
* Reinitializes the USER parameters on the FTP server
*
* @throws FtpProtocolException if the command fails
*/
public sun.net.ftp.FtpClient reInit() throws sun.net.ftp.FtpProtocolException, IOException {
issueCommandCheck("REIN");
loggedIn = false;
if (useCrypto) {
if (server instanceof SSLSocket) {
javax.net.ssl.SSLSession session = ((SSLSocket) server).getSession();
session.invalidate();
// Restore previous socket and streams
server = oldSocket;
oldSocket = null;
try {
out = new PrintStream(new BufferedOutputStream(server.getOutputStream()),
true, encoding);
} catch (UnsupportedEncodingException e) {
throw new InternalError(encoding + "encoding not found", e);
}
in = new BufferedInputStream(server.getInputStream());
}
}
useCrypto = false;
return this;
}
/**
* Changes the transfer type (binary, ascii, ebcdic) and issue the
* proper command (e.g. TYPE A) to the server.
*
* @param type the <code>FtpTransferType</code> to use.
* @return This FtpClient
* @throws IOException if an error occurs during transmission.
*/
public sun.net.ftp.FtpClient setType(TransferType type) throws sun.net.ftp.FtpProtocolException, IOException {
String cmd = "NOOP";
this.type = type;
if (type == TransferType.ASCII) {
cmd = "TYPE A";
}
if (type == TransferType.BINARY) {
cmd = "TYPE I";
}
if (type == TransferType.EBCDIC) {
cmd = "TYPE E";
}
issueCommandCheck(cmd);
return this;
}
/**
* Issues a LIST command to the server to get the current directory
* listing, and returns the InputStream from the data connection.
* {@link #completePending()} <b>has</b> to be called once the application
* is finished writing to the stream.
*
* @param path the pathname of the directory to list, or <code>null</code>
* for the current working directory.
* @return the <code>InputStream</code> from the resulting data connection
* @throws IOException if an error occurs during the transmission.
* @see #changeDirectory(String)
* @see #listFiles(String)
*/
public InputStream list(String path) throws sun.net.ftp.FtpProtocolException, IOException {
Socket s;
s = openDataConnection(path == null ? "LIST" : "LIST " + path);
if (s != null) {
return createInputStream(s.getInputStream());
}
return null;
}
/**
* Issues a NLST path command to server to get the specified directory
* content. It differs from {@link #list(String)} method by the fact that
* it will only list the file names which would make the parsing of the
* somewhat easier.
*
* {@link #completePending()} <b>has</b> to be called once the application
* is finished writing to the stream.
*
* @param path a <code>String</code> containing the pathname of the
* directory to list or <code>null</code> for the current working
* directory.
* @return the <code>InputStream</code> from the resulting data connection
* @throws IOException if an error occurs during the transmission.
*/
public InputStream nameList(String path) throws sun.net.ftp.FtpProtocolException, IOException {
Socket s;
s = openDataConnection(path == null ? "NLST" : "NLST " + path);
if (s != null) {
return createInputStream(s.getInputStream());
}
return null;
}
/**
* Issues the SIZE [path] command to the server to get the size of a
* specific file on the server.
* Note that this command may not be supported by the server. In which
* case -1 will be returned.
*
* @param path a <code>String</code> containing the pathname of the
* file.
* @return a <code>long</code> containing the size of the file or -1 if
* the server returned an error, which can be checked with
* {@link #getLastReplyCode()}.
* @throws IOException if an error occurs during the transmission.
*/
public long getSize(String path) throws sun.net.ftp.FtpProtocolException, IOException {
if (path == null || path.length() == 0) {
throw new IllegalArgumentException("path can't be null or empty");
}
issueCommandCheck("SIZE " + path);
if (lastReplyCode == FtpReplyCode.FILE_STATUS) {
String s = getResponseString();
s = s.substring(4, s.length() - 1);
return Long.parseLong(s);
}
return -1;
}
private static String[] MDTMformats = {
"yyyyMMddHHmmss.SSS",
"yyyyMMddHHmmss"
};
private static SimpleDateFormat[] dateFormats = new SimpleDateFormat[MDTMformats.length];
static {
for (int i = 0; i < MDTMformats.length; i++) {
dateFormats[i] = new SimpleDateFormat(MDTMformats[i]);
dateFormats[i].setTimeZone(TimeZone.getTimeZone("GMT"));
}
}
/**
* Issues the MDTM [path] command to the server to get the modification
* time of a specific file on the server.
* Note that this command may not be supported by the server, in which
* case <code>null</code> will be returned.
*
* @param path a <code>String</code> containing the pathname of the file.
* @return a <code>Date</code> representing the last modification time
* or <code>null</code> if the server returned an error, which
* can be checked with {@link #getLastReplyCode()}.
* @throws IOException if an error occurs during the transmission.
*/
public Date getLastModified(String path) throws sun.net.ftp.FtpProtocolException, IOException {
issueCommandCheck("MDTM " + path);
if (lastReplyCode == FtpReplyCode.FILE_STATUS) {
String s = getResponseString().substring(4);
Date d = null;
for (SimpleDateFormat dateFormat : dateFormats) {
try {
d = dateFormat.parse(s);
} catch (ParseException ex) {
}
if (d != null) {
return d;
}
}
}
return null;
}
/**
* Sets the parser used to handle the directory output to the specified
* one. By default the parser is set to one that can handle most FTP
* servers output (Unix base mostly). However it may be necessary for
* and application to provide its own parser due to some uncommon
* output format.
*
* @param p The <code>FtpDirParser</code> to use.
* @see #listFiles(String)
*/
public sun.net.ftp.FtpClient setDirParser(FtpDirParser p) {
parser = p;
return this;
}
private class FtpFileIterator implements Iterator<FtpDirEntry>, Closeable {
private BufferedReader in = null;
private FtpDirEntry nextFile = null;
private FtpDirParser fparser = null;
private boolean eof = false;
public FtpFileIterator(FtpDirParser p, BufferedReader in) {
this.in = in;
this.fparser = p;
readNext();
}
private void readNext() {
nextFile = null;
if (eof) {
return;
}
String line = null;
try {
do {
line = in.readLine();
if (line != null) {
nextFile = fparser.parseLine(line);
if (nextFile != null) {
return;
}
}
} while (line != null);
in.close();
} catch (IOException iOException) {
}
eof = true;
}
public boolean hasNext() {
return nextFile != null;
}
public FtpDirEntry next() {
FtpDirEntry ret = nextFile;
readNext();
return ret;
}
public void remove() {
throw new UnsupportedOperationException("Not supported yet.");
}
public void close() throws IOException {
if (in != null && !eof) {
in.close();
}
eof = true;
nextFile = null;
}
}
/**
* Issues a MLSD command to the server to get the specified directory
* listing and applies the current parser to create an Iterator of
* {@link java.net.ftp.FtpDirEntry}. Note that the Iterator returned is also a
* {@link java.io.Closeable}.
* If the server doesn't support the MLSD command, the LIST command is used
* instead.
*
* {@link #completePending()} <b>has</b> to be called once the application
* is finished iterating through the files.
*
* @param path the pathname of the directory to list or <code>null</code>
* for the current working directoty.
* @return a <code>Iterator</code> of files or <code>null</code> if the
* command failed.
* @throws IOException if an error occurred during the transmission
* @see #setDirParser(FtpDirParser)
* @see #changeDirectory(String)
*/
public Iterator<FtpDirEntry> listFiles(String path) throws sun.net.ftp.FtpProtocolException, IOException {
Socket s = null;
BufferedReader sin = null;
try {
s = openDataConnection(path == null ? "MLSD" : "MLSD " + path);
} catch (sun.net.ftp.FtpProtocolException FtpException) {
// The server doesn't understand new MLSD command, ignore and fall
// back to LIST
}
if (s != null) {
sin = new BufferedReader(new InputStreamReader(s.getInputStream()));
return new FtpFileIterator(mlsxParser, sin);
} else {
s = openDataConnection(path == null ? "LIST" : "LIST " + path);
if (s != null) {
sin = new BufferedReader(new InputStreamReader(s.getInputStream()));
return new FtpFileIterator(parser, sin);
}
}
return null;
}
private boolean sendSecurityData(byte[] buf) throws IOException,
sun.net.ftp.FtpProtocolException {
BASE64Encoder encoder = new BASE64Encoder();
String s = encoder.encode(buf);
return issueCommand("ADAT " + s);
}
private byte[] getSecurityData() {
String s = getLastResponseString();
if (s.substring(4, 9).equalsIgnoreCase("ADAT=")) {
BASE64Decoder decoder = new BASE64Decoder();
try {
// Need to get rid of the leading '315 ADAT='
// and the trailing newline
return decoder.decodeBuffer(s.substring(9, s.length() - 1));
} catch (IOException e) {
//
}
}
return null;
}
/**
* Attempts to use Kerberos GSSAPI as an authentication mechanism with the
* ftp server. This will issue an <code>AUTH GSSAPI</code> command, and if
* it is accepted by the server, will followup with <code>ADAT</code>
* command to exchange the various tokens until authentification is
* successful. This conforms to Appendix I of RFC 2228.
*
* @return <code>true</code> if authentication was successful.
* @throws IOException if an error occurs during the transmission.
*/
public sun.net.ftp.FtpClient useKerberos() throws sun.net.ftp.FtpProtocolException, IOException {
/*
* Comment out for the moment since it's not in use and would create
* needless cross-package links.
*
issueCommandCheck("AUTH GSSAPI");
if (lastReplyCode != FtpReplyCode.NEED_ADAT)
throw new sun.net.ftp.FtpProtocolException("Unexpected reply from server");
try {
GSSManager manager = GSSManager.getInstance();
GSSName name = manager.createName("SERVICE:ftp@"+
serverAddr.getHostName(), null);
GSSContext context = manager.createContext(name, null, null,
GSSContext.DEFAULT_LIFETIME);
context.requestMutualAuth(true);
context.requestReplayDet(true);
context.requestSequenceDet(true);
context.requestCredDeleg(true);
byte []inToken = new byte[0];
while (!context.isEstablished()) {
byte[] outToken
= context.initSecContext(inToken, 0, inToken.length);
// send the output token if generated
if (outToken != null) {
if (sendSecurityData(outToken)) {
inToken = getSecurityData();
}
}
}
loggedIn = true;
} catch (GSSException e) {
}
*/
return this;
}
/**
* Returns the Welcome string the server sent during initial connection.
*
* @return a <code>String</code> containing the message the server
* returned during connection or <code>null</code>.
*/
public String getWelcomeMsg() {
return welcomeMsg;
}
/**
* Returns the last reply code sent by the server.
*
* @return the lastReplyCode
*/
public FtpReplyCode getLastReplyCode() {
return lastReplyCode;
}
/**
* Returns the last response string sent by the server.
*
* @return the message string, which can be quite long, last returned
* by the server.
*/
public String getLastResponseString() {
StringBuffer sb = new StringBuffer();
if (serverResponse != null) {
for (String l : serverResponse) {
if (l != null) {
sb.append(l);
}
}
}
return sb.toString();
}
/**
* Returns, when available, the size of the latest started transfer.
* This is retreived by parsing the response string received as an initial
* response to a RETR or similar request.
*
* @return the size of the latest transfer or -1 if either there was no
* transfer or the information was unavailable.
*/
public long getLastTransferSize() {
return lastTransSize;
}
/**
* Returns, when available, the remote name of the last transfered file.
* This is mainly useful for "put" operation when the unique flag was
* set since it allows to recover the unique file name created on the
* server which may be different from the one submitted with the command.
*
* @return the name the latest transfered file remote name, or
* <code>null</code> if that information is unavailable.
*/
public String getLastFileName() {
return lastFileName;
}
/**
* Attempts to switch to a secure, encrypted connection. This is done by
* sending the "AUTH TLS" command.
* <p>See <a href="http://www.ietf.org/rfc/rfc4217.txt">RFC 4217</a></p>
* If successful this will establish a secure command channel with the
* server, it will also make it so that all other transfers (e.g. a RETR
* command) will be done over an encrypted channel as well unless a
* {@link #reInit()} command or a {@link #endSecureSession()} command is issued.
*
* @return <code>true</code> if the operation was successful.
* @throws IOException if an error occurred during the transmission.
* @see #endSecureSession()
*/
public sun.net.ftp.FtpClient startSecureSession() throws sun.net.ftp.FtpProtocolException, IOException {
if (!isConnected()) {
throw new sun.net.ftp.FtpProtocolException("Not connected yet", FtpReplyCode.BAD_SEQUENCE);
}
if (sslFact == null) {
try {
sslFact = (SSLSocketFactory) SSLSocketFactory.getDefault();
} catch (Exception e) {
throw new IOException(e.getLocalizedMessage());
}
}
issueCommandCheck("AUTH TLS");
Socket s = null;
try {
s = sslFact.createSocket(server, serverAddr.getHostName(), serverAddr.getPort(), true);
} catch (javax.net.ssl.SSLException ssle) {
try {
disconnect();
} catch (Exception e) {
}
throw ssle;
}
// Remember underlying socket so we can restore it later
oldSocket = server;
server = s;
try {
out = new PrintStream(new BufferedOutputStream(server.getOutputStream()),
true, encoding);
} catch (UnsupportedEncodingException e) {
throw new InternalError(encoding + "encoding not found", e);
}
in = new BufferedInputStream(server.getInputStream());
issueCommandCheck("PBSZ 0");
issueCommandCheck("PROT P");
useCrypto = true;
return this;
}
/**
* Sends a <code>CCC</code> command followed by a <code>PROT C</code>
* command to the server terminating an encrypted session and reverting
* back to a non crypted transmission.
*
* @return <code>true</code> if the operation was successful.
* @throws IOException if an error occurred during transmission.
* @see #startSecureSession()
*/
public sun.net.ftp.FtpClient endSecureSession() throws sun.net.ftp.FtpProtocolException, IOException {
if (!useCrypto) {
return this;
}
issueCommandCheck("CCC");
issueCommandCheck("PROT C");
useCrypto = false;
// Restore previous socket and streams
server = oldSocket;
oldSocket = null;
try {
out = new PrintStream(new BufferedOutputStream(server.getOutputStream()),
true, encoding);
} catch (UnsupportedEncodingException e) {
throw new InternalError(encoding + "encoding not found", e);
}
in = new BufferedInputStream(server.getInputStream());
return this;
}
/**
* Sends the "Allocate" (ALLO) command to the server telling it to
* pre-allocate the specified number of bytes for the next transfer.
*
* @param size The number of bytes to allocate.
* @return <code>true</code> if the operation was successful.
* @throws IOException if an error occurred during the transmission.
*/
public sun.net.ftp.FtpClient allocate(long size) throws sun.net.ftp.FtpProtocolException, IOException {
issueCommandCheck("ALLO " + size);
return this;
}
/**
* Sends the "Structure Mount" (SMNT) command to the server. This let the
* user mount a different file system data structure without altering his
* login or accounting information.
*
* @param struct a <code>String</code> containing the name of the
* structure to mount.
* @return <code>true</code> if the operation was successful.
* @throws IOException if an error occurred during the transmission.
*/
public sun.net.ftp.FtpClient structureMount(String struct) throws sun.net.ftp.FtpProtocolException, IOException {
issueCommandCheck("SMNT " + struct);
return this;
}
/**
* Sends a SYST (System) command to the server and returns the String
* sent back by the server describing the operating system at the
* server.
*
* @return a <code>String</code> describing the OS, or <code>null</code>
* if the operation was not successful.
* @throws IOException if an error occurred during the transmission.
*/
public String getSystem() throws sun.net.ftp.FtpProtocolException, IOException {
issueCommandCheck("SYST");
/*
* 215 UNIX Type: L8 Version: SUNOS
*/
String resp = getResponseString();
// Get rid of the leading code and blank
return resp.substring(4);
}
/**
* Sends the HELP command to the server, with an optional command, like
* SITE, and returns the text sent back by the server.
*
* @param cmd the command for which the help is requested or
* <code>null</code> for the general help
* @return a <code>String</code> containing the text sent back by the
* server, or <code>null</code> if the command failed.
* @throws IOException if an error occurred during transmission
*/
public String getHelp(String cmd) throws sun.net.ftp.FtpProtocolException, IOException {
issueCommandCheck("HELP " + cmd);
/**
*
* HELP
* 214-The following commands are implemented.
* USER EPRT STRU ALLO DELE SYST RMD MDTM ADAT
* PASS EPSV MODE REST CWD STAT PWD PROT
* QUIT LPRT RETR RNFR LIST HELP CDUP PBSZ
* PORT LPSV STOR RNTO NLST NOOP STOU AUTH
* PASV TYPE APPE ABOR SITE MKD SIZE CCC
* 214 Direct comments to ftp-bugs@jsn.
*
* HELP SITE
* 214-The following SITE commands are implemented.
* UMASK HELP GROUPS
* IDLE ALIAS CHECKMETHOD
* CHMOD CDPATH CHECKSUM
* 214 Direct comments to ftp-bugs@jsn.
*/
Vector<String> resp = getResponseStrings();
if (resp.size() == 1) {
// Single line response
return resp.get(0).substring(4);
}
// on multiple lines answers, like the ones above, remove 1st and last
// line, concat the the others.
StringBuffer sb = new StringBuffer();
for (int i = 1; i < resp.size() - 1; i++) {
sb.append(resp.get(i).substring(3));
}
return sb.toString();
}
/**
* Sends the SITE command to the server. This is used by the server
* to provide services specific to his system that are essential
* to file transfer.
*
* @param cmd the command to be sent.
* @return <code>true</code> if the command was successful.
* @throws IOException if an error occurred during transmission
*/
public sun.net.ftp.FtpClient siteCmd(String cmd) throws sun.net.ftp.FtpProtocolException, IOException {
issueCommandCheck("SITE " + cmd);
return this;
}
}
|