简体   繁体   中英

Value already read, or no value when trying to read from a Stream

I've been trying this for a long time but it keeps giving me an error. I have an array of bytes that should represent a nbt document. I would like to convert this into a c# object with a library: fNbt.

Here is my code:

byte[] buffer = Convert.FromBase64String(value);
byte[] decompressed;

using (var inputStream = new MemoryStream(buffer))
{
    using var outputStream = new MemoryStream();

    using (var gzip = new GZipStream(inputStream, CompressionMode.Decompress, leaveOpen: true))
    {
        gzip.CopyTo(outputStream);
    }

    fNbt.NbtReader reader = new fNbt.NbtReader(outputStream, true);
    var output = reader.ReadValueAs<AuctionItem>(); //Error: Value already read, or no value to read.
    return output;
}

When I try this, it works:

            decompressed = outputStream.ToArray();
            outputStream.Seek(0, SeekOrigin.Begin);
            outputStream.Read(new byte[1000], 0, decompressed.Count() - 1);

But when I try this, it doesn't:

            outputStream.Seek(0, SeekOrigin.Begin);
            fNbt.NbtReader reader = new fNbt.NbtReader(outputStream, true);
            reader.ReadValueAs<AuctionItem>();

NbtReader , like most stream readers, begins reading from the current position of whatever stream you give it. Since you're just done writing to outputStream , then that position is the stream's end. Which means at that point there's nothing to be read.

The solution is to seek the outputStream back to the beginning before reading from it:

outputStream.Seek(0, SeekOrigin.Begin); // <-- seek to the beginning    

// Do the read
fNbt.NbtReader reader = new fNbt.NbtReader(outputStream, true);
var output = reader.ReadValueAs<AuctionItem>(); // No error anymore
return output;

The solution is as follows. NbtReader.ReadValueAs does not consider a nbtCompound or nbtList as value. I made this little reader but it is not done yet (I will update the code once it is done).

    public static T ReadValueAs<T>(string value) where T: new()
    {
        byte[] buffer = Convert.FromBase64String(value);

        using (var inputStream = new MemoryStream(buffer))
        {
            using var outputStream = new MemoryStream();

            using (var gzip = new GZipStream(inputStream, CompressionMode.Decompress, leaveOpen: true))
            {
                gzip.CopyTo(outputStream);
            }
            outputStream.Seek(0, SeekOrigin.Begin);

            return new EasyNbt.NbtReader(outputStream).ReadValueAs<T>();
        }
    }

This is the NbtReader:

    private MemoryStream MemStream { get; set; }

    public NbtReader(MemoryStream memStream)
    {
        MemStream = memStream;
    }

    public T ReadValueAs<T>() where T: new()
    {
        return ReadTagAs<T>(new fNbt.NbtReader(MemStream, true).ReadAsTag());
    }

    private T ReadTagAs<T>(fNbt.NbtTag nbtTag)
    {
        //Reads to the root and adds to T...
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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