Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ByteArrayOutputStream.toByteArray()返回随机字节?_Java_Zip_Bytearray_Java Io - Fatal编程技术网

Java ByteArrayOutputStream.toByteArray()返回随机字节?

Java ByteArrayOutputStream.toByteArray()返回随机字节?,java,zip,bytearray,java-io,Java,Zip,Bytearray,Java Io,我正在尝试为我重构的方法编写Junit测试,以确保该方法仍按预期运行。我注意到一些我无法理解的奇怪行为 为什么Java ByteArrayOutputStream.toByteArray()返回一些随机字节?我认为这可能与内存有关。数组中的11字节总是随机的。有什么见解吗 这是我重构的方法。 package foo; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.Map; i

我正在尝试为我重构的方法编写Junit测试,以确保该方法仍按预期运行。我注意到一些我无法理解的奇怪行为

为什么Java ByteArrayOutputStream.toByteArray()返回一些随机字节?我认为这可能与内存有关。数组中的11字节总是随机的。有什么见解吗

这是我重构的方法。

package foo;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipUtil {

    public static byte[] createZip(Map<String, byte[]> files) throws IOException {
        final ByteArrayOutputStream bos = new ByteArrayOutputStream();
        final ZipOutputStream zipfile = new ZipOutputStream(bos);
        String fileName = null;
        ZipEntry zipentry = null;
        for (Map.Entry<String, byte[]> entry : files.entrySet()) {
            fileName = entry.getKey();
            zipentry = new ZipEntry(fileName);
            zipfile.putNextEntry(zipentry);
            zipfile.write(files.get(fileName));
        }

        zipfile.close();  
        return bos.toByteArray();  
    }  
}
package-foo;
导入java.io.ByteArrayOutputStream;
导入java.io.IOException;
导入java.util.Map;
导入java.util.zip.ZipEntry;
导入java.util.zip.ZipoutStream;
公共类ZipUtil{
公共静态字节[]createZip(映射文件)引发IOException{
final ByteArrayOutputStream bos=新建ByteArrayOutputStream();
最终ZipoutStream zipfile=新ZipoutStream(bos);
字符串文件名=null;
ZipEntry ZipEntry=null;
对于(Map.Entry:files.entrySet()){
fileName=entry.getKey();
zipentry=新zipentry(文件名);
zipfile.putNextEntry(Zippentry);
write(files.get(fileName));
}
zipfile.close();
返回bos.toByteArray();
}  
}
测试类

package foo;

import java.util.HashMap;
import java.util.Map;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest({})
public class ZipUtilTest {

