public void Save(Stream stream) { using (var bw = new BinaryWriter(stream, Encoding.UTF8, leaveOpen: true)) { // 1337 - signature that this is us bw.Write7BitEncodedInt(1337); bw.Write7BitEncodedInt(1); // version bw.Write7BitEncodedInt(_dictionary.Length); bw.Write(_dictionary); _packer.Save(bw); } }
public void Save(BinaryWriter writer) { symbols.Save(writer); writer.Write7BitEncodedInt(offsets.Length); foreach (var offset in offsets) { offset.Save(writer); } }
public static void Write(this BinaryWriter writer, ReadOnlySpan <char> chars) { if (writer == null) { throw new ArgumentNullException(nameof(writer)); } if (_writeCharsShim != null) { _writeCharsShim(writer, chars); return; } var encoding = TryGetEncoding(writer); if (encoding != null) { var array = ArrayPool <byte> .Shared.Rent(encoding.GetByteCount(chars)); try { var byteCount = encoding.GetBytes(chars, array); writer.Write7BitEncodedInt(byteCount); writer.Write(array, index: 0, byteCount); } finally { ArrayPool <byte> .Shared.Return(array); } } var str = new string('\0', chars.Length); chars.CopyTo(MemoryMarshal.AsMemory(str.AsMemory()).Span); writer.Write(str); }
/// <summary> /// Saves the license. /// </summary> public static void SaveLicense(string user, string key, byte[] license) { var licfile = Path.Combine(AppDataPath, ".license"); var xkey = ProtectedData.Protect(Encoding.UTF8.GetBytes(key), null, DataProtectionScope.LocalMachine); var lic = ProtectedData.Protect(license, null, DataProtectionScope.LocalMachine); using (var fs = File.Create(licfile)) using (var bw = new BinaryWriter(fs)) { bw.Write((byte)Utils.Rand.Next(0, 200)); bw.Write(user); bw.Write7BitEncodedInt(xkey.Length); bw.Write(xkey); bw.Write7BitEncodedInt(lic.Length); bw.Write(lic); } }
public void ReadCorruptedJournalFile() { string path = Path.GetFullPath("TestData\\ReadCorruptedJournal"); if (!Directory.Exists(path)) Directory.CreateDirectory(path); JournalWriter jw = new JournalWriter(path, 324, false); List<KeyValuePair<Key, Value>> items = new List<KeyValuePair<Key, Value>>(); for (int i = 0; i < 10; i++) { Key randKey = Key.Random(20); Value randValue = Value.Random(100); jw.Add(randKey, randValue); items.Add(new KeyValuePair<Key, Value>(randKey, randValue)); } jw.Close(); // Reopen the file and add a partial record var fileName = Config.JournalFile(path, 324); var writer = new BinaryWriter(new FileStream(fileName, FileMode.Append, FileAccess.Write, FileShare.None, 1024, false)); Key key = Key.Random(20); writer.Write7BitEncodedInt(key.Length); writer.Write(key.InternalBytes); writer.Write7BitEncodedInt(0); writer.Flush(); writer.Close(); JournalReader jr = new JournalReader(path, 324); int j = 0; foreach (var pair in jr.Enumerate()) { Assert.AreEqual(items[j].Key, pair.Key); Assert.AreEqual(items[j].Value, pair.Value); j++; } jr.Close(); }
public static void WriteStringUTF8(this BinaryWriter writer, string value) { byte[] utf8 = Encoding.UTF8.GetBytes(value); writer.Write7BitEncodedInt(utf8.Length); writer.Write(utf8, 0, utf8.Length); }
void WriteMetadata() { MemoryStream ms = new MemoryStream (); BinaryWriter writer = new BinaryWriter (ms); writer.Write (Encoding.ASCII.GetBytes ("@RAZORDB")); writer.Write7BitEncodedInt (totalBlocks + 1); writer.Write7BitEncodedInt (dataBlocks); writer.Write7BitEncodedInt (indexBlocks); byte[] metadata = ms.ToArray (); Array.Copy (metadata, _buffer, metadata.Length); // Commit the block to disk and wait for the operation to complete WriteBlock (); _fileStream.EndWrite (_async); }