简体   繁体   中英

inconsistent variable when passing it from one method to another

I have a problem that I have not been able to solve and it does not occur to me that it could be.

I have a class to which I am passing an InputStream from the main method, the problem is that when transforming the InputString to String with the class IOUtils.toString of AWS, or with the IOUtils of commons-io, they return an empty String No matter what the problem may be, since inside the main class, it works correctly and returns the String it should, but when I use it inside the other class (without having done anything), it returns the empty String to me.

these are my classes:

    public class Main {

    public static void main(String [] args) throws IOException {

        InputStream inputStream = new ByteArrayInputStream("{\"name\":\"Camilo\",\"functionName\":\"hello\"}".getBytes());
        OutputStream outputStream = new ByteArrayOutputStream();

        LambdaExecutor lambdaExecutor = new LambdaExecutor();


        String test = IOUtils.toString(inputStream); //this test variable have "{\"name\":\"Camilo\",\"functionName\":\"hello\"}"
        lambdaExecutor.handleRequest(inputStream,outputStream);
    }
}

and this:

    public class LambdaExecutor{

    private FrontController frontController;
    public LambdaExecutor(){
        this.frontController = new FrontController();
    }

    public void handleRequest(InputStream inputStream, OutputStream outputStream) throws IOException {
        //Service service = frontController.findService(inputStream);

        String test = IOUtils.toString(inputStream); //this test variable have "" <-empty String

        System.exit(0);
        //service.execute(inputStream, outputStream, context);
    }
}

I used the debug tool, and the InputStream object is the same in both classes

The reason you can't is because you can only read from a stream once.

To be able to read twice, you must call the reset() method for it to return to the beginning. After reading, call reset() and you can read it again!

Some sources don't support resetting it so you would actually have to create the stream again. To check if the source supports it, use the markSupported() method of the stream!

By the time that you've passed the stream into handleRequest() , you've already consumed the stream:

public static void main(String [] args) throws IOException {

    InputStream inputStream = new ByteArrayInputStream("{\"name\":\"Camilo\",\"functionName\":\"hello\"}".getBytes());
    OutputStream outputStream = new ByteArrayOutputStream();

    LambdaExecutor lambdaExecutor = new LambdaExecutor();


    String test = IOUtils.toString(inputStream); //this consumes the stream, and nothing more can be read from it
    lambdaExecutor.handleRequest(inputStream,outputStream);
}

When you took that out, the method worked as, as you said in the comments.

If you want the data to be re-useable, you'll have to use the reset() method if you want the same data again, or close and re-open the stream to re-use the object with different data.

// have your data 
byte[] data = "{\"name\":\"Camilo\",\"functionName\":\"hello\"}".getBytes();
// open the stream
InputStream inputStream = new ByteArrayInputStream(data);
...
// do something with the inputStream, and reset if you need the same data again
if(inputStream.markSupported()) {
    inputStream.reset();
} else {
    inputStream.close();
    inputStream = new ByteArrayInputStream(data);
}
...
// close the stream after use
inputStream.close();

Always close the stream after you use it, or use a try block to take advantage of AutoCloseable ; you can do the same with the output stream:

try (InputStream inputStream = new ByteArrayInputStream(data);
    OutputStream outputStream = new ByteArrayOutputStream()) {
    lambdaExecutor.handleRequest(inputStream, outputStream);
} // auto-closed the streams

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM