简体   繁体   English

不同的结果 ByteArrayOutputStream Java 和 C#

[英]Different results ByteArrayOutputStream Java and C#

UPD: For answers about encoding: Am use UTF-8 everywhere UPD:有关编码的答案:我到处都使用 UTF-8

I'am try use ByteArrayOutputStream on C#.我尝试在 C# 上使用 ByteArrayOutputStream。

I have a string of data:我有一串数据:

ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567896ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567896 ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567896ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567896

For java i use the method:对于 java,我使用以下方法:

String a = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567896ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567896";
byte[] bArr = a.getBytes();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(bArr.length);
DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream);
deflaterOutputStream.write(bArr);
deflaterOutputStream.close();
bArr = byteArrayOutputStream.toByteArray();
System.out.println("out: "+Base64.getEncoder().encodeToString(bArr));

So, for as a result i get the string:所以,结果我得到了字符串:

eJxzdHJ2cXVz9/D08vbx9fMPCAwKDgkNC4+IjEpMSk5JTUvPyMzKzsnNyy8oLCouKS0rr6isMjA0MjYxNTO3sDRzpEw7AHuTKoM= eJxzdHJ2cXVz9/D08vbx9fMPCAwKDgkNC4+IjEpMSk5JTUvPyMzKzsnNyy8oLCouKS0rr6isMjA0MjYxNTO3sDRzpEw7AHuTKoM=

For C# am use next code:对于 C#,我使用下一个代码:

string source = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567896ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567896";
byte[] bArr_source = Encoding.UTF8.GetBytes(source);
MemoryStream byteArrayOutputStream = new MemoryStream(bArr_source.Length);
DeflaterOutputStream dos = new DeflaterOutputStream(byteArrayOutputStream);
dos.Write(bArr_source, 0, bArr_source.Length);
dos.Finish();
dos.Close();
bArr_source = byteArrayOutputStream.ToArray();
Console.WriteLine(Convert.ToBase64String(bArr_source));

But in C# iam get a different line:但是在 C# 中,我得到了不同的一行:

eJyly8MBgAAAAMCVMp7Zdr9su+kbovsfRTMsxwuiJCuqphumZTuu5wdhFCdplhdlVTdt1w/jNC/rth/ndT8vAEIwgmI4QWI/+wd7kyqD eJyly8MBgAAAAMCVMp7Zdr9su+kbovsfRTMsxwuiJCuqphumZTuu5wdhFCdplhdlVTdt1w/jNC/rth/ndT8vAEIwgmI4QWI/+wd7kyqD

Why is this happening and how can I fix the problem?为什么会发生这种情况,我该如何解决这个问题?

There is no problem .没有问题

Both results are valid zlib streams with a check value that checks out, and both decompress to the exact same thing.两个结果都是有效的 zlib 流,带有一个检查值,并且都解压缩到完全相同的东西。

There is no assurance that two different compressors will produce the same output with the same input.无法保证两个不同的压缩器会在相同的输入下产生相同的 output。 The only assurance is that when you decompress , you get the same thing you started with.唯一的保证是当你解压缩时,你得到的是你开始时得到的东西。 And you are.而你是。

I do find it a little odd that the C# code is producing a dynamic block when the result can be four bytes shorter with a fixed block.我确实觉得 C# 代码生成动态块有点奇怪,而结果可以用固定块短四个字节。 I suspect that you are using a much older version of .NET that used a compressor that was poorly written (internally at Microsoft).我怀疑您使用的是 .NET 的更旧版本,它使用了编写不当的压缩器(在 Microsoft 内部)。 It now uses zlib, and wouldn't make that mistake.它现在使用 zlib,不会犯那个错误。 Yet I emphasize, the result is still entirely valid and usable.但我强调,结果仍然是完全有效和可用的。

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

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