// Copyright (c) Dr. Dirk Lellinger. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.IO;
using System.Text;
using SlobViewer.Common;
namespace SlobViewer.Slob
{
///
/// Part of a .
/// A holds the position where to find the contents in the file. After reading the SLOB file,
/// the content is stored in compressed form. If the contents is accessed, it is decompressed.
///
public class StoreItemFileBased : StoreItemBase
{
long _filePosition;
public StoreItemFileBased(long filePosition)
{
_filePosition = filePosition;
}
///
/// Initializes a new instance of the class.
///
/// The array of content IDs.
/// Compressed content as retrieved from the SLOB file.
public StoreItemFileBased(byte[] contentIds, byte[] compressedContent)
{
_contentIds = contentIds;
_compressedContent = compressedContent;
}
///
/// Get the content item with index .
///
/// The index.
/// A tuple, consisting of the content and content id at index .
public (string Content, int ContentId) GetAt(int idx, Stream stream, Encoding encoding, string compression, byte[] buffer)
{
ReadCompressedContentFromStream(stream, buffer);
// StoreCompressedContentForDebugging();
if (null != _compressedContent)
{
DecompressContent(encoding, compression, buffer);
_compressedContent = null;
}
return (_content[idx], _contentIds[idx]);
}
private void StoreCompressedContentForDebugging()
{
using (var s = new FileStream("C:\\TEMP\\CompressedContent.XXX", FileMode.Create, FileAccess.Write, FileShare.Read))
{
s.Write(_compressedContent, 0, _compressedContent.Length);
}
}
private void ReadCompressedContentFromStream(Stream stream, byte[] buffer)
{
stream.Seek(_filePosition, SeekOrigin.Begin);
stream.Read(buffer, 0, 4);
uint count = BigEndianBitConverter.ToUInt32(buffer, 0);
_contentIds = new byte[count];
for (int k = 0; k < count; ++k)
{
_contentIds[k] = (byte)stream.ReadByte();
}
stream.Read(buffer, 0, 4);
uint contentLen = BigEndianBitConverter.ToUInt32(buffer, 0);
_compressedContent = new byte[contentLen];
stream.Read(_compressedContent, 0, (int)contentLen);
}
}
}