#Input Stream Limit

12 messages · Page 1 of 1 (latest)

scarlet meteor
#

I have the following code and I am under the impression that either buffered reader or something inside java.lang.process is unable to return large sets of data when used with a future.

noble notchBOT
#

Hey, @scarlet meteor!
Please remember to /close this post once your question has been answered!

scarlet meteor
#

The purpose of this code is to open an instance of powershell in the backend and return its value once the command is finished executing. The code below works with powershell commands that have a small output but it seems the larger the output the less likely it is to work

#
package Future;

import java.util.ArrayList;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

public class Main {
    public static void main(String[] args) {
        Powershell powershell = new Powershell();
        Future<ArrayList<String>> fut = powershell.execute("Get-AppxPackage –AllUsers");
        try {
            fut.get().forEach(System.out::println);
        } catch (InterruptedException | ExecutionException e) {
            throw new RuntimeException(e);
        }
        powershell.close();
    }
}```
#
package Future;

import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class Powershell {

    private final ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2);

    public Future<ArrayList<String>> execute(String command) {
        String[] commandList = new String[]{"powershell.exe", "-Command", command};
        ProcessBuilder pb = new ProcessBuilder(commandList);
        try {
            Process p = pb.start();
            return this.executorService.submit(new Reader(p.getInputStream()));
        } catch (IOException ex) {
            throw new RuntimeException("Cannot execute PowerShell.exe", ex);
        }
    }

    public void close() {
        this.executorService.shutdown();
    }

}```
#
package Future;

import java.io.*;
import java.util.ArrayList;
import java.util.concurrent.Callable;
import java.util.logging.Logger;

public class Reader implements Callable<ArrayList<String>> {

    private String message;
    private BufferedReader reader;

    public Reader(InputStream inputStream) {
        this.reader = new BufferedReader(new InputStreamReader(inputStream));
        this.message = ( null != message ) ? message : "";
    }

    /**
     * The function is called when the thread is started. It reads the output of the process and prints it to the terminal
     *
     * @return An ArrayList of Strings
     */
    @Override
    public ArrayList<String> call() throws Exception {
        String line;
        ArrayList<String> test = new ArrayList<>();
        try {
            while (null != (line = this.reader.readLine())) {
                test.add(line);
            }
        } catch (IOException e) {
            System.err.println("ERROR: " + e.getMessage());
        }
        return test;
    }
}```
scarlet meteor
#

seems like no input is sent in large commands

unique current
#

@scarlet meteor have you tried getting error stream? maybe its outputting error

scarlet meteor