Mercurial Hosting > luan
annotate src/goodjava/io/IoUtils.java @ 1728:30df07a9771c
minor
| author | Franklin Schmidt <fschmidt@gmail.com> |
|---|---|
| date | Tue, 30 Aug 2022 21:59:51 -0600 |
| parents | 2dbcc8360a3e |
| children | 745f756e719b |
| 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 | |
| 51 public static void deleteRecursively(File file) throws IOException { | |
| 52 if( file.isDirectory() && !isSymbolicLink(file) ) { | |
| 53 for( File f : file.listFiles() ) { | |
| 54 deleteRecursively(f); | |
| 55 } | |
| 56 } | |
| 57 delete(file); | |
| 58 } | |
| 59 | |
| 1497 | 60 public static void link(File existing,File link) throws IOException { |
| 61 Files.createLink( link.toPath(), existing.toPath() ); | |
| 62 } | |
| 63 | |
| 64 public static void symlink(File existing,File link) throws IOException { | |
| 65 Files.createSymbolicLink( link.toPath(), existing.toPath() ); | |
| 1488 | 66 } |
| 67 | |
| 1548 | 68 public static long getCreationTime(File f) throws IOException { |
| 69 return ((FileTime)Files.getAttribute(f.toPath(),"creationTime")).toMillis(); | |
| 70 } | |
| 71 | |
| 1494 | 72 public static void copyAll(InputStream in,OutputStream out) |
| 1493 | 73 throws IOException |
| 74 { | |
| 75 byte[] a = new byte[8192]; | |
| 76 int n; | |
| 77 while( (n=in.read(a)) != -1 ) { | |
| 78 out.write(a,0,n); | |
| 79 } | |
| 1499 | 80 in.close(); |
| 1493 | 81 } |
| 82 | |
| 1509 | 83 public static void copyAll(Reader in,Writer out) |
| 84 throws IOException | |
| 85 { | |
| 86 char[] a = new char[8192]; | |
| 87 int n; | |
| 88 while( (n=in.read(a)) != -1 ) { | |
| 89 out.write(a,0,n); | |
| 90 } | |
| 91 in.close(); | |
| 92 } | |
| 93 | |
| 94 public static String readAll(Reader in) | |
| 95 throws IOException | |
| 96 { | |
| 97 StringWriter sw = new StringWriter(); | |
| 98 copyAll(in,sw); | |
| 99 return sw.toString(); | |
| 100 } | |
| 101 | |
|
1508
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
102 public static long checksum(InputStream in) throws IOException { |
|
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
103 long cs = 0; |
|
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
104 int c; |
|
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
105 while( (c=in.read()) != -1 ) { |
|
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
106 cs = 31 * cs + c; |
|
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
107 } |
|
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
108 in.close(); |
|
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
109 return cs; |
|
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
110 } |
|
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
111 |
| 1698 | 112 public static Set<InetAddress> getInetAddresses() throws IOException { |
| 113 Set<InetAddress> set = new HashSet<InetAddress>(); | |
| 114 for( Enumeration<NetworkInterface> e1 = NetworkInterface.getNetworkInterfaces(); e1.hasMoreElements(); ) { | |
| 115 NetworkInterface ni = e1.nextElement(); | |
| 116 for( Enumeration<InetAddress> e2 = ni.getInetAddresses(); e2.hasMoreElements(); ) { | |
| 117 InetAddress ia = e2.nextElement(); | |
| 118 set.add(ia); | |
| 119 } | |
| 120 } | |
| 121 return set; | |
| 122 } | |
|
1506
d80395468b4e
ssl security in code
Franklin Schmidt <fschmidt@gmail.com>
parents:
1501
diff
changeset
|
123 |
| 1509 | 124 |
| 125 public static class ProcException extends IOException { | |
| 126 private ProcException(String msg) { | |
| 127 super(msg); | |
| 128 } | |
| 129 } | |
| 130 | |
| 131 public static void waitFor(Process proc) | |
| 132 throws IOException, ProcException | |
| 133 { | |
| 134 try { | |
| 135 proc.waitFor(); | |
| 136 } catch(InterruptedException e) { | |
| 137 throw new RuntimeException(e); | |
| 138 } | |
| 139 int exitVal = proc.exitValue(); | |
| 140 if( exitVal != 0 ) { | |
| 141 StringWriter sw = new StringWriter(); | |
| 1547 | 142 try { |
| 143 copyAll( new InputStreamReader(proc.getInputStream()), sw ); | |
| 144 } catch(IOException e) {} | |
| 1509 | 145 copyAll( new InputStreamReader(proc.getErrorStream()), sw ); |
| 146 String error = sw.toString(); | |
| 147 throw new ProcException(error); | |
| 148 } | |
| 149 } | |
| 150 | |
| 151 | |
|
1506
d80395468b4e
ssl security in code
Franklin Schmidt <fschmidt@gmail.com>
parents:
1501
diff
changeset
|
152 static { |
|
d80395468b4e
ssl security in code
Franklin Schmidt <fschmidt@gmail.com>
parents:
1501
diff
changeset
|
153 // undo restrictions of modern scum |
|
d80395468b4e
ssl security in code
Franklin Schmidt <fschmidt@gmail.com>
parents:
1501
diff
changeset
|
154 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
|
155 } |
|
d80395468b4e
ssl security in code
Franklin Schmidt <fschmidt@gmail.com>
parents:
1501
diff
changeset
|
156 |
|
d80395468b4e
ssl security in code
Franklin Schmidt <fschmidt@gmail.com>
parents:
1501
diff
changeset
|
157 public static SSLSocketFactory getSSLSocketFactory() { |
|
d80395468b4e
ssl security in code
Franklin Schmidt <fschmidt@gmail.com>
parents:
1501
diff
changeset
|
158 return (SSLSocketFactory)SSLSocketFactory.getDefault(); |
|
d80395468b4e
ssl security in code
Franklin Schmidt <fschmidt@gmail.com>
parents:
1501
diff
changeset
|
159 } |
|
d80395468b4e
ssl security in code
Franklin Schmidt <fschmidt@gmail.com>
parents:
1501
diff
changeset
|
160 |
|
d80395468b4e
ssl security in code
Franklin Schmidt <fschmidt@gmail.com>
parents:
1501
diff
changeset
|
161 public static SSLServerSocketFactory getSSLServerSocketFactory() { |
|
d80395468b4e
ssl security in code
Franklin Schmidt <fschmidt@gmail.com>
parents:
1501
diff
changeset
|
162 return (SSLServerSocketFactory)SSLServerSocketFactory.getDefault(); |
|
d80395468b4e
ssl security in code
Franklin Schmidt <fschmidt@gmail.com>
parents:
1501
diff
changeset
|
163 } |
|
d80395468b4e
ssl security in code
Franklin Schmidt <fschmidt@gmail.com>
parents:
1501
diff
changeset
|
164 |
|
1508
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1506
diff
changeset
|
165 } |
