Mercurial Hosting > luan
annotate src/goodjava/io/IoUtils.java @ 1530:447b7ef9197f
minor fix
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 28 Jul 2020 22:12:36 -0600 |
parents | 0ba144491a42 |
children | f24a9ba7551e |
rev | line source |
---|---|
1473 | 1 package goodjava.io; |
2 | |
3 import java.io.File; | |
1493 | 4 import java.io.InputStream; |
5 import java.io.OutputStream; | |
1509 | 6 import java.io.Reader; |
7 import java.io.InputStreamReader; | |
8 import java.io.Writer; | |
9 import java.io.StringWriter; | |
1473 | 10 import java.io.IOException; |
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 | 15 import goodjava.logging.Logger; |
16 import goodjava.logging.LoggerFactory; | |
1473 | 17 |
18 | |
19 public final class IoUtils { | |
1509 | 20 private static final Logger logger = LoggerFactory.getLogger(IoUtils.class); |
21 | |
1473 | 22 private IoUtils() {} // never |
23 | |
24 public static void move( File from, File to ) throws IOException { | |
25 Files.move( from.toPath(), to.toPath() ); | |
26 } | |
27 | |
28 public static void delete(File file) throws IOException { | |
29 Files.deleteIfExists( file.toPath() ); | |
30 } | |
1475 | 31 |
1501 | 32 public static void mkdirs(File file) throws IOException { |
33 Files.createDirectories( file.toPath() ); | |
34 } | |
35 | |
1475 | 36 public static boolean isSymbolicLink(File file) { |
37 return Files.isSymbolicLink(file.toPath()); | |
38 } | |
39 | |
40 public static void deleteRecursively(File file) throws IOException { | |
41 if( file.isDirectory() && !isSymbolicLink(file) ) { | |
42 for( File f : file.listFiles() ) { | |
43 deleteRecursively(f); | |
44 } | |
45 } | |
46 delete(file); | |
47 } | |
48 | |
1497 | 49 public static void link(File existing,File link) throws IOException { |
50 Files.createLink( link.toPath(), existing.toPath() ); | |
51 } | |
52 | |
53 public static void symlink(File existing,File link) throws IOException { | |
54 Files.createSymbolicLink( link.toPath(), existing.toPath() ); | |
1488 | 55 } |
56 | |
1494 | 57 public static void copyAll(InputStream in,OutputStream out) |
1493 | 58 throws IOException |
59 { | |
60 byte[] a = new byte[8192]; | |
61 int n; | |
62 while( (n=in.read(a)) != -1 ) { | |
63 out.write(a,0,n); | |
64 } | |
1499 | 65 in.close(); |
1493 | 66 } |
67 | |
1509 | 68 public static void copyAll(Reader in,Writer out) |
69 throws IOException | |
70 { | |
71 char[] a = new char[8192]; | |
72 int n; | |
73 while( (n=in.read(a)) != -1 ) { | |
74 out.write(a,0,n); | |
75 } | |
76 in.close(); | |
77 } | |
78 | |
79 public static String readAll(Reader in) | |
80 throws IOException | |
81 { | |
82 StringWriter sw = new StringWriter(); | |
83 copyAll(in,sw); | |
84 return sw.toString(); | |
85 } | |
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 | 98 |
99 public static class ProcException extends IOException { | |
100 private ProcException(String msg) { | |
101 super(msg); | |
102 } | |
103 } | |
104 | |
105 public static void waitFor(Process proc) | |
106 throws IOException, ProcException | |
107 { | |
108 try { | |
109 proc.waitFor(); | |
110 } catch(InterruptedException e) { | |
111 throw new RuntimeException(e); | |
112 } | |
113 int exitVal = proc.exitValue(); | |
114 if( exitVal != 0 ) { | |
115 StringWriter sw = new StringWriter(); | |
116 copyAll( new InputStreamReader(proc.getInputStream()), sw ); | |
117 copyAll( new InputStreamReader(proc.getErrorStream()), sw ); | |
118 String error = sw.toString(); | |
119 throw new ProcException(error); | |
120 } | |
121 } | |
122 | |
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 } |