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