Skip to content

Commit c0d604c

Browse files
committed
Added try..catch blocks
1 parent 40339ef commit c0d604c

File tree

4 files changed

+78
-68
lines changed

4 files changed

+78
-68
lines changed

App/EncodingChecker.exe

0 Bytes
Binary file not shown.

App/EncodingUtils.dll

0 Bytes
Binary file not shown.

sources/EncodingChecker/EncodingUtils/TextEncoding.cs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,23 +65,30 @@ public static Encoding GetFileEncoding(string filePath, ref bool hasBOM)
6565
/// <returns>System.Text.Encoding (can be null if not available or not supported by .NET).</returns>
6666
public static Encoding GetFileEncoding(string filePath, int? maxBytesToRead, ref bool hasBOM)
6767
{
68-
using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
68+
hasBOM = false;
69+
try
6970
{
70-
// Check for possible UTF-16 encoding (LE or BE).
71-
hasBOM = false;
72-
Encoding encoding = Utf16Detector.DetectFromStream(stream, maxBytesToRead);
73-
if (encoding != null)
74-
{
75-
return encoding;
76-
}
77-
// https://github.com/CharsetDetector/UTF-unknown
78-
stream.Position = 0L;
79-
DetectionDetail resultDetected = CharsetDetector.DetectFromStream(stream, maxBytesToRead).Detected;
80-
if (resultDetected != null)
71+
using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
8172
{
82-
hasBOM = resultDetected.HasBOM;
83-
return resultDetected.Encoding;
73+
// Check for possible UTF-16 encoding (LE or BE).
74+
Encoding encoding = Utf16Detector.DetectFromStream(stream, maxBytesToRead);
75+
if (encoding != null)
76+
{
77+
return encoding;
78+
}
79+
// https://github.com/CharsetDetector/UTF-unknown
80+
stream.Position = 0L;
81+
var result = CharsetDetector.DetectFromStream(stream, maxBytesToRead);
82+
if (result.Detected != null)
83+
{
84+
hasBOM = result.Detected.HasBOM;
85+
return result.Detected.Encoding;
86+
}
87+
return null;
8488
}
89+
}
90+
catch
91+
{
8592
return null;
8693
}
8794
}

sources/EncodingChecker/MainForm.cs

Lines changed: 57 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -290,75 +290,78 @@ private void OnConvert(object sender, EventArgs e)
290290
foreach (ListViewItem item in lstResults.CheckedItems)
291291
{
292292
string charset = item.SubItems[RESULTS_COLUMN_CHARSET].Text;
293-
if (charset.EndsWith("-bom"))
294-
charset = charset.Replace("-bom", "");
295293
if (charset == "(Unknown)")
296294
continue;
295+
296+
if (charset.EndsWith("-bom"))
297+
charset = charset.Replace("-bom", "");
298+
297299
string fileName = item.SubItems[RESULTS_COLUMN_FILE_NAME].Text;
298300
string directory = item.SubItems[RESULTS_COLUMN_DIRECTORY].Text;
299301
string filePath = Path.Combine(directory, fileName);
300302

301-
FileAttributes attributes = File.GetAttributes(filePath);
302-
if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
303-
{
304-
attributes ^= FileAttributes.ReadOnly;
305-
File.SetAttributes(filePath, attributes);
306-
}
307-
308-
if (!Encoding.GetEncoding(charset).Validate(File.ReadAllBytes(filePath)))
309-
{
310-
Debug.WriteLine("Decoding error. " + filePath);
311-
continue;
312-
}
313-
314-
string content;
315-
using (StreamReader reader = new StreamReader(filePath, Encoding.GetEncoding(charset)))
316-
content = reader.ReadToEnd();
317-
318-
string targetCharset = (string)lstConvert.SelectedItem;
319-
Encoding encoding;
320-
// handle UTF-8/16 and UTF-8/16 with BOM
321-
switch (targetCharset)
303+
try
322304
{
323-
case "utf-8":
324-
encoding = new UTF8Encoding(false);
325-
break;
326-
327-
case "utf-8-bom":
328-
encoding = new UTF8Encoding(true);
329-
break;
305+
FileAttributes attributes = File.GetAttributes(filePath);
306+
if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
307+
{
308+
attributes ^= FileAttributes.ReadOnly;
309+
File.SetAttributes(filePath, attributes);
310+
}
330311

331-
case "utf-16":
332-
encoding = new UnicodeEncoding(bigEndian: false, byteOrderMark: false);
333-
break;
312+
if (!Encoding.GetEncoding(charset).Validate(File.ReadAllBytes(filePath)))
313+
{
314+
Debug.WriteLine("Decoding error. " + filePath);
315+
continue;
316+
}
334317

335-
case "utf-16-bom":
336-
encoding = new UnicodeEncoding(bigEndian: false, byteOrderMark: true);
337-
break;
318+
string content;
319+
using (StreamReader reader = new StreamReader(filePath, Encoding.GetEncoding(charset)))
320+
content = reader.ReadToEnd();
338321

339-
case "utf-16BE":
340-
encoding = new UnicodeEncoding(bigEndian: true, byteOrderMark: false);
341-
break;
322+
string targetCharset = (string)lstConvert.SelectedItem;
323+
Encoding encoding;
324+
// handle UTF-8/16 and UTF-8/16 with BOM
325+
switch (targetCharset)
326+
{
327+
case "utf-8":
328+
encoding = new UTF8Encoding(false);
329+
break;
330+
case "utf-8-bom":
331+
encoding = new UTF8Encoding(true);
332+
break;
333+
case "utf-16":
334+
encoding = new UnicodeEncoding(bigEndian: false, byteOrderMark: false);
335+
break;
336+
case "utf-16-bom":
337+
encoding = new UnicodeEncoding(bigEndian: false, byteOrderMark: true);
338+
break;
339+
case "utf-16BE":
340+
encoding = new UnicodeEncoding(bigEndian: true, byteOrderMark: false);
341+
break;
342+
case "utf-16BE-bom":
343+
encoding = new UnicodeEncoding(bigEndian: true, byteOrderMark: true);
344+
break;
345+
default:
346+
encoding = Encoding.GetEncoding(targetCharset);
347+
break;
348+
}
342349

343-
case "utf-16BE-bom":
344-
encoding = new UnicodeEncoding(bigEndian: true, byteOrderMark: true);
345-
break;
350+
using (StreamWriter writer = new StreamWriter(filePath, append: false, encoding))
351+
{
352+
// TODO: catch exceptions
353+
writer.Write(content);
354+
writer.Flush();
355+
}
346356

347-
default:
348-
encoding = Encoding.GetEncoding(targetCharset);
349-
break;
357+
item.Checked = false;
358+
item.ImageIndex = 0;
359+
item.SubItems[RESULTS_COLUMN_CHARSET].Text = targetCharset;
350360
}
351-
352-
using (StreamWriter writer = new StreamWriter(filePath, append: false, encoding))
361+
catch
353362
{
354-
// TODO: catch exceptions
355-
writer.Write(content);
356-
writer.Flush();
363+
// do nothing
357364
}
358-
359-
item.Checked = false;
360-
item.ImageIndex = 0;
361-
item.SubItems[RESULTS_COLUMN_CHARSET].Text = targetCharset;
362365
}
363366

364367
// resume drawing of the results list view control

0 commit comments

Comments
 (0)