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 package org.eclipse.jgit.util;
45
46 import java.io.BufferedReader;
47 import java.io.ByteArrayInputStream;
48 import java.io.File;
49 import java.io.IOException;
50 import java.io.InputStream;
51 import java.io.InputStreamReader;
52 import java.io.OutputStream;
53 import java.io.PrintStream;
54 import java.nio.charset.Charset;
55 import java.security.AccessController;
56 import java.security.PrivilegedAction;
57 import java.text.MessageFormat;
58 import java.util.Arrays;
59 import java.util.HashMap;
60 import java.util.Map;
61 import java.util.Objects;
62 import java.util.concurrent.ExecutorService;
63 import java.util.concurrent.Executors;
64 import java.util.concurrent.TimeUnit;
65 import java.util.concurrent.atomic.AtomicBoolean;
66 import java.util.concurrent.atomic.AtomicReference;
67
68 import org.eclipse.jgit.annotations.Nullable;
69 import org.eclipse.jgit.api.errors.JGitInternalException;
70 import org.eclipse.jgit.errors.CommandFailedException;
71 import org.eclipse.jgit.internal.JGitText;
72 import org.eclipse.jgit.lib.Constants;
73 import org.eclipse.jgit.lib.Repository;
74 import org.eclipse.jgit.util.ProcessResult.Status;
75 import org.slf4j.Logger;
76 import org.slf4j.LoggerFactory;
77
78
79 public abstract class FS {
80
81
82
83
84
85
86 public static class FSFactory {
87
88
89
90 protected FSFactory() {
91
92 }
93
94
95
96
97
98
99
100 public FS detect(Boolean cygwinUsed) {
101 if (SystemReader.getInstance().isWindows()) {
102 if (cygwinUsed == null)
103 cygwinUsed = Boolean.valueOf(FS_Win32_Cygwin.isCygwin());
104 if (cygwinUsed.booleanValue())
105 return new FS_Win32_Cygwin();
106 else
107 return new FS_Win32();
108 } else {
109 return new FS_POSIX();
110 }
111 }
112 }
113
114
115
116
117
118
119
120 public static class ExecutionResult {
121 private TemporaryBuffer stdout;
122
123 private TemporaryBuffer stderr;
124
125 private int rc;
126
127
128
129
130
131
132 public ExecutionResult(TemporaryBuffer stdout, TemporaryBuffer stderr,
133 int rc) {
134 this.stdout = stdout;
135 this.stderr = stderr;
136 this.rc = rc;
137 }
138
139
140
141
142 public TemporaryBuffer getStdout() {
143 return stdout;
144 }
145
146
147
148
149 public TemporaryBuffer getStderr() {
150 return stderr;
151 }
152
153
154
155
156 public int getRc() {
157 return rc;
158 }
159 }
160
161 private final static Logger LOG = LoggerFactory.getLogger(FS.class);
162
163
164 public static final FS DETECTED = detect();
165
166 private volatile static FSFactory factory;
167
168
169
170
171
172
173 public static FS detect() {
174 return detect(null);
175 }
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198 public static FS detect(Boolean cygwinUsed) {
199 if (factory == null) {
200 factory = new FS.FSFactory();
201 }
202 return factory.detect(cygwinUsed);
203 }
204
205 private volatile Holder<File> userHome;
206
207 private volatile Holder<File> gitSystemConfig;
208
209
210
211
212 protected FS() {
213
214 }
215
216
217
218
219
220
221
222 protected FS(FS src) {
223 userHome = src.userHome;
224 gitSystemConfig = src.gitSystemConfig;
225 }
226
227
228 public abstract FS newInstance();
229
230
231
232
233
234
235
236 public abstract boolean supportsExecute();
237
238
239
240
241
242
243
244
245
246
247
248
249 public boolean supportsAtomicCreateNewFile() {
250 return true;
251 }
252
253
254
255
256
257
258
259
260 public boolean supportsSymlinks() {
261 return false;
262 }
263
264
265
266
267
268
269 public abstract boolean isCaseSensitive();
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285 public abstract boolean canExecute(File f);
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300 public abstract boolean setExecute(File f, boolean canExec);
301
302
303
304
305
306
307
308
309
310
311
312 public long lastModified(File f) throws IOException {
313 return FileUtils.lastModified(f);
314 }
315
316
317
318
319
320
321
322
323
324
325 public void setLastModified(File f, long time) throws IOException {
326 FileUtils.setLastModified(f, time);
327 }
328
329
330
331
332
333
334
335
336
337
338 public long length(File path) throws IOException {
339 return FileUtils.getLength(path);
340 }
341
342
343
344
345
346
347
348
349
350 public void delete(File f) throws IOException {
351 FileUtils.delete(f);
352 }
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372 public File resolve(final File dir, final String name) {
373 final File abspn = new File(name);
374 if (abspn.isAbsolute())
375 return abspn;
376 return new File(dir, name);
377 }
378
379
380
381
382
383
384
385
386
387
388
389
390 public File userHome() {
391 Holder<File> p = userHome;
392 if (p == null) {
393 p = new Holder<>(userHomeImpl());
394 userHome = p;
395 }
396 return p.value;
397 }
398
399
400
401
402
403
404
405
406
407 public FS setUserHome(File path) {
408 userHome = new Holder<>(path);
409 return this;
410 }
411
412
413
414
415
416
417 public abstract boolean retryFailedLockFileCommit();
418
419
420
421
422
423
424 protected File userHomeImpl() {
425 final String home = AccessController
426 .doPrivileged(new PrivilegedAction<String>() {
427 @Override
428 public String run() {
429 return System.getProperty("user.home");
430 }
431 });
432 if (home == null || home.length() == 0)
433 return null;
434 return new File(home).getAbsoluteFile();
435 }
436
437
438
439
440
441
442
443
444
445
446
447
448 protected static File searchPath(final String path, final String... lookFor) {
449 if (path == null)
450 return null;
451
452 for (final String p : path.split(File.pathSeparator)) {
453 for (String command : lookFor) {
454 final File e = new File(p, command);
455 if (e.isFile())
456 return e.getAbsoluteFile();
457 }
458 }
459 return null;
460 }
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476 @Nullable
477 protected static String readPipe(File dir, String[] command,
478 String encoding) throws CommandFailedException {
479 return readPipe(dir, command, encoding, null);
480 }
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500 @Nullable
501 protected static String readPipe(File dir, String[] command,
502 String encoding, Map<String, String> env)
503 throws CommandFailedException {
504 final boolean debug = LOG.isDebugEnabled();
505 try {
506 if (debug) {
507 LOG.debug("readpipe " + Arrays.asList(command) + ","
508 + dir);
509 }
510 ProcessBuilder pb = new ProcessBuilder(command);
511 pb.directory(dir);
512 if (env != null) {
513 pb.environment().putAll(env);
514 }
515 Process p;
516 try {
517 p = pb.start();
518 } catch (IOException e) {
519
520 throw new CommandFailedException(-1, e.getMessage(), e);
521 }
522 p.getOutputStream().close();
523 GobblerThread gobbler = new GobblerThread(p, command, dir);
524 gobbler.start();
525 String r = null;
526 try (BufferedReader lineRead = new BufferedReader(
527 new InputStreamReader(p.getInputStream(), encoding))) {
528 r = lineRead.readLine();
529 if (debug) {
530 LOG.debug("readpipe may return '" + r + "'");
531 LOG.debug("remaining output:\n");
532 String l;
533 while ((l = lineRead.readLine()) != null) {
534 LOG.debug(l);
535 }
536 }
537 }
538
539 for (;;) {
540 try {
541 int rc = p.waitFor();
542 gobbler.join();
543 if (rc == 0 && !gobbler.fail.get()) {
544 return r;
545 } else {
546 if (debug) {
547 LOG.debug("readpipe rc=" + rc);
548 }
549 throw new CommandFailedException(rc,
550 gobbler.errorMessage.get(),
551 gobbler.exception.get());
552 }
553 } catch (InterruptedException ie) {
554
555 }
556 }
557 } catch (IOException e) {
558 LOG.error("Caught exception in FS.readPipe()", e);
559 }
560 if (debug) {
561 LOG.debug("readpipe returns null");
562 }
563 return null;
564 }
565
566 private static class GobblerThread extends Thread {
567
568
569 private static final int PROCESS_EXIT_TIMEOUT = 5;
570
571 private final Process p;
572 private final String desc;
573 private final String dir;
574 final AtomicBoolean fail = new AtomicBoolean();
575 final AtomicReference<String> errorMessage = new AtomicReference<>();
576 final AtomicReference<Throwable> exception = new AtomicReference<>();
577
578 GobblerThread(Process p, String[] command, File dir) {
579 this.p = p;
580 this.desc = Arrays.toString(command);
581 this.dir = Objects.toString(dir);
582 }
583
584 @Override
585 public void run() {
586 StringBuilder err = new StringBuilder();
587 try (InputStream is = p.getErrorStream()) {
588 int ch;
589 while ((ch = is.read()) != -1) {
590 err.append((char) ch);
591 }
592 } catch (IOException e) {
593 if (waitForProcessCompletion(e) && p.exitValue() != 0) {
594 setError(e, e.getMessage(), p.exitValue());
595 fail.set(true);
596 } else {
597
598
599 }
600 } finally {
601 if (waitForProcessCompletion(null) && err.length() > 0) {
602 setError(null, err.toString(), p.exitValue());
603 if (p.exitValue() != 0) {
604 fail.set(true);
605 }
606 }
607 }
608 }
609
610 @SuppressWarnings("boxing")
611 private boolean waitForProcessCompletion(IOException originalError) {
612 try {
613 if (!p.waitFor(PROCESS_EXIT_TIMEOUT, TimeUnit.SECONDS)) {
614 setError(originalError, MessageFormat.format(
615 JGitText.get().commandClosedStderrButDidntExit,
616 desc, PROCESS_EXIT_TIMEOUT), -1);
617 fail.set(true);
618 }
619 } catch (InterruptedException e) {
620 LOG.error(MessageFormat.format(
621 JGitText.get().threadInterruptedWhileRunning, desc), e);
622 }
623 return false;
624 }
625
626 private void setError(IOException e, String message, int exitCode) {
627 exception.set(e);
628 errorMessage.set(MessageFormat.format(
629 JGitText.get().exceptionCaughtDuringExecutionOfCommand,
630 desc, dir, Integer.valueOf(exitCode), message));
631 }
632 }
633
634
635
636
637
638
639 protected abstract File discoverGitExe();
640
641
642
643
644
645
646 protected File discoverGitSystemConfig() {
647 File gitExe = discoverGitExe();
648 if (gitExe == null) {
649 return null;
650 }
651
652
653 String v;
654 try {
655 v = readPipe(gitExe.getParentFile(),
656 new String[] { "git", "--version" },
657 Charset.defaultCharset().name());
658 } catch (CommandFailedException e) {
659 LOG.warn(e.getMessage());
660 return null;
661 }
662 if (StringUtils.isEmptyOrNull(v)
663 || (v != null && v.startsWith("jgit"))) {
664 return null;
665 }
666
667
668
669 Map<String, String> env = new HashMap<>();
670 env.put("GIT_EDITOR", "echo");
671
672 String w;
673 try {
674 w = readPipe(gitExe.getParentFile(),
675 new String[] { "git", "config", "--system", "--edit" },
676 Charset.defaultCharset().name(), env);
677 } catch (CommandFailedException e) {
678 LOG.warn(e.getMessage());
679 return null;
680 }
681 if (StringUtils.isEmptyOrNull(w)) {
682 return null;
683 }
684
685 return new File(w);
686 }
687
688
689
690
691
692
693 public File getGitSystemConfig() {
694 if (gitSystemConfig == null) {
695 gitSystemConfig = new Holder<>(discoverGitSystemConfig());
696 }
697 return gitSystemConfig.value;
698 }
699
700
701
702
703
704
705
706
707
708 public FS setGitSystemConfig(File configFile) {
709 gitSystemConfig = new Holder<>(configFile);
710 return this;
711 }
712
713
714
715
716
717
718
719 protected static File resolveGrandparentFile(File grandchild) {
720 if (grandchild != null) {
721 File parent = grandchild.getParentFile();
722 if (parent != null)
723 return parent.getParentFile();
724 }
725 return null;
726 }
727
728
729
730
731
732
733
734
735
736 public String readSymLink(File path) throws IOException {
737 return FileUtils.readSymLink(path);
738 }
739
740
741
742
743
744
745
746 public boolean isSymLink(File path) throws IOException {
747 return FileUtils.isSymlink(path);
748 }
749
750
751
752
753
754
755
756
757
758 public boolean exists(File path) {
759 return FileUtils.exists(path);
760 }
761
762
763
764
765
766
767
768
769
770 public boolean isDirectory(File path) {
771 return FileUtils.isDirectory(path);
772 }
773
774
775
776
777
778
779
780
781
782 public boolean isFile(File path) {
783 return FileUtils.isFile(path);
784 }
785
786
787
788
789
790
791
792
793 public boolean isHidden(File path) throws IOException {
794 return FileUtils.isHidden(path);
795 }
796
797
798
799
800
801
802
803
804
805 public void setHidden(File path, boolean hidden) throws IOException {
806 FileUtils.setHidden(path, hidden);
807 }
808
809
810
811
812
813
814
815
816
817 public void createSymLink(File path, String target) throws IOException {
818 FileUtils.createSymLink(path, target);
819 }
820
821
822
823
824
825
826
827
828
829
830
831
832
833 public boolean createNewFile(File path) throws IOException {
834 return path.createNewFile();
835 }
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850 public String relativize(String base, String other) {
851 return FileUtils.relativizePath(base, other, File.separator, this.isCaseSensitive());
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 public ProcessResult runHookIfPresent(Repository repository,
877 final String hookName,
878 String[] args) throws JGitInternalException {
879 return runHookIfPresent(repository, hookName, args, System.out, System.err,
880 null);
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 public ProcessResult runHookIfPresent(Repository repository,
912 final String hookName,
913 String[] args, PrintStream outRedirect, PrintStream errRedirect,
914 String stdinArgs) throws JGitInternalException {
915 return new ProcessResult(Status.NOT_SUPPORTED);
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 protected ProcessResult internalRunHookIfPresent(Repository repository,
948 final String hookName, String[] args, PrintStream outRedirect,
949 PrintStream errRedirect, String stdinArgs)
950 throws JGitInternalException {
951 final File hookFile = findHook(repository, hookName);
952 if (hookFile == null)
953 return new ProcessResult(Status.NOT_PRESENT);
954
955 final String hookPath = hookFile.getAbsolutePath();
956 final File runDirectory;
957 if (repository.isBare())
958 runDirectory = repository.getDirectory();
959 else
960 runDirectory = repository.getWorkTree();
961 final String cmd = relativize(runDirectory.getAbsolutePath(),
962 hookPath);
963 ProcessBuilder hookProcess = runInShell(cmd, args);
964 hookProcess.directory(runDirectory);
965 try {
966 return new ProcessResult(runProcess(hookProcess, outRedirect,
967 errRedirect, stdinArgs), Status.OK);
968 } catch (IOException e) {
969 throw new JGitInternalException(MessageFormat.format(
970 JGitText.get().exceptionCaughtDuringExecutionOfHook,
971 hookName), e);
972 } catch (InterruptedException e) {
973 throw new JGitInternalException(MessageFormat.format(
974 JGitText.get().exceptionHookExecutionInterrupted,
975 hookName), e);
976 }
977 }
978
979
980
981
982
983
984
985
986
987
988
989
990
991 public File findHook(Repository repository, final String hookName) {
992 File gitDir = repository.getDirectory();
993 if (gitDir == null)
994 return null;
995 final File hookFile = new File(new File(gitDir,
996 Constants.HOOKS), hookName);
997 return hookFile.isFile() ? hookFile : null;
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 public int runProcess(ProcessBuilder processBuilder,
1026 OutputStream outRedirect, OutputStream errRedirect, String stdinArgs)
1027 throws IOException, InterruptedException {
1028 InputStream in = (stdinArgs == null) ? null : new ByteArrayInputStream(
1029 stdinArgs.getBytes(Constants.CHARACTER_ENCODING));
1030 return runProcess(processBuilder, outRedirect, errRedirect, in);
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 public int runProcess(ProcessBuilder processBuilder,
1062 OutputStream outRedirect, OutputStream errRedirect,
1063 InputStream inRedirect) throws IOException,
1064 InterruptedException {
1065 final ExecutorService executor = Executors.newFixedThreadPool(2);
1066 Process process = null;
1067
1068
1069 IOException ioException = null;
1070 try {
1071 process = processBuilder.start();
1072 executor.execute(
1073 new StreamGobbler(process.getErrorStream(), errRedirect));
1074 executor.execute(
1075 new StreamGobbler(process.getInputStream(), outRedirect));
1076 OutputStream outputStream = process.getOutputStream();
1077 if (inRedirect != null) {
1078 new StreamGobbler(inRedirect, outputStream).copy();
1079 }
1080 try {
1081 outputStream.close();
1082 } catch (IOException e) {
1083
1084
1085
1086
1087
1088
1089 }
1090 return process.waitFor();
1091 } catch (IOException e) {
1092 ioException = e;
1093 } finally {
1094 shutdownAndAwaitTermination(executor);
1095 if (process != null) {
1096 try {
1097 process.waitFor();
1098 } catch (InterruptedException e) {
1099
1100
1101
1102
1103 Thread.interrupted();
1104 }
1105
1106
1107
1108 if (inRedirect != null) {
1109 inRedirect.close();
1110 }
1111 try {
1112 process.getErrorStream().close();
1113 } catch (IOException e) {
1114 ioException = ioException != null ? ioException : e;
1115 }
1116 try {
1117 process.getInputStream().close();
1118 } catch (IOException e) {
1119 ioException = ioException != null ? ioException : e;
1120 }
1121 try {
1122 process.getOutputStream().close();
1123 } catch (IOException e) {
1124 ioException = ioException != null ? ioException : e;
1125 }
1126 process.destroy();
1127 }
1128 }
1129
1130 throw ioException;
1131 }
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146 private static boolean shutdownAndAwaitTermination(ExecutorService pool) {
1147 boolean hasShutdown = true;
1148 pool.shutdown();
1149 try {
1150
1151 if (!pool.awaitTermination(60, TimeUnit.SECONDS)) {
1152 pool.shutdownNow();
1153
1154 if (!pool.awaitTermination(60, TimeUnit.SECONDS))
1155 hasShutdown = false;
1156 }
1157 } catch (InterruptedException ie) {
1158
1159 pool.shutdownNow();
1160
1161 Thread.currentThread().interrupt();
1162 hasShutdown = false;
1163 }
1164 return hasShutdown;
1165 }
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179 public abstract ProcessBuilder runInShell(String cmd, String[] args);
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193 public ExecutionResult execute(ProcessBuilder pb, InputStream in)
1194 throws IOException, InterruptedException {
1195 TemporaryBuffer stdout = new TemporaryBuffer.LocalFile(null);
1196 TemporaryBuffer stderr = new TemporaryBuffer.Heap(1024, 1024 * 1024);
1197 try {
1198 int rc = runProcess(pb, stdout, stderr, in);
1199 return new ExecutionResult(stdout, stderr, rc);
1200 } finally {
1201 stdout.close();
1202 stderr.close();
1203 }
1204 }
1205
1206 private static class Holder<V> {
1207 final V value;
1208
1209 Holder(V value) {
1210 this.value = value;
1211 }
1212 }
1213
1214
1215
1216
1217
1218
1219 public static class Attributes {
1220
1221
1222
1223
1224 public boolean isDirectory() {
1225 return isDirectory;
1226 }
1227
1228
1229
1230
1231 public boolean isExecutable() {
1232 return isExecutable;
1233 }
1234
1235
1236
1237
1238 public boolean isSymbolicLink() {
1239 return isSymbolicLink;
1240 }
1241
1242
1243
1244
1245 public boolean isRegularFile() {
1246 return isRegularFile;
1247 }
1248
1249
1250
1251
1252 public long getCreationTime() {
1253 return creationTime;
1254 }
1255
1256
1257
1258
1259
1260 public long getLastModifiedTime() {
1261 return lastModifiedTime;
1262 }
1263
1264 private final boolean isDirectory;
1265
1266 private final boolean isSymbolicLink;
1267
1268 private final boolean isRegularFile;
1269
1270 private final long creationTime;
1271
1272 private final long lastModifiedTime;
1273
1274 private final boolean isExecutable;
1275
1276 private final File file;
1277
1278 private final boolean exists;
1279
1280
1281
1282
1283 protected long length = -1;
1284
1285 final FS fs;
1286
1287 Attributes(FS fs, File file, boolean exists, boolean isDirectory,
1288 boolean isExecutable, boolean isSymbolicLink,
1289 boolean isRegularFile, long creationTime,
1290 long lastModifiedTime, long length) {
1291 this.fs = fs;
1292 this.file = file;
1293 this.exists = exists;
1294 this.isDirectory = isDirectory;
1295 this.isExecutable = isExecutable;
1296 this.isSymbolicLink = isSymbolicLink;
1297 this.isRegularFile = isRegularFile;
1298 this.creationTime = creationTime;
1299 this.lastModifiedTime = lastModifiedTime;
1300 this.length = length;
1301 }
1302
1303
1304
1305
1306
1307
1308
1309
1310 public Attributes(File path, FS fs) {
1311 this(fs, path, false, false, false, false, false, 0L, 0L, 0L);
1312 }
1313
1314
1315
1316
1317 public long getLength() {
1318 if (length == -1)
1319 return length = file.length();
1320 return length;
1321 }
1322
1323
1324
1325
1326 public String getName() {
1327 return file.getName();
1328 }
1329
1330
1331
1332
1333 public File getFile() {
1334 return file;
1335 }
1336
1337 boolean exists() {
1338 return exists;
1339 }
1340 }
1341
1342
1343
1344
1345
1346
1347 public Attributes getAttributes(File path) {
1348 boolean isDirectory = isDirectory(path);
1349 boolean isFile = !isDirectory && path.isFile();
1350 assert path.exists() == isDirectory || isFile;
1351 boolean exists = isDirectory || isFile;
1352 boolean canExecute = exists && !isDirectory && canExecute(path);
1353 boolean isSymlink = false;
1354 long lastModified = exists ? path.lastModified() : 0L;
1355 long createTime = 0L;
1356 return new Attributes(this, path, exists, isDirectory, canExecute,
1357 isSymlink, isFile, createTime, lastModified, -1);
1358 }
1359
1360
1361
1362
1363
1364
1365
1366
1367 public File normalize(File file) {
1368 return file;
1369 }
1370
1371
1372
1373
1374
1375
1376
1377
1378 public String normalize(String name) {
1379 return name;
1380 }
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394 private static class StreamGobbler implements Runnable {
1395 private InputStream in;
1396
1397 private OutputStream out;
1398
1399 public StreamGobbler(InputStream stream, OutputStream output) {
1400 this.in = stream;
1401 this.out = output;
1402 }
1403
1404 @Override
1405 public void run() {
1406 try {
1407 copy();
1408 } catch (IOException e) {
1409
1410 }
1411 }
1412
1413 void copy() throws IOException {
1414 boolean writeFailure = false;
1415 byte buffer[] = new byte[4096];
1416 int readBytes;
1417 while ((readBytes = in.read(buffer)) != -1) {
1418
1419
1420
1421 if (!writeFailure && out != null) {
1422 try {
1423 out.write(buffer, 0, readBytes);
1424 out.flush();
1425 } catch (IOException e) {
1426 writeFailure = true;
1427 }
1428 }
1429 }
1430 }
1431 }
1432 }