aboutsummaryrefslogtreecommitdiffstats
path: root/cdf/src/cdf-prop-value/cdf-util/src/main/java/org/onap/dcae/cdf/util/common/Popen.java
blob: d7f58462e9c05ddb5826302523b45b68eb403839 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
    Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.  
 
    Licensed under the Apache License, Version 2.0 (the "License"); 
    you may not use this code except in compliance 
    with the License. You may obtain a copy of the License 
    at http://www.apache.org/licenses/LICENSE-2.0 
 
    Unless required by applicable law or agreed to in writing, software  
    distributed under the License is distributed on an "AS IS" BASIS,  
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or  
    implied. See the License for the specific language governing  
    permissions and limitations under the License. 

*/
package org.onap.dcae.cdf.util.common;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;

public class Popen {
    public static class Results {
	public final String stdout, stderr;
	public final int exitValue;
	public Results(String so, String se, int e) {
	    stdout = so; stderr = se; exitValue = e;
	}
    }

    public static Results popen(String cmd) throws java.io.IOException, java.lang.InterruptedException {
	return popen(cmd, null);
    }

    public static Results popen(String cmd, String stdin) throws java.io.IOException, java.lang.InterruptedException {
	Process process = Runtime.getRuntime().exec(cmd);
	return proc(process, stdin);
    }

    public static Results popen(String[] args) throws java.io.IOException, java.lang.InterruptedException {
	return popen(args, null);
    }

    public static Results popen(String[] args, String stdin) throws java.io.IOException, java.lang.InterruptedException {
	Process process = Runtime.getRuntime().exec(args);
	return proc(process, stdin);
    }

    private static Results proc(Process process, String stdin) throws java.io.IOException, java.lang.InterruptedException {
	OutputStream pinput = process.getOutputStream();
	InputStream poutput = process.getInputStream();
	InputStream perror = process.getErrorStream();

	if (stdin != null)
	    pinput.write(stdin.getBytes());
	pinput.close();

	String stdout = captureStream(poutput);
	poutput.close();
	String stderr = captureStream(perror);
	perror.close();
	process.waitFor();
	//	System.out.println("stdin=\nnvvvvvvvvvvvvvvvv\n");
	//	System.out.println(stdin);
	//	System.out.println("^^^^^^^^^^^^^^^^");
	//	System.out.println("stdout=\nvvvvvvvvvvvvvvvv\n");
	//	System.out.println(stdout);
	//	System.out.println("^^^^^^^^^^^^^^^^");
	//	System.out.println("stderr=\nvvvvvvvvvvvvvvvv\n");
	//	System.out.println(stderr);
	//	System.out.println("^^^^^^^^^^^^^^^^");
	return new Results(stdout, stderr, process.exitValue());
    }

    private static String captureStream(InputStream inp) throws java.io.IOException {
	byte[] buf = new byte[8192];
	StringBuffer out = new StringBuffer();
	int b;
	while ((b = inp.read(buf)) > 0) {
	    out.append(new String(buf, 0, b));
	}
	return out.toString();
    }
}