Skip to content

Commit f09d68c

Browse files
committed
feat: optimized performance; replaced deprecated api usage in tests;
1 parent e7d36b3 commit f09d68c

File tree

6 files changed

+59
-109
lines changed

6 files changed

+59
-109
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,25 @@ jPowerShell2 is a fork of [jPowerShell](https://github.com/profesorfalken/jPower
77

88
## 💻 Installation
99

10-
To install jPowerShell you can add the dependecy to your software project management tool: https://search.maven.org/artifact/io.github.autocomplete1/jPowerShell2/1.0.1/jar
10+
To install jPowerShell you can add the dependecy to your software project management tool: https://search.maven.org/artifact/io.github.autocomplete1/jPowerShell2/1.0.2/jar
1111

1212
Maven:
1313
```
1414
<dependency>
1515
<groupId>io.github.autocomplete1</groupId>
1616
<artifactId>jPowerShell2</artifactId>
17-
<version>1.0.1</version>
17+
<version>1.0.2</version>
1818
<scope>compile</scope>
1919
</dependency>
2020
```
2121

2222
Gradle:
2323
```
24-
implementation 'io.github.autocomplete1:jPowerShell2:1.0.1'
24+
implementation 'io.github.autocomplete1:jPowerShell2:1.0.2'
2525
```
2626

2727
Instead, you can direct download the JAR file and add it to your classpath.
28-
https://repo1.maven.org/maven2/io/github/autocomplete1/jPowerShell2/1.0.1/jPowerShell2-1.0.1.jar
28+
https://repo1.maven.org/maven2/io/github/autocomplete1/jPowerShell2/1.0.2/jPowerShell2-1.0.2.jar
2929

3030
## ⚡️ Usage
3131

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ plugins {
66
}
77

88
group = 'io.github.autocomplete1'
9-
version = '1.0.1'
9+
version = '1.0.2'
1010
description = 'Simple Java API to interact with PowerShell console'
1111
java.sourceCompatibility = JavaVersion.VERSION_1_8
1212

src/main/java/io/github/autocomplete1/PowerShell.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ public class PowerShell implements AutoCloseable {
4646
// Writer to send commands
4747
private PrintWriter commandWriter;
4848

49+
private BufferedReader outputReader;
50+
4951
// Threaded session variables
5052
private boolean closed = false;
5153
private ExecutorService executorService;
@@ -68,7 +70,7 @@ private PowerShell() {
6870
}
6971

7072
/**
71-
* Allows to override jPowerShell configuration using a map of key/value <br>
73+
* Allows overriding jPowerShell configuration using a map of key/value <br>
7274
* Default values are taken from file <i>jpowershell.properties</i>, which can
7375
* be replaced just setting it on project classpath
7476
* <p>
@@ -114,8 +116,8 @@ public PowerShell configuration(Map<String, String> config) {
114116
}
115117

116118
/**
117-
* Creates a session in PowerShell console an returns an instance which allows
118-
* to execute commands in PowerShell context.<br>
119+
* Creates a session in PowerShell console which returns an instance which allows
120+
* executing commands in PowerShell context.<br>
119121
* It uses the default PowerShell installation in the system.
120122
*
121123
* @return an instance of the class
@@ -126,8 +128,8 @@ public static PowerShell openSession() throws PowerShellNotAvailableException {
126128
}
127129

128130
/**
129-
* Creates a session in PowerShell console an returns an instance which allows
130-
* to execute commands in PowerShell context.<br>
131+
* Creates a session in PowerShell console which returns an instance which allows
132+
* executing commands in PowerShell context.<br>
131133
* This method allows to define a PowersShell executable path different from default
132134
*
133135
* @param customPowerShellExecutablePath the path of powershell executable. If you are using
@@ -140,8 +142,8 @@ public static PowerShell openSession(String customPowerShellExecutablePath) thro
140142
}
141143

142144
/**
143-
* Creates a session in PowerShell console an returns an instance which allows
144-
* to execute commands in PowerShell context.<br>
145+
* Creates a session in PowerShell console which returns an instance which allows
146+
* executing commands in PowerShell context.<br>
145147
* This method allows to define a PowersShell executable path different from default
146148
*
147149
* @param customPowerShellExecutablePath the path of powershell executable. If you are using
@@ -158,11 +160,11 @@ public static PowerShell openSession(String customPowerShellExecutablePath, Map<
158160

159161
String powerShellExecutablePath = customPowerShellExecutablePath == null ? (OSDetector.isWindows() ? DEFAULT_WIN_EXECUTABLE : DEFAULT_LINUX_EXECUTABLE) : customPowerShellExecutablePath;
160162

161-
return powerShell.initalize(powerShellExecutablePath);
163+
return powerShell.initialize(powerShellExecutablePath);
162164
}
163165

164166
// Initializes PowerShell console in which we will enter the commands
165-
private PowerShell initalize(String powerShellExecutablePath) throws PowerShellNotAvailableException {
167+
private PowerShell initialize(String powerShellExecutablePath) throws PowerShellNotAvailableException {
166168
String codePage = PowerShellCodepage.getIdentifierByCodePageName(Charset.defaultCharset().name());
167169
ProcessBuilder pb;
168170

@@ -193,6 +195,8 @@ private PowerShell initalize(String powerShellExecutablePath) throws PowerShellN
193195
//Prepare writer that will be used to send commands to powershell
194196
this.commandWriter = new PrintWriter(new OutputStreamWriter(new BufferedOutputStream(p.getOutputStream())), true);
195197

198+
this.outputReader = new BufferedReader(new InputStreamReader(p.getInputStream()));
199+
196200
// Init thread pool. 2 threads are needed: one to write and read console and the other to close it
197201
this.executorService = Executors.newFixedThreadPool(2);
198202

@@ -219,8 +223,7 @@ public PowerShellResponse executeCommand(String command) {
219223

220224
checkState();
221225

222-
PowerShellCommandProcessor commandProcessor = new PowerShellCommandProcessor("standard", p.getInputStream(),
223-
this.waitPause, this.scriptMode);
226+
PowerShellCommandProcessor commandProcessor = new PowerShellCommandProcessor(this.outputReader, this.waitPause, this.scriptMode);
224227
Future<String> result = executorService.submit(commandProcessor);
225228

226229
// Launch command
@@ -251,7 +254,7 @@ public PowerShellResponse executeCommand(String command) {
251254
}
252255

253256
/**
254-
* Execute a single command in PowerShell consolscriptModee and gets result
257+
* Execute a single command in PowerShell console scriptMode and gets result
255258
*
256259
* @param command the command to execute
257260
* @return response with the output of the command
@@ -269,7 +272,7 @@ public static PowerShellResponse executeSingleCommand(String command) {
269272
}
270273

271274
/**
272-
* Allows to chain command executions providing a more fluent API.<p>
275+
* Allows chaining command executions providing a more fluent API.<p>
273276
* <p>
274277
* This method allows also to optionally handle the response in a closure
275278
*
@@ -447,7 +450,7 @@ public void close() {
447450
commandWriter.close();
448451
try {
449452
if (p.isAlive()) {
450-
p.getInputStream().close();
453+
outputReader.close();
451454
}
452455
} catch (IOException ex) {
453456
logger.log(Level.SEVERE,

src/main/java/io/github/autocomplete1/PowerShellCommandProcessor.java

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717

1818
import java.io.BufferedReader;
1919
import java.io.IOException;
20-
import java.io.InputStream;
21-
import java.io.InputStreamReader;
2220
import java.util.concurrent.Callable;
2321
import java.util.logging.Level;
2422
import java.util.logging.Logger;
@@ -33,25 +31,23 @@ class PowerShellCommandProcessor implements Callable<String> {
3331

3432
private static final String CRLF = "\r\n";
3533

36-
private final BufferedReader reader;
37-
3834
private boolean closed = false;
3935

36+
private final BufferedReader outputReader;
37+
4038
private final boolean scriptMode;
4139

4240
private final int waitPause;
4341

4442
/**
4543
* Constructor that takes the output and the input of the PowerShell session
4644
*
47-
* @param name the name of the CommandProcessor
48-
* @param inputStream the stream needed to read the command output
49-
* @param waitPause long the wait pause in milliseconds
50-
* @param scriptMode boolean indicates if the command executes a script
45+
* @param outputReader outputReader of the powershell session
46+
* @param waitPause long the wait pause in milliseconds
47+
* @param scriptMode boolean indicates if the command executes a script
5148
*/
52-
public PowerShellCommandProcessor(String name, InputStream inputStream, int waitPause, boolean scriptMode) {
53-
this.reader = new BufferedReader(new InputStreamReader(
54-
inputStream));
49+
public PowerShellCommandProcessor(BufferedReader outputReader, int waitPause, boolean scriptMode) {
50+
this.outputReader = outputReader;
5551
this.waitPause = waitPause;
5652
this.scriptMode = scriptMode;
5753
}
@@ -81,9 +77,9 @@ public String call() throws InterruptedException {
8177
//Reads all data from output
8278
private void readData(StringBuilder powerShellOutput) throws IOException {
8379
String line;
84-
while (null != (line = this.reader.readLine())) {
80+
while (null != (line = this.outputReader.readLine())) {
8581

86-
//In the case of script mode it finish when the last line is read
82+
//In the case of script mode it finishes when the last line is read
8783
if (this.scriptMode) {
8884
if (line.equals(PowerShell.END_SCRIPT_STRING)) {
8985
break;
@@ -108,7 +104,7 @@ private void readData(StringBuilder powerShellOutput) throws IOException {
108104
//Checks when we can start reading the output. Timeout if it takes too long in order to avoid hangs
109105
private boolean startReading() throws IOException, InterruptedException {
110106
//If the reader is not ready, gives it some milliseconds
111-
while (!this.reader.ready()) {
107+
while (!this.outputReader.ready()) {
112108
Thread.sleep(this.waitPause);
113109
if (this.closed) {
114110
return false;
@@ -117,20 +113,20 @@ private boolean startReading() throws IOException, InterruptedException {
117113
return true;
118114
}
119115

120-
//Checks when we the reader can continue to read.
116+
//Checks when we have the reader can continue to read.
121117
private boolean canContinueReading() throws IOException, InterruptedException {
122118
//If the reader is not ready, gives it some milliseconds
123119
//It is important to do that, because the ready method guarantees that the readline will not be blocking
124-
if (!this.reader.ready()) {
120+
if (!this.outputReader.ready()) {
125121
Thread.sleep(this.waitPause);
126122
}
127123

128124
//If not ready yet, wait a moment to make sure it is finished
129-
if (!this.reader.ready()) {
125+
if (!this.outputReader.ready()) {
130126
Thread.sleep(50);
131127
}
132128

133-
return this.reader.ready();
129+
return this.outputReader.ready();
134130
}
135131

136132
/**

src/main/java/io/github/autocomplete1/PowerShellResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
package io.github.autocomplete1;
1717

1818
/**
19-
* Response of PowerShell command. This object encapsulate all the useful
19+
* Response of PowerShell command. This object encapsulates all the useful
2020
* returned information
2121
*
2222
* @author Javier Garcia Alonso

0 commit comments

Comments
 (0)