Mercurial Hosting > luan
comparison src/goodjava/io/IoUtils.java @ 1509:0ba144491a42
lucene.backup zip
| author | Franklin Schmidt <fschmidt@gmail.com> |
|---|---|
| date | Sun, 17 May 2020 14:29:33 -0600 |
| parents | 86c5e7000ecf |
| children | f24a9ba7551e |
comparison
equal
deleted
inserted
replaced
| 1508:86c5e7000ecf | 1509:0ba144491a42 |
|---|---|
| 1 package goodjava.io; | 1 package goodjava.io; |
| 2 | 2 |
| 3 import java.io.File; | 3 import java.io.File; |
| 4 import java.io.InputStream; | 4 import java.io.InputStream; |
| 5 import java.io.OutputStream; | 5 import java.io.OutputStream; |
| 6 import java.io.Reader; | |
| 7 import java.io.InputStreamReader; | |
| 8 import java.io.Writer; | |
| 9 import java.io.StringWriter; | |
| 6 import java.io.IOException; | 10 import java.io.IOException; |
| 7 import java.nio.file.Files; | 11 import java.nio.file.Files; |
| 8 import java.security.Security; | 12 import java.security.Security; |
| 9 import javax.net.ssl.SSLSocketFactory; | 13 import javax.net.ssl.SSLSocketFactory; |
| 10 import javax.net.ssl.SSLServerSocketFactory; | 14 import javax.net.ssl.SSLServerSocketFactory; |
| 15 import goodjava.logging.Logger; | |
| 16 import goodjava.logging.LoggerFactory; | |
| 11 | 17 |
| 12 | 18 |
| 13 public final class IoUtils { | 19 public final class IoUtils { |
| 20 private static final Logger logger = LoggerFactory.getLogger(IoUtils.class); | |
| 21 | |
| 14 private IoUtils() {} // never | 22 private IoUtils() {} // never |
| 15 | 23 |
| 16 public static void move( File from, File to ) throws IOException { | 24 public static void move( File from, File to ) throws IOException { |
| 17 Files.move( from.toPath(), to.toPath() ); | 25 Files.move( from.toPath(), to.toPath() ); |
| 18 } | 26 } |
| 55 out.write(a,0,n); | 63 out.write(a,0,n); |
| 56 } | 64 } |
| 57 in.close(); | 65 in.close(); |
| 58 } | 66 } |
| 59 | 67 |
| 68 public static void copyAll(Reader in,Writer out) | |
| 69 throws IOException | |
| 70 { | |
| 71 char[] a = new char[8192]; | |
| 72 int n; | |
| 73 while( (n=in.read(a)) != -1 ) { | |
| 74 out.write(a,0,n); | |
| 75 } | |
| 76 in.close(); | |
| 77 } | |
| 78 | |
| 79 public static String readAll(Reader in) | |
| 80 throws IOException | |
| 81 { | |
| 82 StringWriter sw = new StringWriter(); | |
| 83 copyAll(in,sw); | |
| 84 return sw.toString(); | |
| 85 } | |
| 86 | |
| 60 public static long checksum(InputStream in) throws IOException { | 87 public static long checksum(InputStream in) throws IOException { |
| 61 long cs = 0; | 88 long cs = 0; |
| 62 int c; | 89 int c; |
| 63 while( (c=in.read()) != -1 ) { | 90 while( (c=in.read()) != -1 ) { |
| 64 cs = 31 * cs + c; | 91 cs = 31 * cs + c; |
| 65 } | 92 } |
| 66 in.close(); | 93 in.close(); |
| 67 return cs; | 94 return cs; |
| 95 } | |
| 96 | |
| 97 | |
| 98 | |
| 99 public static class ProcException extends IOException { | |
| 100 private ProcException(String msg) { | |
| 101 super(msg); | |
| 102 } | |
| 103 } | |
| 104 | |
| 105 public static void waitFor(Process proc) | |
| 106 throws IOException, ProcException | |
| 107 { | |
| 108 try { | |
| 109 proc.waitFor(); | |
| 110 } catch(InterruptedException e) { | |
| 111 throw new RuntimeException(e); | |
| 112 } | |
| 113 int exitVal = proc.exitValue(); | |
| 114 if( exitVal != 0 ) { | |
| 115 StringWriter sw = new StringWriter(); | |
| 116 copyAll( new InputStreamReader(proc.getInputStream()), sw ); | |
| 117 copyAll( new InputStreamReader(proc.getErrorStream()), sw ); | |
| 118 String error = sw.toString(); | |
| 119 throw new ProcException(error); | |
| 120 } | |
| 68 } | 121 } |
| 69 | 122 |
| 70 | 123 |
| 71 static { | 124 static { |
| 72 // undo restrictions of modern scum | 125 // undo restrictions of modern scum |
