- Home /
Question by
programmrzinc · Feb 17, 2017 at 03:20 PM ·
arraydictionarybinarybinarywriter
'Too many bytes in what should have been a 7 bit encoded Int32' and idk what I'm doing wrong...
What am I doing wrong? everything is as it should. I'm basically reading and writing a dictionary to the filesystem, and populating some RawImages with the values of the dictionary. Can someone shed some light?
static void Serialize(string path, Dictionary<string, byte[]> data)
{
using (var file = File.Create(path))
using (var deflate = new DeflateStream(file, CompressionMode.Compress))
using (var writer = new BinaryWriter(deflate))
{
writer.Write(data.Count);
foreach(var pair in data)
{
writer.Write(pair.Key);
writer.Write(pair.Value);
}
writer.Close();
}
}
static Dictionary<string,byte[]> Deserialize(string path)
{
using (var file = File.OpenRead(path))
using (var deflate = new DeflateStream(file, CompressionMode.Decompress))
using (var reader = new BinaryReader(deflate))
{
int count = reader.ReadInt32();
var data = new Dictionary<string, byte[]>();
while(count-->0) {
data.Add(reader.ReadString(), reader.ReadBytes(count));
}
reader.Close();
return data;
}
}
Comment
Best Answer
Answer by anathemort · Feb 17, 2017 at 05:33 PM
You need to record the length of the pair.Value
byte[] so you know how much data to read back out. So in your Serialize
method, you would change your loop to be something like this:
foreach(var pair in data)
{
writer.Write(pair.Key);
writer.Write(pair.Value.Length);
writer.Write(pair.Value);
}
Then in your Deserialize
, when you read the data:
int count = reader.ReadInt32();
var dict = new Dictionary<string, byte[]>(count); //initialize capacity, too
for(int i = 0; i < count; i++)
{
var key = reader.ReadString();
var len = reader.ReadInt32();
dict.Add(key, reader.ReadBytes(len));
}
I did what you said, assigning a length before I read it. Works smoothly. $$anonymous$$uch love lol
static void Serialize(string path, Dictionary<string, byte[]> data)
{
using (var file = File.Create(path))
using (var deflate = new DeflateStream(file, Compression$$anonymous$$ode.Compress))
using (var writer = new BinaryWriter(deflate))
{
writer.Write(data.Count);
foreach(var pair in data)
{
writer.Write(pair.$$anonymous$$ey.Length);
writer.Write(Encoding.ASCII.GetBytes(pair.$$anonymous$$ey));
writer.Write(pair.Value.Length);
writer.Write(pair.Value);
}
writer.Close();
}
}
static void Deserialize(string path, Dictionary<string, byte[]> data)
{
using (var file = File.OpenRead(path))
using (var deflate = new DeflateStream(file, Compression$$anonymous$$ode.Decompress))
using (var reader = new BinaryReader(deflate))
{
int count = reader.ReadInt32();
string url = "";
byte[] image;
while(count-->0) {
int urlLength = reader.ReadInt32();
url = ASCIIEncoding.ASCII.GetString(reader.ReadBytes(urlLength));
int dataLength = reader.ReadInt32();
image = reader.ReadBytes(dataLength);
if(!data.Contains$$anonymous$$ey(url))
data.Add(url, image);
}
reader.Close();
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612111324im_/https://answers.unity.com/themes/thub/images/avi.jpg)