*
* Copyright (c) 2001, 2002 Southern Storm Software, Pty Ltd
* Copyright (C) 2003, 2004 Novell, Inc.
- * Copyright (C) 2006 Kornél Pál <http://www.kornelpal.hu/>
+ * Copyright (C) 2006 Kornél Pál <http://www.kornelpal.hu/>
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the "Software"),
{
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.
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)
{
#if NET_2_0
[CLSCompliantAttribute (false)]
+ [ComVisible (false)]
public unsafe override int GetByteCount (char* chars, int count)
{
if (chars == null)
}
#if !NET_2_0
- public unsafe override byte [] GetBytes (String s)
+ public override byte [] GetBytes (String s)
{
if (s == null)
throw new ArgumentNullException ("s");
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;
}
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];
#if NET_2_0
[CLSCompliantAttribute (false)]
+ [ComVisible (false)]
public unsafe override int GetBytes (char* chars, int charCount,
byte* bytes, int byteCount)
{
#if NET_2_0
[CLSCompliantAttribute (false)]
+ [ComVisible (false)]
public unsafe override int GetCharCount (byte* bytes, int count)
{
if (bytes == null)
#if NET_2_0
[CLSCompliantAttribute (false)]
+ [ComVisible (false)]
public unsafe override int GetChars (byte* bytes, int byteCount,
char* chars, int charCount)
{
}
#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;
}
if (byteCount == 0)
return 0;
- bool isBigEndian = bigEndian;
int leftOver = leftOverByte;
int count;
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));
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;