【发布时间】:2013-12-10 08:56:10
【问题描述】:
好吧,我刚刚发布了一个(半类似的)问题here,我问了这个问题,但我遇到了问题,这是一个 IO 问题。
在我们的程序中,我们必须读取一个文件(大约 10MB)并对其进行解密,我正在使用以下类。
class FileStream extends InputStream {
public int available() throws IOException {
return baseStream.available();
}
public boolean markSupported() {
return false;
}
InputStream baseStream;
byte[] data = new byte[1048576];
int loopRead = 0;
int streamSize;
int dataRead;
public FileStream(InputStream is) throws Exception {
this.baseStream = is;
streamSize = is.available();
}
public int read() {
if (dataRead == streamSize) {
return -1;/* End of the stream */
}
dataRead++;
if (loopRead == data.length) {
decrypt();
}
return data[loopRead++];
}
byte[] tempRead = new byte[4096];// block(smallest) decryption size
void decrypt() {
try {
int r;
for (int i = 0; i < (data.length / tempRead.length); i++) {
System.out.print("Data Available: "+baseStream.available());
r = baseStream.read(tempRead);// read from base stream, ERROR IS HERE!
System.out.print("Data read: "+r);//not always 4096 in jar file, vary numbers!
if (r <= 0) {
return;
}
System.arraycopy(tempRead, 0, data, (i * tempRead.length),
tempRead.length);
}// end-for
// /////decrypt the data////////
loopRead = 0;
} catch (Exception ex) {
}
}
}
它 WORKS 当我从 eclipse 运行它时,但是当我生成 JAR 文件(导出)并从命令行 (java -Xmx1280M -jar app.jar) 运行它时,它不会读取数据正确。
块大小(crypt block size)是4096,所以文件大小可以被4096整除,真不知道为什么会这样!它应该每次读取 4096 字节的数据,但它不会,并且数据可用性总是超过 4096(应该是)。
请帮忙,这里可能会出现什么错误或情况?!
提前致谢。
【问题讨论】:
-
运行时会发生什么?如果您不这样做,它可能会有所帮助;不仅仅是在解密方法中吞下异常。将 x.printStackTrace() 放入 catch 块中,重新构建,运行运行,看看您是否从中得到任何帮助
-
亲爱的也不例外,它只是不会在每次调用中读取
4096字节。 @DaveHowes -
这不是奇怪的行为。这正是定义的行为。您只是假设如果它在 Eclipse 中以这种方式工作,那么它在其他任何地方都会以相同的方式工作。