annotate src/goodjava/io/IoUtils.java @ 1511:fb5be0e8ca54

better time_out
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 22 May 2020 19:10:14 -0600
parents 0ba144491a42
children f24a9ba7551e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1473
6c6ce14db6a8 add goodjava.io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 package goodjava.io;
6c6ce14db6a8 add goodjava.io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2
6c6ce14db6a8 add goodjava.io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 import java.io.File;
1493
471ef3e6a84e more io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1488
diff changeset
4 import java.io.InputStream;
471ef3e6a84e more io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1488
diff changeset
5 import java.io.OutputStream;
1509
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
6 import java.io.Reader;
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
7 import java.io.InputStreamReader;
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
8 import java.io.Writer;
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
9 import java.io.StringWriter;
1473
6c6ce14db6a8 add goodjava.io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 import java.io.IOException;
6c6ce14db6a8 add goodjava.io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 import java.nio.file.Files;
1506
d80395468b4e ssl security in code
Franklin Schmidt <fschmidt@gmail.com>
parents: 1501
diff changeset
12 import java.security.Security;
d80395468b4e ssl security in code
Franklin Schmidt <fschmidt@gmail.com>
parents: 1501
diff changeset
13 import javax.net.ssl.SSLSocketFactory;
d80395468b4e ssl security in code
Franklin Schmidt <fschmidt@gmail.com>
parents: 1501
diff changeset
14 import javax.net.ssl.SSLServerSocketFactory;
1509
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
15 import goodjava.logging.Logger;
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
16 import goodjava.logging.LoggerFactory;
1473
6c6ce14db6a8 add goodjava.io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17
6c6ce14db6a8 add goodjava.io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18
6c6ce14db6a8 add goodjava.io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 public final class IoUtils {
1509
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
20 private static final Logger logger = LoggerFactory.getLogger(IoUtils.class);
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
21
1473
6c6ce14db6a8 add goodjava.io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 private IoUtils() {} // never
6c6ce14db6a8 add goodjava.io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23
6c6ce14db6a8 add goodjava.io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 public static void move( File from, File to ) throws IOException {
6c6ce14db6a8 add goodjava.io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 Files.move( from.toPath(), to.toPath() );
6c6ce14db6a8 add goodjava.io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 }
6c6ce14db6a8 add goodjava.io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27
6c6ce14db6a8 add goodjava.io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 public static void delete(File file) throws IOException {
6c6ce14db6a8 add goodjava.io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 Files.deleteIfExists( file.toPath() );
6c6ce14db6a8 add goodjava.io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 }
1475
c7b86342857f more IoUtils
Franklin Schmidt <fschmidt@gmail.com>
parents: 1473
diff changeset
31
1501
Franklin Schmidt <fschmidt@gmail.com>
parents: 1499
diff changeset
32 public static void mkdirs(File file) throws IOException {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1499
diff changeset
33 Files.createDirectories( file.toPath() );
Franklin Schmidt <fschmidt@gmail.com>
parents: 1499
diff changeset
34 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 1499
diff changeset
35
1475
c7b86342857f more IoUtils
Franklin Schmidt <fschmidt@gmail.com>
parents: 1473
diff changeset
36 public static boolean isSymbolicLink(File file) {
c7b86342857f more IoUtils
Franklin Schmidt <fschmidt@gmail.com>
parents: 1473
diff changeset
37 return Files.isSymbolicLink(file.toPath());
c7b86342857f more IoUtils
Franklin Schmidt <fschmidt@gmail.com>
parents: 1473
diff changeset
38 }
c7b86342857f more IoUtils
Franklin Schmidt <fschmidt@gmail.com>
parents: 1473
diff changeset
39
c7b86342857f more IoUtils
Franklin Schmidt <fschmidt@gmail.com>
parents: 1473
diff changeset
40 public static void deleteRecursively(File file) throws IOException {
c7b86342857f more IoUtils
Franklin Schmidt <fschmidt@gmail.com>
parents: 1473
diff changeset
41 if( file.isDirectory() && !isSymbolicLink(file) ) {
c7b86342857f more IoUtils
Franklin Schmidt <fschmidt@gmail.com>
parents: 1473
diff changeset
42 for( File f : file.listFiles() ) {
c7b86342857f more IoUtils
Franklin Schmidt <fschmidt@gmail.com>
parents: 1473
diff changeset
43 deleteRecursively(f);
c7b86342857f more IoUtils
Franklin Schmidt <fschmidt@gmail.com>
parents: 1473
diff changeset
44 }
c7b86342857f more IoUtils
Franklin Schmidt <fschmidt@gmail.com>
parents: 1473
diff changeset
45 }
c7b86342857f more IoUtils
Franklin Schmidt <fschmidt@gmail.com>
parents: 1473
diff changeset
46 delete(file);
c7b86342857f more IoUtils
Franklin Schmidt <fschmidt@gmail.com>
parents: 1473
diff changeset
47 }
c7b86342857f more IoUtils
Franklin Schmidt <fschmidt@gmail.com>
parents: 1473
diff changeset
48
1497
f04bfbb08721 link_from
Franklin Schmidt <fschmidt@gmail.com>
parents: 1494
diff changeset
49 public static void link(File existing,File link) throws IOException {
f04bfbb08721 link_from
Franklin Schmidt <fschmidt@gmail.com>
parents: 1494
diff changeset
50 Files.createLink( link.toPath(), existing.toPath() );
f04bfbb08721 link_from
Franklin Schmidt <fschmidt@gmail.com>
parents: 1494
diff changeset
51 }
f04bfbb08721 link_from
Franklin Schmidt <fschmidt@gmail.com>
parents: 1494
diff changeset
52
f04bfbb08721 link_from
Franklin Schmidt <fschmidt@gmail.com>
parents: 1494
diff changeset
53 public static void symlink(File existing,File link) throws IOException {
f04bfbb08721 link_from
Franklin Schmidt <fschmidt@gmail.com>
parents: 1494
diff changeset
54 Files.createSymbolicLink( link.toPath(), existing.toPath() );
1488
af55cfad6e12 start lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
55 }
af55cfad6e12 start lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
56
1494
91c167099462 more io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1493
diff changeset
57 public static void copyAll(InputStream in,OutputStream out)
1493
471ef3e6a84e more io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1488
diff changeset
58 throws IOException
471ef3e6a84e more io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1488
diff changeset
59 {
471ef3e6a84e more io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1488
diff changeset
60 byte[] a = new byte[8192];
471ef3e6a84e more io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1488
diff changeset
61 int n;
471ef3e6a84e more io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1488
diff changeset
62 while( (n=in.read(a)) != -1 ) {
471ef3e6a84e more io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1488
diff changeset
63 out.write(a,0,n);
471ef3e6a84e more io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1488
diff changeset
64 }
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents: 1497
diff changeset
65 in.close();
1493
471ef3e6a84e more io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1488
diff changeset
66 }
471ef3e6a84e more io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1488
diff changeset
67
1509
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
68 public static void copyAll(Reader in,Writer out)
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
69 throws IOException
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
70 {
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
71 char[] a = new char[8192];
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
72 int n;
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
73 while( (n=in.read(a)) != -1 ) {
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
74 out.write(a,0,n);
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
75 }
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
76 in.close();
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
77 }
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
78
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
79 public static String readAll(Reader in)
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
80 throws IOException
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
81 {
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
82 StringWriter sw = new StringWriter();
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
83 copyAll(in,sw);
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
84 return sw.toString();
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
85 }
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
86
1508
86c5e7000ecf lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents: 1506
diff changeset
87 public static long checksum(InputStream in) throws IOException {
86c5e7000ecf lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents: 1506
diff changeset
88 long cs = 0;
86c5e7000ecf lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents: 1506
diff changeset
89 int c;
86c5e7000ecf lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents: 1506
diff changeset
90 while( (c=in.read()) != -1 ) {
86c5e7000ecf lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents: 1506
diff changeset
91 cs = 31 * cs + c;
86c5e7000ecf lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents: 1506
diff changeset
92 }
86c5e7000ecf lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents: 1506
diff changeset
93 in.close();
86c5e7000ecf lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents: 1506
diff changeset
94 return cs;
86c5e7000ecf lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents: 1506
diff changeset
95 }
86c5e7000ecf lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents: 1506
diff changeset
96
1506
d80395468b4e ssl security in code
Franklin Schmidt <fschmidt@gmail.com>
parents: 1501
diff changeset
97
1509
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
98
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
99 public static class ProcException extends IOException {
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
100 private ProcException(String msg) {
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
101 super(msg);
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
102 }
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
103 }
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
104
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
105 public static void waitFor(Process proc)
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
106 throws IOException, ProcException
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
107 {
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
108 try {
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
109 proc.waitFor();
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
110 } catch(InterruptedException e) {
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
111 throw new RuntimeException(e);
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
112 }
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
113 int exitVal = proc.exitValue();
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
114 if( exitVal != 0 ) {
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
115 StringWriter sw = new StringWriter();
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
116 copyAll( new InputStreamReader(proc.getInputStream()), sw );
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
117 copyAll( new InputStreamReader(proc.getErrorStream()), sw );
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
118 String error = sw.toString();
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
119 throw new ProcException(error);
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
120 }
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
121 }
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
122
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
123
1506
d80395468b4e ssl security in code
Franklin Schmidt <fschmidt@gmail.com>
parents: 1501
diff changeset
124 static {
d80395468b4e ssl security in code
Franklin Schmidt <fschmidt@gmail.com>
parents: 1501
diff changeset
125 // undo restrictions of modern scum
d80395468b4e ssl security in code
Franklin Schmidt <fschmidt@gmail.com>
parents: 1501
diff changeset
126 Security.setProperty("jdk.tls.disabledAlgorithms","SSLv3, RC4, DES, MD5withRSA, DH keySize < 1024, EC keySize < 224, 3DES_EDE_CBC");
d80395468b4e ssl security in code
Franklin Schmidt <fschmidt@gmail.com>
parents: 1501
diff changeset
127 }
d80395468b4e ssl security in code
Franklin Schmidt <fschmidt@gmail.com>
parents: 1501
diff changeset
128
d80395468b4e ssl security in code
Franklin Schmidt <fschmidt@gmail.com>
parents: 1501
diff changeset
129 public static SSLSocketFactory getSSLSocketFactory() {
d80395468b4e ssl security in code
Franklin Schmidt <fschmidt@gmail.com>
parents: 1501
diff changeset
130 return (SSLSocketFactory)SSLSocketFactory.getDefault();
d80395468b4e ssl security in code
Franklin Schmidt <fschmidt@gmail.com>
parents: 1501
diff changeset
131 }
d80395468b4e ssl security in code
Franklin Schmidt <fschmidt@gmail.com>
parents: 1501
diff changeset
132
d80395468b4e ssl security in code
Franklin Schmidt <fschmidt@gmail.com>
parents: 1501
diff changeset
133 public static SSLServerSocketFactory getSSLServerSocketFactory() {
d80395468b4e ssl security in code
Franklin Schmidt <fschmidt@gmail.com>
parents: 1501
diff changeset
134 return (SSLServerSocketFactory)SSLServerSocketFactory.getDefault();
d80395468b4e ssl security in code
Franklin Schmidt <fschmidt@gmail.com>
parents: 1501
diff changeset
135 }
d80395468b4e ssl security in code
Franklin Schmidt <fschmidt@gmail.com>
parents: 1501
diff changeset
136
1508
86c5e7000ecf lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents: 1506
diff changeset
137 }