static NbtCompression DetectCompression([NotNull] Stream stream)
{
NbtCompression compression;
if (!stream.CanSeek) {
throw new NotSupportedException("Cannot auto-detect compression on a stream that's not seekable.");
}
int firstByte = stream.ReadByte();
switch (firstByte) {
case -1:
throw new EndOfStreamException();
case (byte)NbtTagType.Compound: // 0x0A
compression = NbtCompression.None;
break;
case 0x1F:
// GZip magic number
compression = NbtCompression.GZip;
break;
case 0x78:
// ZLib header
compression = NbtCompression.ZLib;
break;
default:
throw new InvalidDataException("Could not auto-detect compression format.");
}
stream.Seek(-1, SeekOrigin.Current);
return compression;
}