繁体   English   中英

从一种方法传递给另一种方法时变量不一致

[英]inconsistent variable when passing it from one method to another

我有一个我无法解决的问题,而且我可能不会解决。

我有一个要从main方法传递InputStream的类,问题是,当使用AWS的IOUtils.toString类或commons-io的IOUtils将InputString转换为String时,它们返回空字符串No不管是什么问题,由于在主类中可以正常工作并返回应有的String,但是当我在其他类中使用它(未做任何事情)时,它将空字符串返回给我。

这些是我的课程:

    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);
    }
}

和这个:

    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);
    }
}

我使用了调试工具,并且两个类中的InputStream对象相同

之所以不能,是因为您只能从流中读取一次。

为了能够读取两次,必须调用reset()方法使其返回到开头。 阅读后,调用reset()即可再次阅读!

有些来源不支持重置它,因此您实际上必须再次创建流。 要检查源是否支持它,请使用流的markSupported()方法!

在将流传handleRequest() ,您已经使用了流:

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);
}

当您将其取出时,该方法按您在评论中所说的那样工作。

如果希望数据可重复使用,则如果要再次使用相同的数据,则必须使用reset()方法,或者关闭并重新打开流以使用具有不同数据的对象。

// 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();

使用流后始终关闭它,或者使用try块来利用AutoCloseable 您可以对输出流执行相同的操作:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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