PropertyStringTableHeader ReadHeader() { using (LockRead()) { m_Fs.Seek(0, SeekOrigin.Begin); TransactionUtils.ReadWholeArray(m_Fs, m_HeaderBuffer); return(TransactionUtils.Deserialize <PropertyStringTableHeader>(m_HeaderBuffer)); } }
void WriteHeader(bool notify) { using (LockWrite()) { m_Fs.Seek(0, SeekOrigin.Begin); TransactionUtils.SerializeInto(m_Header, m_HeaderBuffer, 0); m_Fs.Write(m_HeaderBuffer, 0, m_HeaderBuffer.Length); if (notify && !m_DelayedSync) { Sync(); } } }
public string GetString(int symbol) { if (symbol == PropertyStringTable.EmptyStringSymbol) { return(string.Empty); } if (symbol == PropertyStringTable.StringTableFullSymbol) { return(null); } using (LockRead()) { if (symbol >= m_Header.usedStringBytes) { return(null); } var byteOffset = GetStringsOffset() + symbol; if (byteOffset > m_Fs.Length - PropertyStringTable.StringLengthByteSize) { return(null); } m_Fs.Seek(byteOffset, SeekOrigin.Begin); var strLength = ReadInt32(); if (strLength == 0) { return(string.Empty); } if (strLength < 0) { return(null); } if (byteOffset + PropertyStringTable.StringLengthByteSize + strLength > m_Fs.Length) { return(null); } if (m_StringBuffer.Length < strLength) { m_StringBuffer = new byte[strLength]; } TransactionUtils.ReadIntoArray(m_Fs, m_StringBuffer, strLength); return(PropertyStringTable.encoding.GetString(m_StringBuffer, 0, strLength)); } }
void Create(int stringCount, int averageStringLength = DefaultAverageStringSize) { var header = new PropertyStringTableHeader(); header.version = Version; header.count = 0; header.symbolSlots = Math.Max(stringCount * HashFactor, 1); var bytesPerString = GetStringByteSize(averageStringLength); header.allocatedStringBytes = stringCount * bytesPerString; header.usedStringBytes = StringLengthByteSize; var buffer = new byte[Marshal.SizeOf <PropertyStringTableHeader>() + GetSymbolsByteSize(header.symbolSlots) + header.allocatedStringBytes]; TransactionUtils.SerializeInto(header, buffer, 0); using (var fs = File.Open(filePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite | FileShare.Delete)) fs.Write(buffer, 0, buffer.Length); }
public IList <string> GetAllStrings() { using (LockRead()) { var strings = new List <string>(m_Header.count); // Skip the first length for empty string (symbol 0) var startOffset = GetStringsOffset() + PropertyStringTable.StringLengthByteSize; m_Fs.Seek(startOffset, SeekOrigin.Begin); for (var i = 0; i < m_Header.count; ++i) { var strLength = ReadInt32(); if (strLength == 0) { strings.Add(string.Empty); } if (strLength < 0) { break; } if (m_Fs.Position + PropertyStringTable.StringLengthByteSize + strLength > m_Fs.Length) { break; } if (m_StringBuffer.Length < strLength) { m_StringBuffer = new byte[strLength]; } TransactionUtils.ReadIntoArray(m_Fs, m_StringBuffer, strLength); var str = PropertyStringTable.encoding.GetString(m_StringBuffer, 0, strLength); strings.Add(str); } return(strings); } }
void WriteInt32(int value) { TransactionUtils.SerializeInto(value, m_SymbolBuffer, 0); m_Fs.Write(m_SymbolBuffer, 0, m_SymbolBuffer.Length); }
int ReadInt32() { TransactionUtils.ReadWholeArray(m_Fs, m_SymbolBuffer); return(TransactionUtils.Deserialize <int>(m_SymbolBuffer)); }