X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mcs%2Fclass%2Fcorlib%2FSystem.Text%2FUnicodeEncoding.cs;h=1d4ef22469391bfa9e9c6ef4527740a2887d3e38;hb=c694fb8e631e8f79b383b8ff696228abe1689a20;hp=80373bb3618253e8ed5c5c6664232a458331894a;hpb=948dbf8d4581ac17f5420cc4f7dc375e3c502576;p=mono.git diff --git a/mcs/class/corlib/System.Text/UnicodeEncoding.cs b/mcs/class/corlib/System.Text/UnicodeEncoding.cs index 80373bb3618..1d4ef224693 100644 --- a/mcs/class/corlib/System.Text/UnicodeEncoding.cs +++ b/mcs/class/corlib/System.Text/UnicodeEncoding.cs @@ -4,7 +4,7 @@ * * Copyright (c) 2001, 2002 Southern Storm Software, Pty Ltd * Copyright (C) 2003, 2004 Novell, Inc. - * Copyright (C) 2006 Kornél Pál + * Copyright (C) 2006 Kornél Pál * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the "Software"), @@ -29,9 +29,13 @@ namespace System.Text { using System; +using System.Runtime.InteropServices; [Serializable] -[MonoTODO ("Fix serialization compatibility with MS.NET")] +#if NET_2_0 +[ComVisible (true)] +#endif +[MonoTODO ("Serialization format not compatible with .NET")] public class UnicodeEncoding : Encoding { // Magic numbers used by Windows for Unicode. @@ -78,6 +82,14 @@ public class UnicodeEncoding : Encoding windows_code_page = UNICODE_CODE_PAGE; } +#if NET_2_0 + [MonoTODO ("Implement throwOnInvalidBytes")] + public UnicodeEncoding (bool bigEndian, bool byteOrderMark, bool throwOnInvalidBytes) + : this (bigEndian, byteOrderMark) + { + } +#endif + // Get the number of bytes needed to encode a character buffer. public override int GetByteCount (char[] chars, int index, int count) { @@ -103,6 +115,7 @@ public class UnicodeEncoding : Encoding #if NET_2_0 [CLSCompliantAttribute (false)] + [ComVisible (false)] public unsafe override int GetByteCount (char* chars, int count) { if (chars == null) @@ -147,7 +160,7 @@ public class UnicodeEncoding : Encoding } #if !NET_2_0 - public unsafe override byte [] GetBytes (String s) + public override byte [] GetBytes (String s) { if (s == null) throw new ArgumentNullException ("s"); @@ -155,10 +168,7 @@ public class UnicodeEncoding : Encoding int byteCount = GetByteCount (s); byte [] bytes = new byte [byteCount]; - if (byteCount != 0) - fixed (char* charPtr = s) - fixed (byte* bytePtr = bytes) - GetBytesInternal (charPtr, s.Length, bytePtr, byteCount); + GetBytes (s, 0, s.Length, bytes, 0); return bytes; } @@ -183,6 +193,10 @@ public class UnicodeEncoding : Encoding throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array")); } + // For consistency + if (charCount == 0) + return 0; + int byteCount = bytes.Length - byteIndex; if (bytes.Length == 0) bytes = new byte [1]; @@ -194,6 +208,7 @@ public class UnicodeEncoding : Encoding #if NET_2_0 [CLSCompliantAttribute (false)] + [ComVisible (false)] public unsafe override int GetBytes (char* chars, int charCount, byte* bytes, int byteCount) { @@ -239,6 +254,7 @@ public class UnicodeEncoding : Encoding #if NET_2_0 [CLSCompliantAttribute (false)] + [ComVisible (false)] public unsafe override int GetCharCount (byte* bytes, int count) { if (bytes == null) @@ -284,6 +300,7 @@ public class UnicodeEncoding : Encoding #if NET_2_0 [CLSCompliantAttribute (false)] + [ComVisible (false)] public unsafe override int GetChars (byte* bytes, int byteCount, char* chars, int charCount) { @@ -300,30 +317,41 @@ public class UnicodeEncoding : Encoding } #endif + // Decode a buffer of bytes into a string. + [ComVisible (false)] + public unsafe override String GetString (byte [] bytes, int index, int count) + { + if (bytes == null) + throw new ArgumentNullException ("bytes"); + if (index < 0 || index > bytes.Length) + throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array")); + if (count < 0 || count > (bytes.Length - index)) + throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array")); + + if (count == 0) + return string.Empty; + + // GetCharCountInternal + int charCount = count / 2; + string s = string.InternalAllocateStr (charCount); + + fixed (byte* bytePtr = bytes) + fixed (char* charPtr = s) + GetCharsInternal (bytePtr + index, count, charPtr, charCount); + + return s; + } + private unsafe int GetCharsInternal (byte* bytes, int byteCount, char* chars, int charCount) { int count = byteCount / 2; - bool isBigEndian; - - // Determine the byte order in the incoming buffer. - if (byteCount >= 2) - { - if (bytes [0] == (byte) 0xFE && bytes [1] == (byte) 0xFF) - isBigEndian = true; - else if (bytes [0] == (byte) 0xFF && bytes [1] == (byte) 0xFE) - isBigEndian = false; - else - isBigEndian = bigEndian; - } else { - isBigEndian = bigEndian; - } // Validate that we have sufficient space in "chars". if (charCount < count) throw new ArgumentException (_("Arg_InsufficientSpace")); - CopyChars (bytes, (byte*) chars, byteCount, isBigEndian); + CopyChars (bytes, (byte*) chars, byteCount, bigEndian); return count; } @@ -559,7 +587,6 @@ public class UnicodeEncoding : Encoding if (byteCount == 0) return 0; - bool isBigEndian = bigEndian; int leftOver = leftOverByte; int count; @@ -572,7 +599,7 @@ public class UnicodeEncoding : Encoding throw new ArgumentException (_("Arg_InsufficientSpace")); if (leftOver != -1) { - if (isBigEndian) + if (bigEndian) chars [charIndex] = unchecked ((char) ((leftOver << 8) | (int) bytes [byteIndex])); else chars [charIndex] = unchecked ((char) (((int) bytes [byteIndex] << 8) | leftOver)); @@ -584,7 +611,7 @@ public class UnicodeEncoding : Encoding if ((byteCount & unchecked ((int) 0xFFFFFFFE)) != 0) fixed (byte* bytePtr = bytes) fixed (char* charPtr = chars) - CopyChars (bytePtr + byteIndex, (byte*) (charPtr + charIndex), byteCount, isBigEndian); + CopyChars (bytePtr + byteIndex, (byte*) (charPtr + charIndex), byteCount, bigEndian); if ((byteCount & 1) == 0) leftOverByte = -1;