Stream Copyto

Unity doesn’t have Stream’s CopyTo function? Really? Am I wrong or is that ridiculous?

Assets/Scripts/Utilities/Gzip.cs(22,49): error CS1061: Type System.IO.Compression.GZipStream' does not contain a definition for CopyTo’ and no extension method CopyTo' of type System.IO.Compression.GZipStream’ could be found (are you missing a using directive or an assembly reference?)

Assets/Scripts/Utilities/Gzip.cs(11,49): error CS1061: Type System.IO.MemoryStream' does not contain a definition for CopyTo’ and no extension method CopyTo' of type System.IO.MemoryStream’ could be found (are you missing a using directive or an assembly reference?)

int num;
byte[ ] buffer = new byte[4096];
while ((num = src.Read(buffer, 0, buffer.Length)) != 0)
dest.Write(buffer, 0, num);

CopyTo: For when the above code isn’t good enough.

If you have the library, just use the reference straight to that point to use the function. Otherwise code your own alternative.

We all have the library. If you notice, it’s under the System namespace. I think Unity uses something older than .Net 4.0. So disappointing!

You can’t just call a library from nothing, you have to include it with a include call, or using call etc. etc. otherwise if you know the root of it you can do something like.

system.input.bogus.CopyTo

to get the thing to run.

using System;
using System.IO;
using System.IO.Compression;
public static class Gzip {
public static byte[ ] Compress(byte[ ] bytes) {
using (MemoryStream iStream = new MemoryStream(bytes)) {
using (MemoryStream oStream = new MemoryStream()) {
using (GZipStream cStream = new GZipStream(oStream, CompressionMode.Compress)) {
iStream.CopyTo(cStream);

return oStream.GetBuffer();
}
}
}
}

public static byte[ ] Decompress(byte[ ] bytes) {
using (MemoryStream iStream = new MemoryStream(bytes)) {
using (MemoryStream oStream = new MemoryStream()) {
using (GZipStream dStream = new GZipStream(iStream, CompressionMode.Decompress)) {
dStream.CopyTo(oStream);

return oStream.GetBuffer();
}
}
}
}
}

I know.

If it were a static class, I would need to reference the namespace as you say. It’s not static. It’s a part of the stream, so it’s only important that the stream declaration includes the namespace.

If you notice, I have the ‘using’ declaration at the top. If that were the issue, then the original declaration wouldn’t work.

Also, I know this code works because I’m using the same class in my Visual C# project. The CopyTo declaration is missing in whatever .NET version Unity uses. :confused:

Unity’s version of Mono is not yet up to .NET 4.0.
If I recall Unity uses a modified version of Mono 2.6 which was not yet supporting .NET 4.0

I suppose I will just have to wait.