fNbt.NbtFile.SaveToStream C# (CSharp) Method

SaveToStream() public method

Saves this NBT file to a stream. Nothing is written to stream if RootTag is null.
is null. If AutoDetect was given as the mode. If an unrecognized/unsupported value was given for . If given stream does not support writing. If RootTag is null; /// or if RootTag is unnamed; /// or if one of the NbtCompound tags contained unnamed tags; /// or if an NbtList tag had Unknown list type and no elements.
public SaveToStream ( [ stream, NbtCompression compression ) : long
stream [ Stream to write data to. May not be null.
compression NbtCompression Compression mode to use for saving. May not be AutoDetect.
return long
        public long SaveToStream([NotNull] Stream stream, NbtCompression compression)
        {
            if (stream == null) throw new ArgumentNullException("stream");

            switch (compression) {
                case NbtCompression.AutoDetect:
                    throw new ArgumentException("AutoDetect is not a valid NbtCompression value for saving.");
                case NbtCompression.ZLib:
                case NbtCompression.GZip:
                case NbtCompression.None:
                    break;
                default:
                    throw new ArgumentOutOfRangeException("compression");
            }

            if (rootTag.Name == null) {
                // This may trigger if root tag has been renamed
                throw new NbtFormatException(
                    "Cannot save NbtFile: Root tag is not named. Its name may be an empty string, but not null.");
            }

            long startOffset = 0;
            if (stream.CanSeek) {
                startOffset = stream.Position;
            } else {
                stream = new ByteCountingStream(stream);
            }

            switch (compression) {
                case NbtCompression.ZLib:
                    stream.WriteByte(0x78);
                    stream.WriteByte(0x01);
                    int checksum;
                    using (var compressStream = new ZLibStream(stream, CompressionMode.Compress, true)) {
                        var bufferedStream = new BufferedStream(compressStream, WriteBufferSize);
                        RootTag.WriteTag(new NbtBinaryWriter(bufferedStream, BigEndian));
                        bufferedStream.Flush();
                        checksum = compressStream.Checksum;
                    }
                    byte[] checksumBytes = BitConverter.GetBytes(checksum);
                    if (BitConverter.IsLittleEndian) {
                        // Adler32 checksum is big-endian
                        Array.Reverse(checksumBytes);
                    }
                    stream.Write(checksumBytes, 0, checksumBytes.Length);
                    break;

                case NbtCompression.GZip:
                    using (var compressStream = new GZipStream(stream, CompressionMode.Compress, true)) {
                        // use a buffered stream to avoid GZipping in small increments (which has a lot of overhead)
                        var bufferedStream = new BufferedStream(compressStream, WriteBufferSize);
                        RootTag.WriteTag(new NbtBinaryWriter(bufferedStream, BigEndian));
                        bufferedStream.Flush();
                    }
                    break;

                case NbtCompression.None:
                    var writer = new NbtBinaryWriter(stream, BigEndian);
                    RootTag.WriteTag(writer);
                    break;

                default:
                    throw new ArgumentOutOfRangeException("compression");
            }

            if (stream.CanSeek) {
                return stream.Position - startOffset;
            } else {
                return ((ByteCountingStream)stream).BytesWritten;
            }
        }

Usage Example

Ejemplo n.º 1
0
 public void Export(string path, NbtTag root)
 {
     if (Snbt)
     {
         File.WriteAllText(path, root.ToSnbt(CreateOptions()));
     }
     else
     {
         var file = new fNbt.NbtFile();
         file.BigEndian = BigEndian;
         file.RootTag   = root;
         using (var writer = File.Create(path))
         {
             if (BedrockHeader)
             {
                 writer.Seek(8, SeekOrigin.Begin);
             }
             long size = file.SaveToStream(writer, Compression);
             if (BedrockHeader)
             {
                 // bedrock level.dat files start with a header containing a magic number and then the little-endian size of the data
                 writer.Seek(0, SeekOrigin.Begin);
                 writer.Write(new byte[] { 8, 0, 0, 0 }, 0, 4);
                 writer.Write(DataUtils.GetBytes((int)size, little_endian: !BigEndian), 0, 4);
             }
         }
     }
 }
All Usage Examples Of fNbt.NbtFile::SaveToStream