Mercurial Hosting > luan
annotate src/goodjava/io/IoUtils.java @ 1565:44e444a8da64
minor
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 11 Nov 2020 13:17:58 -0700 |
parents | 736ec76bbf42 |
children | 08177ced7fa0 |
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; | |
1548 | 12 import java.nio.file.attribute.FileTime; |
1506
d80395468b4e
ssl security in code
Franklin Schmidt <fschmidt@gmail.com>
parents:
1501
diff
changeset
|
13 import java.security.Security; |
d80395468b4e
ssl security in code
Franklin Schmidt <fschmidt@gmail.com>
parents:
1501
diff
changeset
|
14 import javax.net.ssl.SSLSocketFactory; |
d80395468b4e
ssl security in code
Franklin Schmidt <fschmidt@gmail.com>
parents:
1501
diff
changeset
|
15 import javax.net.ssl.SSLServerSocketFactory; |
1509 | 16 import goodjava.logging.Logger; |
17 import goodjava.logging.LoggerFactory; | |
1473 | 18 |
19 | |
20 public final class IoUtils { | |
1509 | 21 private static final Logger logger = LoggerFactory.getLogger(IoUtils.class); |
22 | |
1473 | 23 private IoUtils() {} // never |
24 | |
25 public static void move( File from, File to ) throws IOException { | |
26 Files.move( from.toPath(), to.toPath() ); | |
27 } | |
28 | |
29 public static void delete(File file) throws IOException { | |
30 Files.deleteIfExists( file.toPath() ); | |
31 } | |
1475 | 32 |
1501 | 33 public static void mkdirs(File file) throws IOException { |
34 Files.createDirectories( file.toPath() ); | |
35 } | |
36 | |
1475 | 37 public static boolean isSymbolicLink(File file) { |
38 return Files.isSymbolicLink(file.toPath()); | |
39 } | |
40 | |
41 public static void deleteRecursively(File file) throws IOException { | |
42 if( file.isDirectory() && !isSymbolicLink(file) ) { | |
43 for( File f : file.listFiles() ) { | |
44 deleteRecursively(f); | |
45 } | |
46 } | |
47 delete(file); | |
48 } | |
49 | |
1497 | 50 public static void link(File existing,File link) throws IOException { |
51 Files.createLink( link.toPath(), existing.toPath() ); | |
52 } | |
53 | |
54 public static void symlink(File existing,File link) throws IOException { | |
55 Files.createSymbolicLink( link.toPath(), existing.toPath() ); | |
1488 | 56 } |
57 | |
1548 | 58 public static long getCreationTime(File f) throws IOException { |
59 return ((FileTime)Files.getAttribute(f.toPath(),"creationTime")).toMillis(); | |
60 } | |
61 | |
1494 | 62 public static void copyAll(InputStream in,OutputStream out) |
1493 | 63 throws IOException |
64 { | |
65 byte[] a = new byte[8192]; | |
66 int n; | |
67 while( (n=in.read(a)) != -1 ) { | |
68 out.write(a,0,n); | |
69 } | |
1499 | 70 in.close(); |
1493 | 71 } |
72 | |
1509 | 73 public static void copyAll(Reader in,Writer out) |
74 throws IOException | |
75 { | |
76 char[] a = new char[8192]; | |
77 int n; | |
78 while( (n=in.read(a)) != -1 ) { | |
79 out.write(a,0,n); | |
80 } | |
81 in.close(); | |
82 } | |
83 | |
84 public static String readAll(Reader in) | |
85 throws IOException | |
86 { | |
87 StringWriter sw = new StringWriter(); | |
88 copyAll(in,sw); | |
89 return sw.toString(); | |
90 } | |
91 | |
1508
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
92 public static long checksum(InputStream in) throws IOException { |
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
93 long cs = 0; |
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
94 int c; |
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
95 while( (c=in.read()) != -1 ) { |
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
96 cs = 31 * cs + c; |
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
97 } |
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
98 in.close(); |
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
99 return cs; |
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
100 } |
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
101 |
1506
d80395468b4e
ssl security in code
Franklin Schmidt <fschmidt@gmail.com>
parents:
1501
diff
changeset
|
102 |
1509 | 103 |
104 public static class ProcException extends IOException { | |
105 private ProcException(String msg) { | |
106 super(msg); | |
107 } | |
108 } | |
109 | |
110 public static void waitFor(Process proc) | |
111 throws IOException, ProcException | |
112 { | |
113 try { | |
114 proc.waitFor(); | |
115 } catch(InterruptedException e) { | |
116 throw new RuntimeException(e); | |
117 } | |
118 int exitVal = proc.exitValue(); | |
119 if( exitVal != 0 ) { | |
120 StringWriter sw = new StringWriter(); | |
1547 | 121 try { |
122 copyAll( new InputStreamReader(proc.getInputStream()), sw ); | |
123 } catch(IOException e) {} | |
1509 | 124 copyAll( new InputStreamReader(proc.getErrorStream()), sw ); |
125 String error = sw.toString(); | |
126 throw new ProcException(error); | |
127 } | |
128 } | |
129 | |
130 | |
1506
d80395468b4e
ssl security in code
Franklin Schmidt <fschmidt@gmail.com>
parents:
1501
diff
changeset
|
131 static { |
d80395468b4e
ssl security in code
Franklin Schmidt <fschmidt@gmail.com>
parents:
1501
diff
changeset
|
132 // undo restrictions of modern scum |
d80395468b4e
ssl security in code
Franklin Schmidt <fschmidt@gmail.com>
parents:
1501
diff
changeset
|
133 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
|
134 } |
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 public static SSLSocketFactory getSSLSocketFactory() { |
d80395468b4e
ssl security in code
Franklin Schmidt <fschmidt@gmail.com>
parents:
1501
diff
changeset
|
137 return (SSLSocketFactory)SSLSocketFactory.getDefault(); |
d80395468b4e
ssl security in code
Franklin Schmidt <fschmidt@gmail.com>
parents:
1501
diff
changeset
|
138 } |
d80395468b4e
ssl security in code
Franklin Schmidt <fschmidt@gmail.com>
parents:
1501
diff
changeset
|
139 |
d80395468b4e
ssl security in code
Franklin Schmidt <fschmidt@gmail.com>
parents:
1501
diff
changeset
|
140 public static SSLServerSocketFactory getSSLServerSocketFactory() { |
d80395468b4e
ssl security in code
Franklin Schmidt <fschmidt@gmail.com>
parents:
1501
diff
changeset
|
141 return (SSLServerSocketFactory)SSLServerSocketFactory.getDefault(); |
d80395468b4e
ssl security in code
Franklin Schmidt <fschmidt@gmail.com>
parents:
1501
diff
changeset
|
142 } |
d80395468b4e
ssl security in code
Franklin Schmidt <fschmidt@gmail.com>
parents:
1501
diff
changeset
|
143 |
1508
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
144 } |