1
0
mirror of https://github.com/chylex/Brotli-Builder.git synced 2025-01-05 03:42:46 +01:00
Brotli-Builder/BrotliCalc/Helpers/BrotliFile.cs

59 lines
1.7 KiB
C#

using System;
using System.Diagnostics;
using System.IO;
using BrotliLib.Brotli;
namespace BrotliCalc.Helpers{
abstract class BrotliFile{
public string Path { get; }
public string Name { get; }
public abstract string FullName { get; }
public byte[] Contents => contentsLazy.Value;
public int? SizeBytes => sizeBytesLazy.Value;
private readonly Lazy<byte[]> contentsLazy;
private readonly Lazy<int?> sizeBytesLazy;
protected BrotliFile(string path, string name){
this.Path = path;
this.Name = name;
this.contentsLazy = new Lazy<byte[]>(() => File.ReadAllBytes(Path), isThreadSafe: true);
this.sizeBytesLazy = new Lazy<int?>(() => {
try{
return (int)new FileInfo(Path).Length;
}catch(Exception ex){
Debug.WriteLine(ex);
return null;
}
}, isThreadSafe: false);
}
public override string ToString(){
return FullName;
}
// Types
internal class Uncompressed : BrotliFile{
public override string FullName => Name;
public Uncompressed(string path, string name) : base(path, name){}
}
internal class Compressed : BrotliFile{
public string Identifier { get; }
public BrotliFileStructure Structure => BrotliFileStructure.FromBytes(Contents);
public override string FullName => $"{Name}.{Identifier}{Brotli.CompressedFileExtension}";
public Compressed(string path, string name, string identifier) : base(path, name){
this.Identifier = identifier;
}
}
}
}