diff 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
line wrap: on
line diff
--- a/src/goodjava/io/IoUtils.java	Sat May 16 17:56:02 2020 -0600
+++ b/src/goodjava/io/IoUtils.java	Sun May 17 14:29:33 2020 -0600
@@ -3,14 +3,22 @@
 import java.io.File;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.io.Reader;
+import java.io.InputStreamReader;
+import java.io.Writer;
+import java.io.StringWriter;
 import java.io.IOException;
 import java.nio.file.Files;
 import java.security.Security;
 import javax.net.ssl.SSLSocketFactory;
 import javax.net.ssl.SSLServerSocketFactory;
+import goodjava.logging.Logger;
+import goodjava.logging.LoggerFactory;
 
 
 public final class IoUtils {
+	private static final Logger logger = LoggerFactory.getLogger(IoUtils.class);
+
 	private IoUtils() {}  // never
 
 	public static void move( File from, File to ) throws IOException {
@@ -57,6 +65,25 @@
 		in.close();
 	}
 
+	public static void copyAll(Reader in,Writer out)
+		throws IOException
+	{
+		char[] a = new char[8192];
+		int n;
+		while( (n=in.read(a)) != -1 ) {
+			out.write(a,0,n);
+		}
+		in.close();
+	}
+
+	public static String readAll(Reader in)
+		throws IOException
+	{
+		StringWriter sw = new StringWriter();
+		copyAll(in,sw);
+		return sw.toString();
+	}
+
 	public static long checksum(InputStream in) throws IOException {
 		long cs = 0;
 		int c;
@@ -68,6 +95,32 @@
 	}
 
 
+
+	public static class ProcException extends IOException {
+		private ProcException(String msg) {
+			super(msg);
+		}
+	}
+
+	public static void waitFor(Process proc)
+		throws IOException, ProcException
+	{
+		try {
+			proc.waitFor();
+		} catch(InterruptedException e) {
+			throw new RuntimeException(e);
+		}
+		int exitVal = proc.exitValue();
+		if( exitVal != 0 ) {
+			StringWriter sw = new StringWriter();
+			copyAll( new InputStreamReader(proc.getInputStream()), sw );
+			copyAll( new InputStreamReader(proc.getErrorStream()), sw );
+			String error = sw.toString();
+			throw new ProcException(error);
+		}
+	}
+
+
 	static {
 		// undo restrictions of modern scum
 		Security.setProperty("jdk.tls.disabledAlgorithms","SSLv3, RC4, DES, MD5withRSA, DH keySize < 1024, EC keySize < 224, 3DES_EDE_CBC");