changeset 30:23fd4247fc1c

* Add "big-endian" binary reader that reads ints/shorts in big-endian format * Add methods to String Manipulation to output a byte string as hex characters no-open-ticket
author IBBoard <dev@ibboard.co.uk>
date Sun, 19 Apr 2009 11:31:47 +0000
parents e38192f55d2d
children 7a3749a2d8e6
files IBBoard.csproj IO/BinaryReaderBigEndian.cs Lang/StringManipulation.cs
diffstat 3 files changed, 156 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/IBBoard.csproj	Sat Apr 11 14:47:20 2009 +0000
+++ b/IBBoard.csproj	Sun Apr 19 11:31:47 2009 +0000
@@ -124,6 +124,7 @@
     <Compile Include="CustomMath\NumberParser.cs" />
     <Compile Include="Lang\TranslationLoadException.cs" />
     <Compile Include="Xml\XmlTools.cs" />
+    <Compile Include="IO\BinaryReaderBigEndian.cs" />
   </ItemGroup>
   <ItemGroup>
     <Content Include="libs\log4net.dll" />
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/IO/BinaryReaderBigEndian.cs	Sun Apr 19 11:31:47 2009 +0000
@@ -0,0 +1,137 @@
+//  This file (FileStreamstream.cs) is a part of the IBBoard project and is copyright 2009 IBBoard
+// 
+//  The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license.
+// 
+
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Text;
+
+namespace IBBoard
+{
+	/// <summary>
+	/// A custom replacement for the BinaryReader that reads numbers as big-endian and also provides methods to read byte delimited strings.
+	/// </summary>
+	public class BinaryReaderBigEndian
+	{
+		private Stream stream;
+		private Encoding encoding;
+		
+		public BinaryReaderBigEndian(Stream input) : this(input, Encoding.UTF8)
+		{
+		}
+		
+		public BinaryReaderBigEndian(Stream input, Encoding encoding)
+		{
+			stream = input;
+			this.encoding = encoding;
+		}
+
+		public byte[] ReadBytes(int byteCount)
+		{
+			byte[] bytes = new byte[byteCount];
+			stream.Read(bytes, 0, byteCount);
+			return bytes;
+		}
+
+		private byte[] ReadBytesForNumber(int byteCount)
+		{
+			byte[] bytes = ReadBytes(byteCount);
+			int halfCount = byteCount / 2;
+
+			for (int i = 0, j = byteCount - 1; i < halfCount; i++, j--)
+			{
+				byte temp = bytes[i];
+				bytes[i] = bytes[j];
+				bytes[j] = temp;
+			}
+
+			return bytes;
+		}
+
+		public byte ReadByte()
+		{
+			return (byte)stream.ReadByte();
+		}
+
+		public short ReadShort()
+		{
+			return BitConverter.ToInt16(ReadBytesForNumber(2), 0);
+		}
+
+		public ushort ReadUShort()
+		{
+			return BitConverter.ToUInt16(ReadBytesForNumber(2), 0);
+		}
+
+		public int ReadInt()
+		{
+			return BitConverter.ToInt32(ReadBytesForNumber(4), 0);
+		}
+
+		public uint ReadUInt()
+		{
+			return BitConverter.ToUInt32(ReadBytesForNumber(4), 0);
+		}
+
+		public string ReadDelimitedString(byte delimiter)
+		{
+			List<byte> bytes = new List<byte>();
+
+			while (!EndOfStream)
+			{
+				byte b = ReadByte();
+
+				if (b!=delimiter)
+				{
+					bytes.Add(b);
+				}
+				else
+				{
+					break;
+				}
+			}
+
+			return encoding.GetString(bytes.ToArray());
+		}
+
+		public string ReadString(int length)
+		{
+			byte[] bytes = ReadBytes(length);
+			return encoding.GetString(bytes);
+		}
+
+		public string ReadUShortLengthString()
+		{
+			ushort length = ReadUShort();
+			return ReadString(length);
+		}
+		
+		public string ReadIntLengthString()
+		{
+			int length = ReadInt();
+			return ReadString(length);
+		}
+
+		public void Seek(int distance)
+		{
+			stream.Seek(distance, SeekOrigin.Begin);
+		}
+
+		public void Move(int distance)
+		{
+			stream.Seek(distance, SeekOrigin.Current);
+		}
+
+		public void Close()
+		{
+			stream.Close();
+		}
+
+		public bool EndOfStream
+		{
+			get { return stream.Length - 1 == stream.Position; }
+		}
+	}
+}
--- a/Lang/StringManipulation.cs	Sat Apr 11 14:47:20 2009 +0000
+++ b/Lang/StringManipulation.cs	Sun Apr 19 11:31:47 2009 +0000
@@ -22,6 +22,24 @@
 		public static byte[] StringToBytes(string str)
 		{
 			return utf8.GetBytes(str);
+		}
+
+		public static string ByteArrayToHexString(byte[] arr)
+		{
+			return ByteArrayToHexString(arr, true);
+		}		                                          
+
+		public static string ByteArrayToHexString(byte[] arr, bool spaceBytes)
+		{
+			StringBuilder sb = new StringBuilder(arr.Length);
+			string format = (spaceBytes ? "{0,-3:x2}" : "{0:x2}");
+
+			foreach (byte b in arr)
+			{
+				sb.Append(String.Format(format, b));
+			}
+
+			return sb.ToString().TrimEnd();
 		}
 
 		public static string RemoveFromLast(string stringToTrim, char removeFrom)