zoukankan      html  css  js  c++  java
  • Blob未完成(待优化)

    /**************************************************/
    /*    Author   : Anby                            */
    /*    Using    :兼容powerBuilder中的Blob数据类型 */
    /*    DateTime :2013-08-04                       */
    /*    版本     :未知,功能尚未完全实现           */
    /**************************************************/
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
     
    namespace BolbTools 
    {
        public enum DateType { Date, Time, DateTime }
        public enum DataType { Boolean, Char, Date, DateTime, Decimal, Double, Int, String, Blob }
        public class Blob
        {
            #region 字段
            private List<byte> _arrList = new List<byte>();   //定义可变长的数组
    
            private double _maxLength = Math.Pow(2,32)-1;
            #endregion
    
            #region 属性
            public List<byte> ArrList
            {
                get { return _arrList; }
                set { _arrList = value; }
            }
    
            #endregion
    
            #region 方法
    
            /*获取str的char数组长度*/
            public static long Length(string str)
            {
               return str.ToCharArray().Length;    
            }
           
            /*获取blob对象的list长度   */
            public static long Length(Blob blob)
            {
                return blob._arrList.Count;
            }
    
            public void Add(byte b)
            {
                _arrList.Add(b);
            }
            
    
    
            public void InsertByte(int index,byte b)
            {
                _arrList.Insert(index,b);
            }
    
            public ulong InsertBlob(int index, Blob blob)
            {
                try
                {
                    _arrList.InsertRange(index, blob._arrList);
                }
                catch (ArgumentOutOfRangeException ex)
                {
                    for (int i = 0; i < index; i++)
                        _arrList.Add(0);
                    _arrList.InsertRange(index, blob._arrList);
                }
                return (ulong)(blob._arrList.Count + index);
            }
    
    
            /*从blob对象中截取从n开始,到blob.length结尾的数据*/
            public Blob GetBlob(Blob blob, int n)
            {
              return  GetBlob(blob,n,blob._arrList.Count);
            }
            /*从blob对象中截取从n开始,到length长度的数据*/
            public Blob GetBlob(Blob blob, int n,int length)
            {
                //@TODO 获取n-length的blob对象
    
                //定义空的Blob对象
                Blob resultBlob = new Blob();
    
                //如果索引值超出blob的范围,那么返回一个空的Blob对象
                if (n > blob._arrList.Count)
                {
                    return resultBlob;
                }
                //如果n+length>blob的长度,返回从n开始到blob.length的blob对象
                if ((n + length) > blob._arrList.Count)
                {
                    for (int i = n; i < blob._arrList.Count; i++)
                        resultBlob._arrList.Add(blob._arrList[i]);
                }
                else //否则返回从n开始length长度的blob对象
                {
                    for (int i = n; i < n+length; i++)
                        resultBlob._arrList.Add(blob._arrList[i]);
                }
                return resultBlob;
    
               
    
                return null;
            }
    
    
    
    
            public void RemoveAt(int index)
            {
                _arrList.RemoveAt(index);
            }
    
    
    
            /*将字符串转换为Bolb*/
            //Blob方法:将字符串转换为Blob对象--此方法中list不含有
            public static Blob  StrToBlob(string text)
            {
                Blob sub = new Blob();
                byte[] bytes = UnicodeEncoding.Default.GetBytes(text);
                foreach (byte b in bytes)
                {
                    sub.Add(b);
                } 
                return sub;
            }
    
            public static string BlobToStr(Blob blob)
            {
                //如果blob中的数据list长度为空 返回null
                if (blob._arrList.Count == 0) return null;
                 return  System.Text.UnicodeEncoding.Default.GetString(blob._arrList.ToArray());
            }
    
    
    
    
            public static ulong InsertBlob(ref Blob blob,int n,object data,DataType datatype)
            {
                Blob sub=new Blob();
                switch (datatype)
                {
                    case DataType.Boolean:
                        sub= ConvertToBlob(bool.Parse(data.ToString()));
                        break;
                    case DataType.Char:
                        sub=ConvertToBlob(char.Parse(data.ToString()));
                        break;
                    case DataType.Decimal:
                        sub = ConvertToBlob(decimal.Parse(data.ToString()));
                        break;
                    case DataType.Double:
                        sub=ConvertToBlob(double.Parse(data.ToString()));
                        break;
                    case DataType.Int:
                        sub = ConvertToBlob(int.Parse(data.ToString()));
                        break;
                    case DataType.String:
                        sub = ConvertToBlob(data.ToString());
                        break;
                    case DataType.Blob:
                        sub = (Blob)data;
                        break;
                }
                return blob.InsertBlob(n, sub);
            }
    
    
            #region  ConvertToBlob重载
            /*将字符串转换为Blob*/
            public static Blob ConvertToBlob(string text)
            {
                Blob sub = new Blob();
                sub = StrToBlob(text);
                sub.Add(0);//由于字符串中含有终止位
                return sub;
            }
    
            /*将一个字节转为为Blob*/
            public static Blob ConvertToBlob(char charValue)
            {
                return StrToBlob(charValue.ToString());
            }
    
            /*将Bool型数据转换为Blob*/
            public static Blob ConvertToBlob(bool boolValue)
            {
                return StrToBlob(boolValue.ToString());
            }
    
            /*将日期型数据转换为Blob*/
            public static Blob ConvertToBlob(DateTime date, DateType type)
            {
                Blob sub = new Blob();
                switch (type)
                {
                    case DateType.Date:
                        sub = StrToBlob(date.ToString("yyyy-MM-dd"));
                        break;
                    case DateType.Time:
                        sub = StrToBlob(date.ToString("hh-mm-ss"));
                        break;
                    default:
                        sub = StrToBlob(date.ToString("yyyy-MM-dd hh-mm-ss"));
                        break;
                }
                return sub;
            }
    
            /*将Double类型转换为Bolb*/
            public static Blob ConvertToBlob(double doubleValue)
            {
                return StrToBlob(doubleValue.ToString());
            }
    
            /*将Int类型转为Bolb*/
            public static Blob ConvertToBlob(int intValue)
            {
                return StrToBlob(intValue.ToString());
            }
    
            /*将decValue类型转为Bolb*/
            public static Blob ConvertToBlob(decimal decValue)
            {
                return StrToBlob(decValue.ToString());
            }
            #endregion
    
    
            #endregion
    
            #region 索引器
            /*定义索引器*/
            public byte this[int index]
            {
                get
                {   //检查索引范围
                    if (index < 0 || index >= _maxLength)
                    {
                        return 0;
                    }
                    else
                    {
                        return _arrList[index];
                    }
                }
                set
                {
                    if (!(index <= 0))
                    {
                        _arrList[index] = value;
                    }
                }
    
            }
            #endregion
    
        
        }  
    
    }
    View Code
  • 相关阅读:
    在前端眼中pc端和移动的开发区别
    两个对象的合并
    关于后台传来的json是含英文字母的string
    关于jquery的取消阻止默认事件
    练习:模态对话框
    JS之DOM节点增删改查与属性设置
    JS之DOM Event(事件)
    JS的DOM对象之DOM节点
    JS的history对象和location对象
    JS的BOM对象之window对象,利用window对象方法实现定时器
  • 原文地址:https://www.cnblogs.com/anbylau2130/p/3236348.html
Copyright © 2011-2022 走看看