annotate src/goodjava/io/IoUtils.java @ 1625:57c8baadb357

handle UnsupportedClassVersionError
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 05 Sep 2021 12:32:27 -0600
parents 736ec76bbf42
children 08177ced7fa0
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
6c6ce14db6a8 add goodjava.io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 public static void delete(File file) throws IOException {
6c6ce14db6a8 add goodjava.io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 Files.deleteIfExists( file.toPath() );
6c6ce14db6a8 add goodjava.io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 }
1475
c7b86342857f more IoUtils
Franklin Schmidt <fschmidt@gmail.com>
parents: 1473
diff changeset
32
1501
Franklin Schmidt <fschmidt@gmail.com>
parents: 1499
diff changeset
33 public static void mkdirs(File file) throws IOException {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1499
diff changeset
34 Files.createDirectories( file.toPath() );
Franklin Schmidt <fschmidt@gmail.com>
parents: 1499
diff changeset
35 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 1499
diff changeset
36
1475
c7b86342857f more IoUtils
Franklin Schmidt <fschmidt@gmail.com>
parents: 1473
diff changeset
37 public static boolean isSymbolicLink(File file) {
c7b86342857f more IoUtils
Franklin Schmidt <fschmidt@gmail.com>
parents: 1473
diff changeset
38 return Files.isSymbolicLink(file.toPath());
c7b86342857f more IoUtils
Franklin Schmidt <fschmidt@gmail.com>
parents: 1473
diff changeset
39 }
c7b86342857f more IoUtils
Franklin Schmidt <fschmidt@gmail.com>
parents: 1473
diff changeset
40
c7b86342857f more IoUtils
Franklin Schmidt <fschmidt@gmail.com>
parents: 1473
diff changeset
41 public static void deleteRecursively(File file) throws IOException {
c7b86342857f more IoUtils
Franklin Schmidt <fschmidt@gmail.com>
parents: 1473
diff changeset
42 if( file.isDirectory() && !isSymbolicLink(file) ) {
c7b86342857f more IoUtils
Franklin Schmidt <fschmidt@gmail.com>
parents: 1473
diff changeset
43 for( File f : file.listFiles() ) {
c7b86342857f more IoUtils
Franklin Schmidt <fschmidt@gmail.com>
parents: 1473
diff changeset
44 deleteRecursively(f);
c7b86342857f more IoUtils
Franklin Schmidt <fschmidt@gmail.com>
parents: 1473
diff changeset
45 }
c7b86342857f more IoUtils
Franklin Schmidt <fschmidt@gmail.com>
parents: 1473
diff changeset
46 }
c7b86342857f more IoUtils
Franklin Schmidt <fschmidt@gmail.com>
parents: 1473
diff changeset
47 delete(file);
c7b86342857f more IoUtils
Franklin Schmidt <fschmidt@gmail.com>
parents: 1473
diff changeset
48 }
c7b86342857f more IoUtils
Franklin Schmidt <fschmidt@gmail.com>
parents: 1473
diff changeset
49
1497
f04bfbb08721 link_from
Franklin Schmidt <fschmidt@gmail.com>
parents: 1494
diff changeset
50 public static void link(File existing,File link) throws IOException {
f04bfbb08721 link_from
Franklin Schmidt <fschmidt@gmail.com>
parents: 1494
diff changeset
51 Files.createLink( link.toPath(), existing.toPath() );
f04bfbb08721 link_from
Franklin Schmidt <fschmidt@gmail.com>
parents: 1494
diff changeset
52 }
f04bfbb08721 link_from
Franklin Schmidt <fschmidt@gmail.com>
parents: 1494
diff changeset
53
f04bfbb08721 link_from
Franklin Schmidt <fschmidt@gmail.com>
parents: 1494
diff changeset
54 public static void symlink(File existing,File link) throws IOException {
f04bfbb08721 link_from
Franklin Schmidt <fschmidt@gmail.com>
parents: 1494
diff changeset
55 Files.createSymbolicLink( link.toPath(), existing.toPath() );
1488
af55cfad6e12 start lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
56 }
af55cfad6e12 start lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
57
1548
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1547
diff changeset
58 public static long getCreationTime(File f) throws IOException {
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1547
diff changeset
59 return ((FileTime)Files.getAttribute(f.toPath(),"creationTime")).toMillis();
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1547
diff changeset
60 }
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1547
diff changeset
61
1494
91c167099462 more io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1493
diff changeset
62 public static void copyAll(InputStream in,OutputStream out)
1493
471ef3e6a84e more io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1488
diff changeset
63 throws IOException
471ef3e6a84e more io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1488
diff changeset
64 {
471ef3e6a84e more io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1488
diff changeset
65 byte[] a = new byte[8192];
471ef3e6a84e more io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1488
diff changeset
66 int n;
471ef3e6a84e more io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1488
diff changeset
67 while( (n=in.read(a)) != -1 ) {
471ef3e6a84e more io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1488
diff changeset
68 out.write(a,0,n);
471ef3e6a84e more io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1488
diff changeset
69 }
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents: 1497
diff changeset
70 in.close();
1493
471ef3e6a84e more io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1488
diff changeset
71 }
471ef3e6a84e more io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1488
diff changeset
72
1509
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
73 public static void copyAll(Reader in,Writer out)
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
74 throws IOException
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
75 {
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
76 char[] a = new char[8192];
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
77 int n;
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
78 while( (n=in.read(a)) != -1 ) {
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
79 out.write(a,0,n);
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
80 }
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
81 in.close();
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
82 }
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
83
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
84 public static String readAll(Reader in)
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
85 throws IOException
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 StringWriter sw = new StringWriter();
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
88 copyAll(in,sw);
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
89 return sw.toString();
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
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
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
103
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
104 public static class ProcException extends IOException {
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
105 private ProcException(String msg) {
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
106 super(msg);
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 }
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
109
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
110 public static void waitFor(Process proc)
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
111 throws IOException, ProcException
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 try {
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
114 proc.waitFor();
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
115 } catch(InterruptedException e) {
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
116 throw new RuntimeException(e);
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
117 }
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
118 int exitVal = proc.exitValue();
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
119 if( exitVal != 0 ) {
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
120 StringWriter sw = new StringWriter();
1547
Franklin Schmidt <fschmidt@gmail.com>
parents: 1509
diff changeset
121 try {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1509
diff changeset
122 copyAll( new InputStreamReader(proc.getInputStream()), sw );
Franklin Schmidt <fschmidt@gmail.com>
parents: 1509
diff changeset
123 } catch(IOException e) {}
1509
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
124 copyAll( new InputStreamReader(proc.getErrorStream()), sw );
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
125 String error = sw.toString();
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
126 throw new ProcException(error);
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
127 }
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
128 }
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
129
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
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 }