I have 2 deflate functions written in C# and Scala, when running with the same input, the returned byte array has a difference in leading bytes and trailing bytes (the difference between the bytes in the middle is expected by the unsigned/signed bytes mechanism between C# and Scala).
Deflate function in Scala:
import java.io.ByteArrayOutputStream
import java.util.zip.{Deflater, DeflaterOutputStream}
import zio._
object ZDeflater {
val deflater = ZManaged.makeEffectTotal(new Deflater(Deflater.DEFLATED, true))(_.end)
val buffer = ZManaged.fromAutoCloseable(ZIO.succeed(new ByteArrayOutputStream()))
val stream = for {
d <- deflater
b <- buffer
s <- ZManaged.fromAutoCloseable(ZIO.succeed(new DeflaterOutputStream(b, d, true)))
} yield (b, s)
def deflate(input: Array[Byte]): RIO[blocking.Blocking, Array[Byte]] = stream.use { case (buffer, stream) =>
for {
() <- blocking.effectBlocking(stream.write(input))
() <- blocking.effectBlocking(stream.flush())
result = buffer.toByteArray
} yield result
}
}
Deflate function in C#:
private static byte[] Deflate(byte[] uncompressedBytes)
{
using (var output = new MemoryStream())
{
using (var zip = new DeflateStream(output, CompressionMode.Compress, true))
{
zip.Write(uncompressedBytes, 0, uncompressedBytes.Length);
}
return output.ToArray();
}
}
Outputs after deflating: Scala:
ZDeflater.deflate(data.getBytes(StandardCharsets.UTF_8))
124, -111, …, 126, 1, 0, 0, -1, -1
C#:
Deflate(Encoding.UTF8.GetBytes(data))
125, 145, …, 126, 1
Does anyone know what causes the difference between the first and last bytes? Any of your assumptions are very helpful to me. Thank a bunch
P/s: We're having a problem with a situation where C#'s Deflate output works for a specific 3rd part and Scala's output doesn't. So I'm trying to figure out how to make Scala's output to be the same as C#'s