首页 > 编程语言 > 详细

java 流不能复用 stream has already been operated upon or closed

时间:2019-12-28 18:16:53      阅读:3238      评论:0      收藏:0      [点我收藏+]

场景

//防止sql注入
    Stream<String> idList =accDocIDList.stream().map((x) ->
                {
                if (x.matches("[^a-zA-Z0-9-]"))
                {
                    //throw new BalanceException("xxx-xxx", "hehe");
                    throw new RuntimeException("非法凭证ID");
                }
                 return String.format("‘%1$s‘", x);
            });

        parameters = new IDbParameter[]
                {
                        bqlExecuter.makeInParam("funcID", funcID),
                        bqlExecuter.makeInParam("userID", userID),
                        bqlExecuter.makeInParam("accOrgID", accOrgID),
                        bqlExecuter.makeInParam("AccLedgerID", ledger),
                        bqlExecuter.makeInParam("year", year),
                        bqlExecuter.makeInParam("flag", "1"),
                        bqlExecuter.makeInParam("accPeriodID", accPeriodID),
                };
        //分页
        int pageSize = 900;
        int pageNum = 0;
        while (pageNum * pageSize < accDocIDList.size())
        {
            List<String> list = idList.skip(pageNum*pageSize).limit(pageSize).collect(Collectors.toList());  //此处会重新使用流 第二次循环会报错 stream has already been operated upon or closed 
            String idsql = bql + " and A.ID in (" + String.join(",", list) + ")";
            var ret = bqlExecuter.executeBqlStatement(idsql, refEntityIDs, parameters);
            pageNum++;
        }

解决方法  使用Supplier

//防止sql注入
        Supplier< Stream<String>> idList = ()->accDocIDList.stream().map((x) ->
                {
                if (x.matches("[^a-zA-Z0-9-]"))
                {
                    //throw new BalanceException("xxx-xxx", "hehe");
                    throw new RuntimeException("非法凭证ID");
                }
                 return String.format("‘%1$s‘", x);
            });

        parameters = new IDbParameter[]
                {
                        bqlExecuter.makeInParam("funcID", funcID),
                        bqlExecuter.makeInParam("userID", userID),
                        bqlExecuter.makeInParam("accOrgID", accOrgID),
                        bqlExecuter.makeInParam("AccLedgerID", ledger),
                        bqlExecuter.makeInParam("year", year),
                        bqlExecuter.makeInParam("flag", "1"),
                        bqlExecuter.makeInParam("accPeriodID", accPeriodID),
                };
        //分页
        int pageSize = 900;
        int pageNum = 0;
        while (pageNum * pageSize < accDocIDList.size())
        {
            List<String> list = idList.get().skip(pageNum*pageSize).limit(pageSize).collect(Collectors.toList());
            String idsql = bql + " and A.ID in (" + String.join(",", list) + ")";
            var ret = bqlExecuter.executeBqlStatement(idsql, refEntityIDs, parameters);
            pageNum++;
        }

java 流不能复用 stream has already been operated upon or closed

原文:https://www.cnblogs.com/wolbo/p/12112203.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!