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