Example #1
0
        public void Save(ByteString id, byte[] bytes)
        {
            if (id.IsNull())
            {
                throw new ArgumentNullException(nameof(id));
            }

            if (id.Length != 32)
            {
                throw new ArgumentException(nameof(id));
            }

            if (bytes.IsNull())
            {
                throw new ArgumentException(nameof(bytes));
            }

            var fileName = Path.Combine(
                directory.FullName,
                HexConvert.FromBytes(id.ToByteArray()));
            var fileInfo = new FileInfo(fileName);

            // We assume the hash never collides.
            if (fileInfo.Exists)
            {
                return;
            }

            using (var stream = fileInfo.OpenWrite())
                stream.Write(bytes, 0, bytes.Length);
        }
Example #2
0
 public override object ReadJson(
     JsonReader reader, Type objectType, object existingValue,
     JsonSerializer serializer)
 {
     return(new ByteString(HexConvert.ToBytes(
                               serializer.Deserialize <string>(reader) ?? "")));
 }
Example #3
0
        public IEnumerable <(ByteString, byte[])> LoadAll()
        {
            var regex = new Regex("^[0-9a-fA-F]{64}$", RegexOptions.Singleline);

            foreach (var file in directory.GetFiles())
            {
                var name = file.Name;
                if (!regex.IsMatch(name))
                {
                    continue;
                }

                var id    = ByteString.CopyFrom(HexConvert.ToBytes(name));
                var bytes = File.ReadAllBytes(file.FullName);

                yield return(id, bytes);
            }
        }
Example #4
0
 public override string ToString() => HexConvert.FromBytes(bytes);
Example #5
0
 public override void WriteJson(
     JsonWriter writer, object value, JsonSerializer serializer)
 {
     writer.WriteValue(
         HexConvert.FromBytes(((ByteString)value).bytes));
 }