@ -16,16 +16,18 @@ namespace ICSharpCode.TextEditor.Util
/// </summary>
public static class FileReader
{
public static readonly UTF8Encoding Utf8WithoutBom = new ( encoderShouldEmitUTF8Identifier : false ) ;
public static bool IsUnicode ( Encoding encoding )
{
var codepage = encoding . CodePage ;
int codepage = encoding . CodePage ;
// return true if codepage is any UTF codepage
return codepage = = 6 5 0 0 1 | | codepage = = 6 5 0 0 0 | | codepage = = 1 2 0 0 | | codepage = = 1 2 0 1 ;
}
public static string ReadFileContent ( Stream fs , ref Encoding encoding )
{
using ( va r reader = OpenStream ( fs , encoding ) )
using ( StreamReade r reader = OpenStream ( fs , encoding ) )
{
reader . Peek ( ) ;
encoding = reader . CurrentEncoding ;
@ -35,7 +37,7 @@ namespace ICSharpCode.TextEditor.Util
public static string ReadFileContent ( string fileName , Encoding encoding )
{
using ( var fs = new FileStream ( fileName , FileMode . Open , FileAccess . Read , FileShare . ReadWrite ) )
using ( FileStream fs = new ( fileName , FileMode . Open , FileAccess . Read , FileShare . ReadWrite ) )
{
return ReadFileContent ( fs , ref encoding ) ;
}
@ -50,8 +52,8 @@ namespace ICSharpCode.TextEditor.Util
{
// the autodetection of StreamReader is not capable of detecting the difference
// between ISO-8859-1 and UTF-8 without BOM.
var firstByte = fs . ReadByte ( ) ;
var secondByte = fs . ReadByte ( ) ;
int firstByte = fs . ReadByte ( ) ;
int secondByte = fs . ReadByte ( ) ;
switch ( ( firstByte < < 8 ) | secondByte )
{
case 0 x0000 : // either UTF-32 Big Endian or a binary file; use StreamReader
@ -74,14 +76,14 @@ namespace ICSharpCode.TextEditor.Util
private static StreamReader AutoDetect ( Stream fs , byte firstByte , byte secondByte , Encoding defaultEncoding )
{
var max = ( int ) Math . Min ( fs . Length , val2 : 5 0 0 0 0 0 ) ; // look at max. 500 KB
int max = ( int ) Math . Min ( fs . Length , val2 : 5 0 0 0 0 0 ) ; // look at max. 500 KB
const int ASCII = 0 ;
const int Error = 1 ;
const int UTF8 = 2 ;
const int UTF8Sequence = 3 ;
var state = ASCII ;
var sequenceLength = 0 ;
for ( var i = 0 ; i < max ; i + + )
int state = ASCII ;
int sequenceLength = 0 ;
for ( int i = 0 ; i < max ; i + + )
{
byte b ;
if ( i = = 0 )
@ -159,8 +161,8 @@ namespace ICSharpCode.TextEditor.Util
defaultEncoding = Encoding . Default ; // use system encoding instead
return new StreamReader ( fs , defaultEncoding ) ;
default :
return new StreamReader ( fs ) ;
return new StreamReader ( fs , Utf8WithoutBom ) ;
}
}
}
}
}