annotate src/goodjava/io/IoUtils.java @ 1683:ea56e3b04c03

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 05 Jun 2022 12:44:38 -0600
parents 08177ced7fa0
children 2dbcc8360a3e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1473
6c6ce14db6a8 add goodjava.io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 package goodjava.io;
6c6ce14db6a8 add goodjava.io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2
6c6ce14db6a8 add goodjava.io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 import java.io.File;
1493
471ef3e6a84e more io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1488
diff changeset
4 import java.io.InputStream;
471ef3e6a84e more io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1488
diff changeset
5 import java.io.OutputStream;
1509
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
6 import java.io.Reader;
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
7 import java.io.InputStreamReader;
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
8 import java.io.Writer;
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
9 import java.io.StringWriter;
1473
6c6ce14db6a8 add goodjava.io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 import java.io.IOException;
6c6ce14db6a8 add goodjava.io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 import java.nio.file.Files;
1548
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1547
diff changeset
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
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
16 import goodjava.logging.Logger;
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
17 import goodjava.logging.LoggerFactory;
1473
6c6ce14db6a8 add goodjava.io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18
6c6ce14db6a8 add goodjava.io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19
6c6ce14db6a8 add goodjava.io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 public final class IoUtils {
1509
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
21 private static final Logger logger = LoggerFactory.getLogger(IoUtils.class);
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
22
1473
6c6ce14db6a8 add goodjava.io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 private IoUtils() {} // never
6c6ce14db6a8 add goodjava.io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24
6c6ce14db6a8 add goodjava.io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 public static void move( File from, File to ) throws IOException {
6c6ce14db6a8 add goodjava.io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 Files.move( from.toPath(), to.toPath() );
6c6ce14db6a8 add goodjava.io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 }
6c6ce14db6a8 add goodjava.io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28
1661
08177ced7fa0 add file.copy_to
Franklin Schmidt <fschmidt@gmail.com>
parents: 1548
diff changeset
29 public static void copy( File from, File to ) throws IOException {
08177ced7fa0 add file.copy_to
Franklin Schmidt <fschmidt@gmail.com>
parents: 1548
diff changeset
30 Files.copy( from.toPath(), to.toPath() );
08177ced7fa0 add file.copy_to
Franklin Schmidt <fschmidt@gmail.com>
parents: 1548
diff changeset
31 }
08177ced7fa0 add file.copy_to
Franklin Schmidt <fschmidt@gmail.com>
parents: 1548
diff changeset
32
1473
6c6ce14db6a8 add goodjava.io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 public static void delete(File file) throws IOException {
6c6ce14db6a8 add goodjava.io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 Files.deleteIfExists( file.toPath() );
6c6ce14db6a8 add goodjava.io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 }
1475
c7b86342857f more IoUtils
Franklin Schmidt <fschmidt@gmail.com>
parents: 1473
diff changeset
36
1501
Franklin Schmidt <fschmidt@gmail.com>
parents: 1499
diff changeset
37 public static void mkdirs(File file) throws IOException {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1499
diff changeset
38 Files.createDirectories( file.toPath() );
Franklin Schmidt <fschmidt@gmail.com>
parents: 1499
diff changeset
39 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 1499
diff changeset
40
1475
c7b86342857f more IoUtils
Franklin Schmidt <fschmidt@gmail.com>
parents: 1473
diff changeset
41 public static boolean isSymbolicLink(File file) {
c7b86342857f more IoUtils
Franklin Schmidt <fschmidt@gmail.com>
parents: 1473
diff changeset
42 return Files.isSymbolicLink(file.toPath());
c7b86342857f more IoUtils
Franklin Schmidt <fschmidt@gmail.com>
parents: 1473
diff changeset
43 }
c7b86342857f more IoUtils
Franklin Schmidt <fschmidt@gmail.com>
parents: 1473
diff changeset
44
c7b86342857f more IoUtils
Franklin Schmidt <fschmidt@gmail.com>
parents: 1473
diff changeset
45 public static void deleteRecursively(File file) throws IOException {
c7b86342857f more IoUtils
Franklin Schmidt <fschmidt@gmail.com>
parents: 1473
diff changeset
46 if( file.isDirectory() && !isSymbolicLink(file) ) {
c7b86342857f more IoUtils
Franklin Schmidt <fschmidt@gmail.com>
parents: 1473
diff changeset
47 for( File f : file.listFiles() ) {
c7b86342857f more IoUtils
Franklin Schmidt <fschmidt@gmail.com>
parents: 1473
diff changeset
48 deleteRecursively(f);
c7b86342857f more IoUtils
Franklin Schmidt <fschmidt@gmail.com>
parents: 1473
diff changeset
49 }
c7b86342857f more IoUtils
Franklin Schmidt <fschmidt@gmail.com>
parents: 1473
diff changeset
50 }
c7b86342857f more IoUtils
Franklin Schmidt <fschmidt@gmail.com>
parents: 1473
diff changeset
51 delete(file);
c7b86342857f more IoUtils
Franklin Schmidt <fschmidt@gmail.com>
parents: 1473
diff changeset
52 }
c7b86342857f more IoUtils
Franklin Schmidt <fschmidt@gmail.com>
parents: 1473
diff changeset
53
1497
f04bfbb08721 link_from
Franklin Schmidt <fschmidt@gmail.com>
parents: 1494
diff changeset
54 public static void link(File existing,File link) throws IOException {
f04bfbb08721 link_from
Franklin Schmidt <fschmidt@gmail.com>
parents: 1494
diff changeset
55 Files.createLink( link.toPath(), existing.toPath() );
f04bfbb08721 link_from
Franklin Schmidt <fschmidt@gmail.com>
parents: 1494
diff changeset
56 }
f04bfbb08721 link_from
Franklin Schmidt <fschmidt@gmail.com>
parents: 1494
diff changeset
57
f04bfbb08721 link_from
Franklin Schmidt <fschmidt@gmail.com>
parents: 1494
diff changeset
58 public static void symlink(File existing,File link) throws IOException {
f04bfbb08721 link_from
Franklin Schmidt <fschmidt@gmail.com>
parents: 1494
diff changeset
59 Files.createSymbolicLink( link.toPath(), existing.toPath() );
1488
af55cfad6e12 start lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
60 }
af55cfad6e12 start lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
61
1548
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1547
diff changeset
62 public static long getCreationTime(File f) throws IOException {
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1547
diff changeset
63 return ((FileTime)Files.getAttribute(f.toPath(),"creationTime")).toMillis();
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1547
diff changeset
64 }
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1547
diff changeset
65
1494
91c167099462 more io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1493
diff changeset
66 public static void copyAll(InputStream in,OutputStream out)
1493
471ef3e6a84e more io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1488
diff changeset
67 throws IOException
471ef3e6a84e more io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1488
diff changeset
68 {
471ef3e6a84e more io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1488
diff changeset
69 byte[] a = new byte[8192];
471ef3e6a84e more io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1488
diff changeset
70 int n;
471ef3e6a84e more io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1488
diff changeset
71 while( (n=in.read(a)) != -1 ) {
471ef3e6a84e more io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1488
diff changeset
72 out.write(a,0,n);
471ef3e6a84e more io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1488
diff changeset
73 }
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents: 1497
diff changeset
74 in.close();
1493
471ef3e6a84e more io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1488
diff changeset
75 }
471ef3e6a84e more io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1488
diff changeset
76
1509
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
77 public static void copyAll(Reader in,Writer out)
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
78 throws IOException
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
79 {
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
80 char[] a = new char[8192];
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
81 int n;
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
82 while( (n=in.read(a)) != -1 ) {
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
83 out.write(a,0,n);
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
84 }
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
85 in.close();
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
86 }
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
87
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
88 public static String readAll(Reader in)
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
89 throws IOException
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
90 {
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
91 StringWriter sw = new StringWriter();
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
92 copyAll(in,sw);
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
93 return sw.toString();
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
94 }
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
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
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
107
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
108 public static class ProcException extends IOException {
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
109 private ProcException(String msg) {
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
110 super(msg);
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
111 }
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
112 }
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
113
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
114 public static void waitFor(Process proc)
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
115 throws IOException, ProcException
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
116 {
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
117 try {
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
118 proc.waitFor();
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
119 } catch(InterruptedException e) {
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
120 throw new RuntimeException(e);
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
121 }
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
122 int exitVal = proc.exitValue();
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
123 if( exitVal != 0 ) {
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
124 StringWriter sw = new StringWriter();
1547
Franklin Schmidt <fschmidt@gmail.com>
parents: 1509
diff changeset
125 try {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1509
diff changeset
126 copyAll( new InputStreamReader(proc.getInputStream()), sw );
Franklin Schmidt <fschmidt@gmail.com>
parents: 1509
diff changeset
127 } catch(IOException e) {}
1509
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
128 copyAll( new InputStreamReader(proc.getErrorStream()), sw );
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
129 String error = sw.toString();
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
130 throw new ProcException(error);
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
131 }
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
132 }
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
133
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
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 }