gpt4 book ai didi

java - 为什么我的 java 代码在使用 SmbFileInputStream 和 BufferedOutputStream 时会进入无限循环

转载 作者:行者123 更新时间:2023-11-30 05:21:24 25 4
gpt4 key购买 nike

我有一个 java 代码,是由我之前的某个人编写的。该代码在 AIX 中运行良好,但我们最近不得不将所有脚本从 AIX 移至 RHEL 7.6。在 RHEL 上,代码似乎陷入了一个很长的循环,并且需要大约 5 分钟才能完成的 scipts 需要几个小时才能完成该循环。

循环使用SmbFileInputStream和BufferedOutputStream。日志没有显示任何错误/异常。

任何帮助将不胜感激,因为 AIX 服务器将在下周停用,我需要使其在 RHEL 上运行。

public File getSMBFile(SmbFile infile, String username, String password, String localdir) throws Exception {

SmbFileInputStream smbIn = new SmbFileInputStream(infile);
//File temp = File.createTempFile(infile.getName(), null);
//temp.deleteOnExit();

File localFile = new File(localdir, infile.getName());
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(localFile));

byte[] b = new byte[8192];
int n = 0;

while ((n = smbIn.read(b)) > 0) {
log.info("In while" + localFile + " to " + b + " " + 0 + " " + n);
out.write(b, 0, n);
}

out.flush();
out.close();

smbIn.close();
out.close();
log.info("LocalFile " + localFile);
return localFile;
}

此循环是代码进入无限循环并持续运行几个小时的地方:

while ((n = smbIn.read(b)) > 0) {
log.info("In while" + localFile + " to " + b + " " + 0 + " " + n);
out.write(b, 0, n);
}

日志输出:2019 年 12 月 30 日 10:16:18 PM com.perceptionit.configuration.inventory.processing.FileCollector getSMBFile信息:在 while/opt/dataloading/data/statisticalbss/4G_Updated_Region-Sheet-15-11-2019.xlsx 中至 [B@fc12260b 0 81922019 年 12 月 30 日 10:17:18 PM com.perceptionit.configuration.inventory.processing.FileCollector getSMBFile信息:在 while/opt/dataloading/data/statisticalbss/4G_Updated_Region-Sheet-15-11-2019.xlsx 中至 [B@fc12260b 0 81922019 年 12 月 30 日 10:18:18 PM com.perceptionit.configuration.inventory.processing.FileCollector getSMBFile信息:在 while/opt/dataloading/data/statisticalbss/4G_Updated_Region-Sheet-15-11-2019.xlsx 中至 [B@fc12260b 0 8192

最佳答案

该代码段中没有任何内容可以进入无限循环。在最坏的情况下,while 循环将一次读取一个字节并最终退出循环。

查看您的日志,情况显然并非如此。它读取 8192 字节(提供的字节数组的大小),但读取 8192 字节的时间相当长,一分钟:10:17:18 PM - 晚上 10:16:18

我在 Google 上搜索了 SmbFileInputStream wait too long on RHEL,这个答案显示在第一个: JCIFS: file retrieval is too slow to be usable

对于如何深入挖掘并解决这个问题有很多选择。

关于java - 为什么我的 java 代码在使用 SmbFileInputStream 和 BufferedOutputStream 时会进入无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59534510/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com