    @Test
    public void test_createZip() throws Exception {

        // Setup
        Map<String, byte[]> files = new HashMap<String, byte[]>();

        byte[] byteArr = new byte[] { 49, 17, 23,
                -29, -126, 111, -72, -112, 48, 32, 91, -28, -14, 112 };

        files.put("foo.txt", byteArr);

        // Test
        byte[] result = ZipUtil.createZip(files);

        // Validations
        byte[] expectedByteArray1 = new byte[] { 80, 75, 3, 4, 20, 0, 8, 8, 8, 0, 46, -120, -11, 72, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7,
                0, 0, 0, 102, 111, 111, 46, 116, 120, 116, 51, 20, 20, 127, -36, -108, -65, 99, -126, -127, 66, -12, -109, 79, 5, 0, 80,
                75, 7, 8, -99, 100, -122, -62, 16, 0, 0, 0, 14, 0, 0, 0, 80, 75, 1, 2, 20, 0, 20, 0, 8, 8, 8, 0, 46, -120, -11, 72, -99,
                100, -122, -62, 16, 0, 0, 0, 14, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 102, 111, 111, 46, 116,
                120, 116, 80, 75, 5, 6, 0, 0, 0, 0, 1, 0, 1, 0, 53, 0, 0, 0, 69, 0, 0, 0, 0, 0 };

        Assert.assertNotNull(result);
        Assert.assertEquals(144, result.length);

        // a few bytes are "random". So test the first 9 never random bytes
        for (int i = 0; i < 10; i++) {
            Assert.assertEquals(expectedByteArray1[i], result[i]);
        }

        // This fails
        // Assert.assertEquals(expectedByteArray1[10], result[10]);

        for (int i = 11; i < 70; i++) {
            Assert.assertEquals(expectedByteArray1[i], result[i]);
        }
    }
}
package-foo;
导入java.util.HashMap;
导入java.util.Map;
导入org.junit.Assert;
导入org.junit.Test;
导入org.junit.runner.RunWith;
导入org.powermock.core.classloader.annotations.PrepareForTest;
导入org.powermock.modules.junit4.PowerMockRunner;
@RunWith(PowerMockRunner.class)
@PrepareForTest({})
公共类ZipUtilTest{
@试验
public void test_createZip()引发异常{
//设置
映射文件=新的HashMap();
byte[]byteArr=新字节[]{49,17,23,
-29, -126, 111, -72, -112, 48, 32, 91, -28, -14, 112 };
file.put(“foo.txt”,byteArr);
//试验
byte[]result=ZipUtil.createZip(文件);
//验证
字节[]expectedByteArray1=新字节[]{80,75,3,4,20,0,8,8,8,0,46,-120,-11,72,0,0,0,0,0,0,0,0,0,0,7,
0, 0, 0, 102, 111, 111, 46, 116, 120, 116, 51, 20, 20, 127, -36, -108, -65, 99, -126, -127, 66, -12, -109, 79, 5, 0, 80,
75, 7, 8, -99, 100, -122, -62, 16, 0, 0, 0, 14, 0, 0, 0, 80, 75, 1, 2, 20, 0, 20, 0, 8, 8, 8, 0, 46, -120, -11, 72, -99,
100, -122, -62, 16, 0, 0, 0, 14, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 102, 111, 111, 46, 116,
120, 116, 80, 75, 5, 6, 0, 0, 0, 0, 1, 0, 1, 0, 53, 0, 0, 0, 69, 0, 0, 0, 0, 0 };
Assert.assertNotNull(结果);
Assert.assertEquals(144,result.length);
//有几个字节是“随机的”。所以测试前9个从不随机的字节
对于(int i=0;i<10;i++){
Assert.assertEquals(预期为tearray1[i],结果[i]);
}
//这失败了
//Assert.assertEquals(预期为tearray1[10],结果[10]);
对于(int i=11;i<70;i++){
Assert.assertEquals(预期为tearray1[i],结果[i]);
}
}
}

您的测试用例假设使用相同输入创建的连续zip文件将产生完全相同的输出字节。事实似乎并非如此——zip文件规范存储文件修改日期和时间,从字节10(小尾端)开始。这就是为什么字节从该位置开始是不同的


也就是说,我不认为将zip文件中的字节与已知的zip文件进行比较是一种非常有效的单元测试。更有效的测试是“往返”zip文件-从新创建的存档中提取压缩文件,并与已知的输入文件进行比较,以确保它们匹配。

您的测试用例假设使用相同的输入创建的连续zip文件将产生完全相同的输出字节。事实似乎并非如此——zip文件规范存储文件修改日期和时间,从字节10(小尾端)开始。这就是为什么字节从该位置开始是不同的


也就是说,我不认为将zip文件中的字节与已知的zip文件进行比较是一种非常有效的单元测试。更有效的测试是“往返”zip文件-从新创建的存档中提取压缩文件,并与已知的输入文件进行比较,以确保它们匹配。

zipfile.write(entry.getValue())
zipfile.write(files.get(fileName))更好
。为什么要在循环之外声明
fileName
zipEntry
zipfile.write(entry.getValue())
要比
zipfile.write(files.get(fileName))
好。为什么要在循环之外声明
fileName
zipEntry
呢?很好。字节10-11是上次修改文件的时间。关于我的单元测试,我知道只有一个单元测试让我困惑,这就是为什么我问了关于ZIP文件byte array.Nice的问题。字节10-11是上次修改文件的时间。关于我的单元测试,我知道只有一个单元测试让我困惑,这就是为什么我问了关于ZIP文件字节数组的问题。