1
0
mirror of https://github.com/chylex/Brotli-Builder.git synced 2025-05-13 17:34:05 +02:00

Update validate-compression cmd to use streaming API & distinguish mismatch and exception

This commit is contained in:
chylex 2020-03-31 12:02:46 +02:00
parent fba5c43d39
commit b4e55f2f54
2 changed files with 18 additions and 7 deletions

View File

@ -2,6 +2,7 @@
using System.Collections.Generic;
using BrotliCalc.Commands.Base;
using BrotliCalc.Helpers;
using BrotliLib.Brotli.Output;
using BrotliLib.Collections;
namespace BrotliCalc.Commands{
@ -12,26 +13,36 @@ namespace BrotliCalc.Commands{
protected override string WorkDesc => "Validated";
protected override string[] Columns { get; } = {
"File", "Quality", "Matches"
"File", "Quality", "Result"
};
protected override IEnumerable<object?[]> GenerateRows(BrotliFileGroup group, BrotliFile.Compressed file){
var original = group.Uncompressed.Contents;
var decompressed = file.Structure.Decompress().AsBytes;
if (!CollectionHelper.Equal(decompressed, original)){
throw new InvalidOperationException("Mismatched output bytes.");
var reader = file.Reader;
var output = new BrotliOutputStored();
reader.AddOutputCallback(output);
while(reader.NextMetaBlock() != null){}
reader.RemoveOutputCallback(output);
if (!CollectionHelper.Equal(output.AsBytes, original)){
throw new MismatchedOutputBytesException();
}
return new List<object?[]>{
new object?[]{ file.Name, file.Identifier, 1 }
new object?[]{ file.Name, file.Identifier, "OK" }
};
}
protected override IEnumerable<object?[]> OnError(BrotliFileGroup group, BrotliFile.Compressed file, Exception ex){
return new List<object?[]>{
new object?[]{ file.Name, file.Identifier, 0 }
new object?[]{ file.Name, file.Identifier, ex is MismatchedOutputBytesException ? "Mismatch" : "Error" }
};
}
private sealed class MismatchedOutputBytesException : Exception{
public MismatchedOutputBytesException() : base("Mismatched output bytes."){}
}
}
}

View File

@ -61,7 +61,7 @@ namespace BrotliCalc.Helpers{
public override string FullName => $"{Name}.{Identifier}{Brotli.CompressedFileExtension}";
public BrotliFileStructure Structure => structureLazy.Value;
public IBrotliFileStream Reader => BrotliFileReader.FromBytes(Contents, MarkerLevel.None);
public BrotliFileReader Reader => BrotliFileReader.FromBytes(Contents, MarkerLevel.None);
private readonly Lazy<BrotliFileStructure> structureLazy;