[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Dotgnu-pnet-commits] CVS: pnetlib/Basic/Ecma BinaryReader.cs,NONE,1.1 B
From: |
Rhys Weatherley <address@hidden> |
Subject: |
[Dotgnu-pnet-commits] CVS: pnetlib/Basic/Ecma BinaryReader.cs,NONE,1.1 BinaryWriter.cs,NONE,1.1 BitConverter.cs,NONE,1.1 OptionalAttribute.cs,NONE,1.1 README,NONE,1.1 TypeCode.cs,NONE,1.1 |
Date: |
Thu, 29 May 2003 04:07:03 -0400 |
Update of /cvsroot/dotgnu-pnet/pnetlib/Basic/Ecma
In directory subversions:/tmp/cvs-serv20651/Basic/Ecma
Added Files:
BinaryReader.cs BinaryWriter.cs BitConverter.cs
OptionalAttribute.cs README TypeCode.cs
Log Message:
Modifications to build the VB library in ECMA_COMPAT modes.
--- NEW FILE ---
/*
* BinaryReader.cs - Implementation of the "System.IO.BinaryReader" class.
*
* Copyright (C) 2001, 2003 Southern Storm Software, Pty Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
namespace System.IO
{
#if ECMA_COMPAT
using System;
using System.Text;
using Microsoft.VisualBasic;
internal class BinaryReader : IDisposable
{
// Internal state.
private Stream input;
private Encoding encoding;
private Decoder decoder;
private byte[] smallBuffer;
private char[] smallCharBuffer;
// Constructors.
public BinaryReader(Stream input)
: this(input, Encoding.UTF8)
{
// Nothing to do here.
}
public BinaryReader(Stream input, Encoding encoding)
{
if(input == null)
{
throw new
ArgumentNullException("input");
}
if(encoding == null)
{
throw new
ArgumentNullException("encoding");
}
if(!input.CanRead)
{
throw new
ArgumentException(S._("IO_NotSupp_Read"));
}
this.input = input;
this.encoding = encoding;
decoder = encoding.GetDecoder();
smallBuffer = new byte [16];
smallCharBuffer = new char [1];
}
// Get the base stream that underlies this binary reader.
public virtual Stream BaseStream
{
get
{
return input;
}
}
// Close this stream.
public virtual void Close()
{
if(input != null)
{
input.Close();
}
Dispose(true);
}
// Implement the IDisposable interface.
void IDisposable.Dispose()
{
Dispose(true);
}
// Internal implementation of stream disposal.
protected virtual void Dispose(bool disposing)
{
if(input != null)
{
input = null;
}
encoding = null;
decoder = null;
smallBuffer = null;
smallCharBuffer = null;
}
// Read a number of bytes into the small buffer.
protected virtual void FillBuffer(int num)
{
if(input == null)
{
throw new
IOException(S._("IO_StreamClosed"));
}
else if(input.Read(smallBuffer, 0, num) != num)
{
throw new
EndOfStreamException(S._("IO_ReadEndOfStream"));
}
}
// Peek at the next character.
public virtual int PeekChar()
{
// Bail out if the stream is invalid.
if(input == null)
{
throw new
IOException(S._("IO_StreamClosed"));
}
// If we cannot seek on the stream, then
indicate EOF.
if(!input.CanSeek)
{
return -1;
}
// Save the current position, read, and
back-track.
long posn = input.Position;
int ch = Read();
input.Position = posn;
return ch;
}
// Read the next character, returning -1 at EOF.
public virtual int Read()
{
int posn;
int byteval;
byte[] newBuffer;
// Bail out if the stream is invalid.
if(input == null)
{
throw new
IOException(S._("IO_StreamClosed"));
}
// Keep reading bytes until we have enough for
one character
// according to the encoding.
posn = 0;
do
{
if(posn >= smallBuffer.Length)
{
newBuffer = new byte
[smallBuffer.Length + 16];
Array.Copy(smallBuffer,
newBuffer, smallBuffer.Length);
smallBuffer = newBuffer;
}
byteval = input.ReadByte();
if(byteval == -1)
{
return -1;
}
smallBuffer[posn++] = (byte)byteval;
}
while(decoder.GetChars(smallBuffer, 0, posn,
smallCharBuffer, 0) < 1);
// Return the character that we read to the
caller.
return smallCharBuffer[0];
}
// Read the next character, throwing an exception at EOF.
public virtual char ReadChar()
{
int ch = Read();
if(ch == -1)
{
throw new
EndOfStreamException(S._("IO_ReadEndOfStream"));
}
return (char)ch;
}
// Read a buffer of bytes.
public virtual int Read(byte[] buffer, int index, int count)
{
if(input == null)
{
throw new
IOException(S._("IO_StreamClosed"));
}
return input.Read(buffer, index, count);
}
// Read a buffer of characters.
public virtual int Read(char[] buffer, int index, int count)
{
int posn;
int byteval;
int num;
byte[] newBuffer;
// Bail out if the stream is invalid.
if(input == null)
{
throw new
IOException(S._("IO_StreamClosed"));
}
// Keep reading bytes until EOF or we have
enough characters
// to fill the buffer.
posn = 0;
num = 0;
for(;;)
{
if(posn >= smallBuffer.Length)
{
newBuffer = new byte
[smallBuffer.Length + 16];
Array.Copy(smallBuffer,
newBuffer, smallBuffer.Length);
smallBuffer = newBuffer;
}
byteval = input.ReadByte();
if(byteval == -1)
{
break;
}
smallBuffer[posn++] = (byte)byteval;
if(decoder.GetChars(smallBuffer, 0,
posn,
buffer, index) != 0)
{
posn = 0;
++num;
++index;
}
}
// Return the number of characters we have read
so far.
return num;
}
// Read boolean values.
public virtual bool ReadBoolean()
{
FillBuffer(1);
return (smallBuffer[0] != 0);
}
// Read various kinds of integer values.
public virtual byte ReadByte()
{
FillBuffer(1);
return smallBuffer[0];
}
[CLSCompliant(false)]
public virtual sbyte ReadSByte()
{
FillBuffer(1);
return (sbyte)(smallBuffer[0]);
}
public virtual short ReadInt16()
{
FillBuffer(2);
return (short)(((int)(smallBuffer[0])) |
(((int)(smallBuffer[1])) <<
8));
}
[CLSCompliant(false)]
public virtual ushort ReadUInt16()
{
FillBuffer(2);
return (ushort)(((int)(smallBuffer[0])) |
(((int)(smallBuffer[1])) << 8));
}
public virtual int ReadInt32()
{
FillBuffer(4);
return ((int)(smallBuffer[0])) |
(((int)(smallBuffer[1])) << 8) |
(((int)(smallBuffer[2])) << 16) |
(((int)(smallBuffer[3])) << 24);
}
[CLSCompliant(false)]
public virtual uint ReadUInt32()
{
FillBuffer(4);
return ((uint)(smallBuffer[0])) |
(((uint)(smallBuffer[1])) << 8) |
(((uint)(smallBuffer[2])) << 16) |
(((uint)(smallBuffer[3])) << 24);
}
public virtual long ReadInt64()
{
FillBuffer(8);
return ((long)(smallBuffer[0])) |
(((long)(smallBuffer[1])) << 8) |
(((long)(smallBuffer[2])) << 16) |
(((long)(smallBuffer[3])) << 24) |
(((long)(smallBuffer[4])) << 32) |
(((long)(smallBuffer[5])) << 40) |
(((long)(smallBuffer[6])) << 48) |
(((long)(smallBuffer[7])) << 56);
}
[CLSCompliant(false)]
public virtual ulong ReadUInt64()
{
FillBuffer(8);
return ((ulong)(smallBuffer[0])) |
(((ulong)(smallBuffer[1])) << 8) |
(((ulong)(smallBuffer[2])) << 16) |
(((ulong)(smallBuffer[3])) << 24) |
(((ulong)(smallBuffer[4])) << 32) |
(((ulong)(smallBuffer[5])) << 40) |
(((ulong)(smallBuffer[6])) << 48) |
(((ulong)(smallBuffer[7])) << 56);
}
// Read floating-point values.
public virtual float ReadSingle()
{
int value;
FillBuffer(4);
if(BitConverter.IsLittleEndian)
{
value = ((int)(smallBuffer[0])) |
(((int)(smallBuffer[1])) << 8) |
(((int)(smallBuffer[2])) << 16)
|
(((int)(smallBuffer[3])) << 24);
}
else
{
value = ((int)(smallBuffer[3])) |
(((int)(smallBuffer[2])) << 8) |
(((int)(smallBuffer[1])) << 16)
|
(((int)(smallBuffer[0])) << 24);
}
return BitConverter.Int32BitsToFloat(value);
}
public virtual double ReadDouble()
{
long value;
FillBuffer(8);
if(BitConverter.IsLittleEndian)
{
value = ((long)(smallBuffer[0])) |
(((long)(smallBuffer[1]))
<< 8) |
(((long)(smallBuffer[2]))
<< 16) |
(((long)(smallBuffer[3]))
<< 24) |
(((long)(smallBuffer[4]))
<< 32) |
(((long)(smallBuffer[5]))
<< 40) |
(((long)(smallBuffer[6]))
<< 48) |
(((long)(smallBuffer[7]))
<< 56);
}
else
{
value = ((long)(smallBuffer[7])) |
(((long)(smallBuffer[6]))
<< 8) |
(((long)(smallBuffer[5]))
<< 16) |
(((long)(smallBuffer[4]))
<< 24) |
(((long)(smallBuffer[3]))
<< 32) |
(((long)(smallBuffer[2]))
<< 40) |
(((long)(smallBuffer[1]))
<< 48) |
(((long)(smallBuffer[0]))
<< 56);
}
return BitConverter.Int64BitsToDouble(value);
}
// Read Decimal values.
public virtual Decimal ReadDecimal()
{
FillBuffer(16);
int[] bits = new int [4];
bits[0] = ((int)(smallBuffer[0])) |
(((int)(smallBuffer[1])) << 8) |
(((int)(smallBuffer[2])) << 16) |
(((int)(smallBuffer[3])) << 24);
bits[1] = ((int)(smallBuffer[4])) |
(((int)(smallBuffer[5])) << 8) |
(((int)(smallBuffer[6])) << 16) |
(((int)(smallBuffer[7])) << 24);
bits[2] = ((int)(smallBuffer[8])) |
(((int)(smallBuffer[9])) << 8) |
(((int)(smallBuffer[10])) << 16) |
(((int)(smallBuffer[11])) << 24);
bits[3] = ((int)(smallBuffer[12])) |
(((int)(smallBuffer[13])) << 8) |
(((int)(smallBuffer[14])) << 16) |
(((int)(smallBuffer[15])) << 24);
return new Decimal(bits);
}
// Read a buffer of bytes.
public virtual byte[] ReadBytes(int count)
{
byte[] buffer;
int result;
if(count < 0)
{
throw new ArgumentOutOfRangeException
("count",
S._("ArgRange_NonNegative"));
}
buffer = new byte [count];
result = Read(buffer, 0, count);
if(result == 0)
{
throw new
EndOfStreamException(S._("IO_ReadEndOfStream"));
}
if(result != count)
{
byte[] newBuffer = new byte [result];
Array.Copy(buffer, newBuffer, result);
return newBuffer;
}
else
{
return buffer;
}
}
// Read a buffer of characters.
public virtual char[] ReadChars(int count)
{
byte[] buffer;
int len;
if(count < 0)
{
throw new ArgumentOutOfRangeException
("count",
S._("ArgRange_NonNegative"));
}
buffer = ReadBytes(count);
if(buffer.Length <= 0 || buffer.Length!=count)
{
/* this exception fall through to the
ReadString */
throw new
EndOfStreamException(S._("IO_ReadEndOfStream"));
}
len=encoding.GetCharCount(buffer,0,buffer.Length);
char[] newBuffer = new char[len];
encoding.GetChars(buffer,0,buffer.Length,newBuffer,0);
return newBuffer;
}
// Read a prefix-encoded string.
public virtual String ReadString()
{
int len = Read7BitEncodedInt();
char[] chars;
if(len == 0)
{
return String.Empty;
}
else
{
chars = ReadChars(len);
/* an exception is thrown from
ReadChars for EndOfStream */
return new String(chars);
}
}
// Read an integer value that is encoded in a 7-bit format.
protected int Read7BitEncodedInt()
{
int value = 0;
int byteval;
int shift = 0;
while(((byteval = ReadByte()) & 0x80) != 0)
{
value |= ((byteval & 0x7F) << shift);
shift += 7;
}
return (value | (byteval << shift));
}
}; // class BinaryReader
#endif // ECMA_COMPAT
}; // namespace System.IO
--- NEW FILE ---
/*
* BinaryWriter.cs - Implementation of the "System.IO.BinaryWriter" class.
*
* Copyright (C) 2001, 2003 Southern Storm Software, Pty Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
namespace System.IO
{
#if ECMA_COMPAT
using System;
using System.Text;
using Microsoft.VisualBasic;
internal class BinaryWriter : IDisposable
{
// The null binary writer.
public static readonly BinaryWriter Null = new
BinaryWriter(Stream.Null);
// Internal state.
private Encoding encoding;
private Encoder encoder;
private byte[] smallBuffer;
private char[] smallCharBuffer;
private byte[] largeBuffer;
// Accessible internal state.
protected Stream OutStream;
// Constructors.
protected BinaryWriter()
: this(Stream.Null, Encoding.UTF8)
{
// Nothing to do here
}
public BinaryWriter(Stream output)
: this(output, Encoding.UTF8)
{
// Nothing to do here.
}
public BinaryWriter(Stream output, Encoding encoding)
{
if(output == null)
{
throw new
ArgumentNullException("output");
}
if(encoding == null)
{
throw new
ArgumentNullException("encoding");
}
if(!output.CanWrite)
{
throw new
ArgumentException(S._("IO_NotSupp_Write"));
}
OutStream = output;
this.encoding = encoding;
encoder = encoding.GetEncoder();
smallBuffer = new byte [16];
smallCharBuffer = new char [1];
}
// Get the base stream that underlies this binary writer.
public virtual Stream BaseStream
{
get
{
return OutStream;
}
}
// Close this stream.
public virtual void Close()
{
if(OutStream != null)
{
OutStream.Close();
}
Dispose(true);
}
// Implement the IDisposable interface.
void IDisposable.Dispose()
{
Dispose(true);
}
// Internal implementation of stream disposal.
protected virtual void Dispose(bool disposing)
{
if(OutStream != null)
{
OutStream = null;
}
encoding = null;
encoder = null;
smallBuffer = null;
smallCharBuffer = null;
largeBuffer = null;
}
// Flush this writer.
public virtual void Flush()
{
if(OutStream != null)
{
OutStream.Flush();
}
}
// Write the contents of the small buffer to the output stream.
private void WriteBuffer(int num)
{
if(OutStream == null)
{
throw new
IOException(S._("IO_StreamClosed"));
}
OutStream.Write(smallBuffer, 0, num);
}
// Seek to a new position within the underlying stream.
// Note: the offset value should be "long", but Microsoft's
// .NET Beta2 SDK defines this as "int".
public virtual long Seek(int offset, SeekOrigin origin)
{
return OutStream.Seek(offset, origin);
}
// Write a boolean value to the output.
public virtual void Write(bool value)
{
smallBuffer[0] = (byte)(value ? 1 : 0);
WriteBuffer(1);
}
// Write integer numeric values to the output.
public virtual void Write(byte value)
{
smallBuffer[0] = value;
WriteBuffer(1);
}
[CLSCompliant(false)]
public virtual void Write(sbyte value)
{
smallBuffer[0] = (byte)value;
WriteBuffer(1);
}
public virtual void Write(short value)
{
smallBuffer[0] = (byte)value;
smallBuffer[1] = (byte)(value >> 8);
WriteBuffer(2);
}
[CLSCompliant(false)]
public virtual void Write(ushort value)
{
smallBuffer[0] = (byte)value;
smallBuffer[1] = (byte)(value >> 8);
WriteBuffer(2);
}
public virtual void Write(int value)
{
smallBuffer[0] = (byte)value;
smallBuffer[1] = (byte)(value >> 8);
smallBuffer[2] = (byte)(value >> 16);
smallBuffer[3] = (byte)(value >> 24);
WriteBuffer(4);
}
[CLSCompliant(false)]
public virtual void Write(uint value)
{
smallBuffer[0] = (byte)value;
smallBuffer[1] = (byte)(value >> 8);
smallBuffer[2] = (byte)(value >> 16);
smallBuffer[3] = (byte)(value >> 24);
WriteBuffer(4);
}
public virtual void Write(long value)
{
smallBuffer[0] = (byte)value;
smallBuffer[1] = (byte)(value >> 8);
smallBuffer[2] = (byte)(value >> 16);
smallBuffer[3] = (byte)(value >> 24);
smallBuffer[4] = (byte)(value >> 32);
smallBuffer[5] = (byte)(value >> 40);
smallBuffer[6] = (byte)(value >> 48);
smallBuffer[7] = (byte)(value >> 56);
WriteBuffer(8);
}
[CLSCompliant(false)]
public virtual void Write(ulong value)
{
smallBuffer[0] = (byte)value;
smallBuffer[1] = (byte)(value >> 8);
smallBuffer[2] = (byte)(value >> 16);
smallBuffer[3] = (byte)(value >> 24);
smallBuffer[4] = (byte)(value >> 32);
smallBuffer[5] = (byte)(value >> 40);
smallBuffer[6] = (byte)(value >> 48);
smallBuffer[7] = (byte)(value >> 56);
WriteBuffer(8);
}
// Write floating-point values to the output.
public virtual void Write(float value)
{
int bits = BitConverter.FloatToInt32Bits(value);
if(BitConverter.IsLittleEndian)
{
smallBuffer[0] = (byte)bits;
smallBuffer[1] = (byte)(bits >> 8);
smallBuffer[2] = (byte)(bits >> 16);
smallBuffer[3] = (byte)(bits >> 24);
}
else
{
smallBuffer[3] = (byte)bits;
smallBuffer[2] = (byte)(bits >> 8);
smallBuffer[1] = (byte)(bits >> 16);
smallBuffer[0] = (byte)(bits >> 24);
}
WriteBuffer(4);
}
public virtual void Write(double value)
{
long bits =
BitConverter.DoubleToInt64Bits(value);
if(BitConverter.IsLittleEndian)
{
smallBuffer[0] = (byte)bits;
smallBuffer[1] = (byte)(bits >> 8);
smallBuffer[2] = (byte)(bits >> 16);
smallBuffer[3] = (byte)(bits >> 24);
smallBuffer[4] = (byte)(bits >> 32);
smallBuffer[5] = (byte)(bits >> 40);
smallBuffer[6] = (byte)(bits >> 48);
smallBuffer[7] = (byte)(bits >> 56);
}
else
{
smallBuffer[7] = (byte)bits;
smallBuffer[6] = (byte)(bits >> 8);
smallBuffer[5] = (byte)(bits >> 16);
smallBuffer[4] = (byte)(bits >> 24);
smallBuffer[3] = (byte)(bits >> 32);
smallBuffer[2] = (byte)(bits >> 40);
smallBuffer[1] = (byte)(bits >> 48);
smallBuffer[0] = (byte)(bits >> 56);
}
WriteBuffer(8);
}
// Write a Decimal value to the output.
public virtual void Write(Decimal value)
{
int[] bits = Decimal.GetBits(value);
smallBuffer[0] = (byte)(bits[0]);
smallBuffer[1] = (byte)(bits[0] >> 8);
smallBuffer[2] = (byte)(bits[0] >> 16);
smallBuffer[3] = (byte)(bits[0] >> 24);
smallBuffer[4] = (byte)(bits[1]);
smallBuffer[5] = (byte)(bits[1] >> 8);
smallBuffer[6] = (byte)(bits[1] >> 16);
smallBuffer[7] = (byte)(bits[1] >> 24);
smallBuffer[8] = (byte)(bits[2]);
smallBuffer[9] = (byte)(bits[2] >> 8);
smallBuffer[10] = (byte)(bits[2] >> 16);
smallBuffer[11] = (byte)(bits[2] >> 24);
smallBuffer[12] = (byte)(bits[3]);
smallBuffer[13] = (byte)(bits[3] >> 8);
smallBuffer[14] = (byte)(bits[3] >> 16);
smallBuffer[15] = (byte)(bits[3] >> 24);
WriteBuffer(16);
}
// Write an array of bytes to the output.
public virtual void Write(byte[] buffer)
{
if(buffer == null)
{
throw new
ArgumentNullException("buffer");
}
if(OutStream == null)
{
throw new
IOException(S._("IO_StreamClosed"));
}
OutStream.Write(buffer, 0, buffer.Length);
}
public virtual void Write(byte[] buffer, int index, int count)
{
if(OutStream == null)
{
throw new
IOException(S._("IO_StreamClosed"));
}
OutStream.Write(buffer, index, count);
}
// Write a character to the output.
public virtual void Write(char ch)
{
smallCharBuffer[0] = ch;
Write(smallCharBuffer, 0, 1);
}
// Write an array of characters to the output.
public virtual void Write(char[] chars)
{
if(chars == null)
{
throw new
ArgumentNullException("chars");
}
Write(chars, 0, chars.Length);
}
public virtual void Write(char[] chars, int index, int count)
{
int byteCount;
byteCount = encoder.GetByteCount(chars, index,
count, true);
if(byteCount <= smallBuffer.Length)
{
encoder.GetBytes(chars, index, count,
smallBuffer, 0, true);
WriteBuffer(byteCount);
}
else
{
if(OutStream == null)
{
throw new
IOException(S._("IO_StreamClosed"));
}
if(largeBuffer == null || byteCount >
largeBuffer.Length)
{
largeBuffer = new byte
[byteCount];
}
encoder.GetBytes(chars, index, count,
largeBuffer, 0, true);
OutStream.Write(largeBuffer, 0,
byteCount);
}
}
// Write a string to the output.
public virtual void Write(String value)
{
char[] chars;
int byteCount;
if(value == null)
{
throw new
ArgumentNullException("value");
}
chars = value.ToCharArray();
byteCount = encoder.GetByteCount(chars, 0,
chars.Length, true);
Write7BitEncodedInt(byteCount); /* byteCount no
charCount */
Write(chars, 0, chars.Length);
}
// Write a 7-bit encoded integer value to the output.
protected void Write7BitEncodedInt(int value)
{
uint temp = (uint)value;
while(temp >= 128)
{
Write((byte)(temp | 0x80));
temp >>= 7;
}
Write((byte)temp);
}
}; // class BinaryWriter
#endif // ECMA_COMPAT
}; // namespace System.IO
--- NEW FILE ---
/*
* BitConverter.cs - Implementation of the "System.BitConverter" class.
*
* Copyright (C) 2001, 2003 Southern Storm Software, Pty Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
namespace System
{
#if ECMA_COMPAT
using System.Text;
using System.Runtime.CompilerServices;
using Microsoft.VisualBasic;
internal sealed class BitConverter
{
// Cannot instantiate this class.
private BitConverter() {}
// Specification of the endian-ness of this platform.
public static readonly bool IsLittleEndian = GetLittleEndian();
// Get the endian-ness of the underlying platform.
[MethodImpl(MethodImplOptions.InternalCall)]
extern private static bool GetLittleEndian();
// Convert a double value into a 64-bit integer.
[MethodImpl(MethodImplOptions.InternalCall)]
extern public static long DoubleToInt64Bits(double value);
// Convert a 64-bit integer into a double value.
[MethodImpl(MethodImplOptions.InternalCall)]
extern public static double Int64BitsToDouble(long value);
// Convert a float value into a 32-bit integer.
[MethodImpl(MethodImplOptions.InternalCall)]
extern internal static int FloatToInt32Bits(float value);
// Convert a 32-bit integer into a float value.
[MethodImpl(MethodImplOptions.InternalCall)]
extern internal static float Int32BitsToFloat(int value);
// Convert a boolean value into an array of bytes.
public static byte[] GetBytes(bool value)
{
byte[] bytes = new byte [1];
bytes[0] = (byte)(value ? 1 : 0);
return bytes;
}
// Convert a character value into an array of bytes.
public static byte[] GetBytes(char value)
{
byte[] bytes = new byte [2];
if(IsLittleEndian)
{
bytes[0] = (byte)value;
bytes[1] = (byte)(value >> 8);
}
else
{
bytes[0] = (byte)(value >> 8);
bytes[1] = (byte)value;
}
return bytes;
}
// Convert a short value into an array of bytes.
public static byte[] GetBytes(short value)
{
byte[] bytes = new byte [2];
if(IsLittleEndian)
{
bytes[0] = (byte)value;
bytes[1] = (byte)(value >> 8);
}
else
{
bytes[0] = (byte)(value >> 8);
bytes[1] = (byte)value;
}
return bytes;
}
// Convert an unsigned short value into an array of bytes.
[CLSCompliant(false)]
public static byte[] GetBytes(ushort value)
{
byte[] bytes = new byte [2];
if(IsLittleEndian)
{
bytes[0] = (byte)value;
bytes[1] = (byte)(value >> 8);
}
else
{
bytes[0] = (byte)(value >> 8);
bytes[1] = (byte)value;
}
return bytes;
}
// Convert an integer value into an array of bytes.
public static byte[] GetBytes(int value)
{
byte[] bytes = new byte [4];
if(IsLittleEndian)
{
bytes[0] = (byte)value;
bytes[1] = (byte)(value >> 8);
bytes[2] = (byte)(value >> 16);
bytes[3] = (byte)(value >> 24);
}
else
{
bytes[0] = (byte)(value >> 24);
bytes[1] = (byte)(value >> 16);
bytes[2] = (byte)(value >> 8);
bytes[3] = (byte)value;
}
return bytes;
}
// Convert an unsigned integer value into an array of bytes.
[CLSCompliant(false)]
public static byte[] GetBytes(uint value)
{
byte[] bytes = new byte [4];
if(IsLittleEndian)
{
bytes[0] = (byte)value;
bytes[1] = (byte)(value >> 8);
bytes[2] = (byte)(value >> 16);
bytes[3] = (byte)(value >> 24);
}
else
{
bytes[0] = (byte)(value >> 24);
bytes[1] = (byte)(value >> 16);
bytes[2] = (byte)(value >> 8);
bytes[3] = (byte)value;
}
return bytes;
}
// Convert a long value into an array of bytes.
public static byte[] GetBytes(long value)
{
byte[] bytes = new byte [8];
if(IsLittleEndian)
{
bytes[0] = (byte)value;
bytes[1] = (byte)(value >> 8);
bytes[2] = (byte)(value >> 16);
bytes[3] = (byte)(value >> 24);
bytes[4] = (byte)(value >> 32);
bytes[5] = (byte)(value >> 40);
bytes[6] = (byte)(value >> 48);
bytes[7] = (byte)(value >> 56);
}
else
{
bytes[0] = (byte)(value >> 56);
bytes[1] = (byte)(value >> 48);
bytes[2] = (byte)(value >> 40);
bytes[3] = (byte)(value >> 32);
bytes[4] = (byte)(value >> 24);
bytes[5] = (byte)(value >> 16);
bytes[6] = (byte)(value >> 8);
bytes[7] = (byte)value;
}
return bytes;
}
// Convert an unsigned long value into an array of bytes.
[CLSCompliant(false)]
public static byte[] GetBytes(ulong value)
{
byte[] bytes = new byte [8];
if(IsLittleEndian)
{
bytes[0] = (byte)value;
bytes[1] = (byte)(value >> 8);
bytes[2] = (byte)(value >> 16);
bytes[3] = (byte)(value >> 24);
bytes[4] = (byte)(value >> 32);
bytes[5] = (byte)(value >> 40);
bytes[6] = (byte)(value >> 48);
bytes[7] = (byte)(value >> 56);
}
else
{
bytes[0] = (byte)(value >> 56);
bytes[1] = (byte)(value >> 48);
bytes[2] = (byte)(value >> 40);
bytes[3] = (byte)(value >> 32);
bytes[4] = (byte)(value >> 24);
bytes[5] = (byte)(value >> 16);
bytes[6] = (byte)(value >> 8);
bytes[7] = (byte)value;
}
return bytes;
}
// Convert a float value into an array of bytes.
public static byte[] GetBytes(float value)
{
return GetBytes(FloatToInt32Bits(value));
}
// Convert a double value into an array of bytes.
public static byte[] GetBytes(double value)
{
return GetBytes(DoubleToInt64Bits(value));
}
// Convert a float value into an array of bytes.
[MethodImpl(MethodImplOptions.InternalCall)]
extern internal static byte[] GetLittleEndianBytes(float value);
// Convert a double value into an array of bytes.
[MethodImpl(MethodImplOptions.InternalCall)]
extern internal static byte[] GetLittleEndianBytes(double value);
// Convert a byte within an array into a boolean value.
public static bool ToBoolean(byte[] value, int startIndex)
{
if(value == null)
{
throw new
ArgumentNullException("value");
}
if(startIndex < 0 || startIndex >= value.Length)
{
throw new ArgumentOutOfRangeException
("startIndex",
S._("Arg_InvalidArrayIndex"));
}
return (value[startIndex] != 0);
}
// Convert bytes within an array into a char value.
public static char ToChar(byte[] value, int startIndex)
{
if(value == null)
{
throw new
ArgumentNullException("value");
}
if(startIndex < 0 || startIndex >=
(value.Length - 1))
{
throw new ArgumentOutOfRangeException
("startIndex",
S._("Arg_InvalidArrayIndex"));
}
if(IsLittleEndian)
{
return
(char)(((int)(value[startIndex])) |
(((int)(value[startIndex + 1])) << 8));
}
else
{
return (char)(((int)(value[startIndex +
1])) |
(((int)(value[startIndex])) << 8));
}
}
// Convert bytes within an array into a short value.
public static short ToInt16(byte[] value, int startIndex)
{
if(value == null)
{
throw new
ArgumentNullException("value");
}
if(startIndex < 0 || startIndex >=
(value.Length - 1))
{
throw new ArgumentOutOfRangeException
("startIndex",
S._("Arg_InvalidArrayIndex"));
}
if(IsLittleEndian)
{
return
(short)(((int)(value[startIndex])) |
(((int)(value[startIndex + 1])) << 8));
}
else
{
return (short)(((int)(value[startIndex
+ 1])) |
(((int)(value[startIndex])) << 8));
}
}
// Convert bytes within an array into an unsigned short value.
[CLSCompliant(false)]
public static ushort ToUInt16(byte[] value, int startIndex)
{
if(value == null)
{
throw new
ArgumentNullException("value");
}
if(startIndex < 0 || startIndex >=
(value.Length - 1))
{
throw new ArgumentOutOfRangeException
("startIndex",
S._("Arg_InvalidArrayIndex"));
}
if(IsLittleEndian)
{
return
(ushort)(((int)(value[startIndex])) |
(((int)(value[startIndex + 1])) << 8));
}
else
{
return (ushort)(((int)(value[startIndex
+ 1])) |
(((int)(value[startIndex])) << 8));
}
}
// Convert bytes within an array into an integer value.
public static int ToInt32(byte[] value, int startIndex)
{
if(value == null)
{
throw new
ArgumentNullException("value");
}
if(startIndex < 0 || startIndex >=
(value.Length - 3))
{
throw new ArgumentOutOfRangeException
("startIndex",
S._("Arg_InvalidArrayIndex"));
}
if(IsLittleEndian)
{
return (((int)(value[startIndex])) |
(((int)(value[startIndex +
1])) << 8) |
(((int)(value[startIndex +
2])) << 16) |
(((int)(value[startIndex +
3])) << 24));
}
else
{
return (((int)(value[startIndex + 3])) |
(((int)(value[startIndex +
2])) << 8) |
(((int)(value[startIndex +
1])) << 16) |
(((int)(value[startIndex]))
<< 24));
}
}
// Convert bytes within an array into an unsigned integer value.
[CLSCompliant(false)]
public static uint ToUInt32(byte[] value, int startIndex)
{
if(value == null)
{
throw new
ArgumentNullException("value");
}
if(startIndex < 0 || startIndex >=
(value.Length - 3))
{
throw new ArgumentOutOfRangeException
("startIndex",
S._("Arg_InvalidArrayIndex"));
}
if(IsLittleEndian)
{
return
(uint)(((int)(value[startIndex])) |
(((int)(value[startIndex + 1])) << 8) |
(((int)(value[startIndex + 2])) << 16) |
(((int)(value[startIndex + 3])) << 24));
}
else
{
return (uint)(((int)(value[startIndex +
3])) |
(((int)(value[startIndex + 2])) << 8) |
(((int)(value[startIndex + 1])) << 16) |
(((int)(value[startIndex])) << 24));
}
}
// Convert bytes within an array into a long value.
public static long ToInt64(byte[] value, int startIndex)
{
if(value == null)
{
throw new
ArgumentNullException("value");
}
if(startIndex < 0 || startIndex >=
(value.Length - 7))
{
throw new ArgumentOutOfRangeException
("startIndex",
S._("Arg_InvalidArrayIndex"));
}
if(IsLittleEndian)
{
return (((long)(value[startIndex])) |
(((long)(value[startIndex +
1])) << 8) |
(((long)(value[startIndex +
2])) << 16) |
(((long)(value[startIndex +
3])) << 24) |
(((long)(value[startIndex +
4])) << 32) |
(((long)(value[startIndex +
5])) << 40) |
(((long)(value[startIndex +
6])) << 48) |
(((long)(value[startIndex +
7])) << 56));
}
else
{
return (((long)(value[startIndex + 7]))
|
(((long)(value[startIndex +
6])) << 8) |
(((long)(value[startIndex +
5])) << 16) |
(((long)(value[startIndex +
4])) << 24) |
(((long)(value[startIndex +
3])) << 32) |
(((long)(value[startIndex +
2])) << 40) |
(((long)(value[startIndex +
1])) << 48) |
(((long)(value[startIndex])) << 56));
}
}
// Convert bytes within an array into an unsigned long value.
[CLSCompliant(false)]
public static ulong ToUInt64(byte[] value, int startIndex)
{
if(value == null)
{
throw new
ArgumentNullException("value");
}
if(startIndex < 0 || startIndex >=
(value.Length - 7))
{
throw new ArgumentOutOfRangeException
("startIndex",
S._("Arg_InvalidArrayIndex"));
}
if(IsLittleEndian)
{
return (((ulong)(value[startIndex])) |
(((ulong)(value[startIndex
+ 1])) << 8) |
(((ulong)(value[startIndex
+ 2])) << 16) |
(((ulong)(value[startIndex
+ 3])) << 24) |
(((ulong)(value[startIndex
+ 4])) << 32) |
(((ulong)(value[startIndex
+ 5])) << 40) |
(((ulong)(value[startIndex
+ 6])) << 48) |
(((ulong)(value[startIndex + 7]))
<< 56));
}
else
{
return (((ulong)(value[startIndex +
7])) |
(((ulong)(value[startIndex
+ 6])) << 8) |
(((ulong)(value[startIndex
+ 5])) << 16) |
(((ulong)(value[startIndex
+ 4])) << 24) |
(((ulong)(value[startIndex
+ 3])) << 32) |
(((ulong)(value[startIndex
+ 2])) << 40) |
(((ulong)(value[startIndex
+ 1])) << 48) |
(((ulong)(value[startIndex])) << 56));
}
}
// Convert bytes within an array into a float value.
public static float ToSingle(byte[] value, int startIndex)
{
return Int32BitsToFloat(ToInt32(value,
startIndex));
}
// Convert bytes within an array into a double value.
public static double ToDouble(byte[] value, int startIndex)
{
return Int64BitsToDouble(ToInt64(value,
startIndex));
}
// Convert bytes within an array into a string of hex values.
public static String ToString(byte[] value)
{
if(value == null)
{
throw new
ArgumentNullException("value");
}
return ToString(value, 0, value.Length);
}
public static String ToString(byte[] value, int startIndex)
{
if(value == null)
{
throw new
ArgumentNullException("value");
}
return ToString(value, startIndex, value.Length
- startIndex);
}
public static String ToString(byte[] value, int startIndex, int length)
{
if(value == null)
{
throw new
ArgumentNullException("value");
}
else if(startIndex < 0 || startIndex >=
value.Length)
{
throw new ArgumentOutOfRangeException
("startIndex",
S._("ArgRange_Array"));
}
else if(length < 0 || (value.Length -
startIndex) < length)
{
throw new ArgumentOutOfRangeException
("length",
S._("ArgRange_Array"));
}
if(length == 0)
{
return String.Empty;
}
StringBuilder builder = new
StringBuilder(length * 3 - 1);
AppendHex(builder, value[startIndex++]);
--length;
while(length > 0)
{
builder.Append('-');
AppendHex(builder, value[startIndex++]);
--length;
}
return builder.ToString();
}
// Append the hex version of a byte to a string builder.
internal static void AppendHex(StringBuilder builder, int value)
{
int digit = ((value / 16) & 0x0F);
if(digit < 10)
builder.Append((char)('0' + digit));
else
builder.Append((char)('A' + digit -
10));
digit = (value & 0x0F);
if(digit < 10)
builder.Append((char)('0' + digit));
else
builder.Append((char)('A' + digit -
10));
}
}; // class BitConverter
#endif // ECMA_COMPAT
}; // namespace System
--- NEW FILE ---
/*
* OptionalAttribute.cs - Implementation of the
* "System.Runtime.InteropServices.OptionalAttribute"
class.
*
* Copyright (C) 2003 Southern Storm Software, Pty Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
namespace System.Runtime.InteropServices
{
#if ECMA_COMPAT
[AttributeUsage(AttributeTargets.Parameter, Inherited=false)]
internal sealed class OptionalAttribute : Attribute
{
// Constructor.
public OptionalAttribute() : base() {}
}; // class OptionalAttribute
#endif // ECMA_COMPAT
}; // namespace System.Runtime.InteropServices
--- NEW FILE ---
This directory contains copies of some classes in "pnetlib/runtime"
so that the "Microsoft.VisualBasic" assembly can be built in ECMA
compatible configurations. The classes are used internally, and
are not exported from the assembly.
--- NEW FILE ---
/*
* TypeCode.cs - Implementation of the "System.TypeCode" class.
*
* Copyright (C) 2001 Southern Storm Software, Pty Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
namespace System
{
#if ECMA_COMPAT
internal enum TypeCode
{
Empty = 0x00,
Object = 0x01,
DBNull = 0x02,
Boolean = 0x03,
Char = 0x04,
SByte = 0x05,
Byte = 0x06,
Int16 = 0x07,
UInt16 = 0x08,
Int32 = 0x09,
UInt32 = 0x0A,
Int64 = 0x0B,
UInt64 = 0x0C,
Single = 0x0D,
Double = 0x0E,
Decimal = 0x0F,
DateTime = 0x10,
String = 0x12
}; // enum TypeCode
#endif // ECMA_COMPAT
}; // namespace System
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Dotgnu-pnet-commits] CVS: pnetlib/Basic/Ecma BinaryReader.cs,NONE,1.1 BinaryWriter.cs,NONE,1.1 BitConverter.cs,NONE,1.1 OptionalAttribute.cs,NONE,1.1 README,NONE,1.1 TypeCode.cs,NONE,1.1,
Rhys Weatherley <address@hidden> <=
- Prev by Date:
[Dotgnu-pnet-commits] CVS: pnetlib/Basic/CompilerServices BooleanType.cs,1.3,1.4 ByteType.cs,1.4,1.5 CharArrayType.cs,1.3,1.4 CharType.cs,1.3,1.4 DateType.cs,1.3,1.4 DecimalType.cs,1.4,1.5 DoubleType.cs,1.4,1.5 FlowControl.cs,1.3,1.4 IntegerType.cs,1.4,1.5 LongType.cs,1.4,1.5 ObjectType.cs,1.4,1.5 ShortType.cs,1.4,1.5 SingleType.cs,1.4,1.5 StringType.cs,1.3,1.4 Utils.cs,1.3,1.4
- Next by Date:
[Dotgnu-pnet-commits] CVS: pnetlib/profiles mscompact,1.1,1.2
- Previous by thread:
[Dotgnu-pnet-commits] CVS: pnetlib/Basic/CompilerServices BooleanType.cs,1.3,1.4 ByteType.cs,1.4,1.5 CharArrayType.cs,1.3,1.4 CharType.cs,1.3,1.4 DateType.cs,1.3,1.4 DecimalType.cs,1.4,1.5 DoubleType.cs,1.4,1.5 FlowControl.cs,1.3,1.4 IntegerType.cs,1.4,1.5 LongType.cs,1.4,1.5 ObjectType.cs,1.4,1.5 ShortType.cs,1.4,1.5 SingleType.cs,1.4,1.5 StringType.cs,1.3,1.4 Utils.cs,1.3,1.4
- Next by thread:
[Dotgnu-pnet-commits] CVS: pnetlib/profiles mscompact,1.1,1.2
- Index(es):