Some fixes to improve compability with dot net 6

Will probably remove gzip some more placeces
This commit is contained in:
niksedk 2021-11-26 18:53:15 +01:00
parent eb4dd7f27d
commit 916f139df0
5 changed files with 60 additions and 12 deletions

View File

@ -50,6 +50,35 @@ namespace Nikse.SubtitleEdit.Core.Common
}
}
public static byte[] ReadBytesShared(string path, int bytesToRead)
{
using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
var index = 0;
var fileLength = fs.Length;
if (fileLength > int.MaxValue)
{
throw new IOException("File too long");
}
var count = Math.Min(bytesToRead, (int)fileLength);
var bytes = new byte[count];
while (count > 0)
{
var n = fs.Read(bytes, index, count);
if (n == 0)
{
throw new InvalidOperationException("End of file reached before expected");
}
index += n;
count -= n;
}
return bytes;
}
}
public static List<string> ReadAllLinesShared(string path, Encoding encoding)
{
return encoding.GetString(ReadAllBytesShared(path)).SplitToLines();
@ -77,6 +106,7 @@ namespace Nikse.SubtitleEdit.Core.Common
&& buffer[3] == 0x04; // (EOT)
}
}
public static bool Is7Zip(string fileName)
{
using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))

View File

@ -919,7 +919,6 @@ namespace Nikse.SubtitleEdit.Controls
var suggestThread = new System.Threading.Thread(DoWork);
suggestThread.Start(parameter);
suggestThread.Join(3000); // wait max 3 seconds
suggestThread.Abort();
if (!parameter.Success)
{
LoadHunspell(_currentDictionary);

View File

@ -286,7 +286,6 @@ namespace Nikse.SubtitleEdit.Forms
var suggestThread = new System.Threading.Thread(DoWork);
suggestThread.Start(parameter);
suggestThread.Join(3000); // wait max 3 seconds
suggestThread.Abort();
if (!parameter.Success)
{
LoadHunspell(_currentDictionary);

View File

@ -33,19 +33,24 @@ namespace Nikse.SubtitleEdit.Logic.Ocr
public void Save()
{
using (Stream gz = new GZipStream(File.OpenWrite(FileName), CompressionMode.Compress))
if (File.Exists(FileName))
{
File.Delete(FileName);
}
using (Stream stream = new FileStream(FileName, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None))
{
var versionBuffer = Encoding.ASCII.GetBytes(Version);
gz.Write(versionBuffer, 0, versionBuffer.Length);
stream.Write(versionBuffer, 0, versionBuffer.Length);
foreach (var ocrChar in OcrCharacters)
{
ocrChar.Save(gz);
ocrChar.Save(stream);
}
foreach (var ocrChar in OcrCharactersExpanded)
{
ocrChar.Save(gz);
ocrChar.Save(stream);
}
}
}
@ -63,24 +68,24 @@ namespace Nikse.SubtitleEdit.Logic.Ocr
}
bool isVersion2;
using (Stream gz = new GZipStream(File.OpenRead(FileName), CompressionMode.Decompress))
using (Stream stream = GetOpenStream())
{
var versionBuffer = new byte[Version.Length];
gz.Read(versionBuffer, 0, versionBuffer.Length);
stream.Read(versionBuffer, 0, versionBuffer.Length);
isVersion2 = Encoding.ASCII.GetString(versionBuffer) == Version;
}
using (Stream gz = new GZipStream(File.OpenRead(FileName), CompressionMode.Decompress))
using (Stream stream = GetOpenStream())
{
bool done = false;
if (isVersion2)
{
var versionBuffer = new byte[Version.Length];
gz.Read(versionBuffer, 0, versionBuffer.Length);
stream.Read(versionBuffer, 0, versionBuffer.Length);
}
while (!done)
{
var ocrChar = new NOcrChar(gz, isVersion2);
var ocrChar = new NOcrChar(stream, isVersion2);
if (ocrChar.LoadedOk)
{
if (ocrChar.ExpandCount > 0)
@ -98,10 +103,25 @@ namespace Nikse.SubtitleEdit.Logic.Ocr
}
}
}
OcrCharacters = list;
OcrCharactersExpanded = listExpanded;
}
private Stream GetOpenStream()
{
var bytes = FileUtil.ReadBytesShared(FileName, 2);
var isVersion2Plain = Encoding.ASCII.GetString(bytes) == Version;
if (isVersion2Plain)
{
return new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
}
else
{
return new GZipStream(File.OpenRead(FileName), CompressionMode.Decompress);
}
}
public void Add(NOcrChar ocrChar)
{
if (ocrChar.ExpandCount > 0)

View File

@ -646,7 +646,7 @@ namespace Nikse.SubtitleEdit.Logic.Ocr
return word;
}
internal static string FixFrenchLApostrophe(string input, string tag, string lastLine)
public static string FixFrenchLApostrophe(string input, string tag, string lastLine)
{
var text = input;
bool endingBeforeThis = string.IsNullOrEmpty(lastLine) || lastLine.EndsWith('.') || lastLine.EndsWith('!') || lastLine.EndsWith('?') ||