private void Init(Stream stream, System.Text.Encoding encoding, int bufferSize)
 {
     this.stream   = stream;
     this.encoding = encoding;
     this.encoder  = encoding.GetEncoder();
     if (bufferSize < 0x80)
     {
         bufferSize = 0x80;
     }
     this.charBuffer = new char[bufferSize];
     this.byteBuffer = new byte[encoding.GetMaxByteCount(bufferSize)];
     this.charLen    = bufferSize;
     if (stream.CanSeek && (stream.Position > 0L))
     {
         this.haveWrittenPreamble = true;
     }
     this.closable = true;
     if (Mda.StreamWriterBufferedDataLost.Enabled)
     {
         string cs = null;
         if (Mda.StreamWriterBufferedDataLost.CaptureAllocatedCallStack)
         {
             cs = Environment.GetStackTrace(null, false);
         }
         this.mdaHelper = new MdaHelper(this, cs);
     }
 }
Пример #2
0
		public object GetRealObject (StreamingContext context)
		{
			if (this.realObject == null)
				this.realObject = this.encoding.GetEncoder ();

			return this.realObject;
		}
Пример #3
0
        static StaticUtils()
        {
            asciiDecoder = Encoding.ASCII.GetDecoder();

            utf8Encoder = Encoding.UTF8.GetEncoder();
            utf8Decoder = Encoding.UTF8.GetDecoder();
        }
 public XmlEncodedRawTextWriter(Stream stream, XmlWriterSettings settings) : this(settings)
 {
     this.stream = stream;
     this.encoding = settings.Encoding;
     this.bufChars = new char[0x1820];
     this.bufBytes = new byte[this.bufChars.Length];
     this.bufBytesUsed = 0;
     this.trackTextContent = true;
     this.inTextContent = false;
     this.lastMarkPos = 0;
     this.textContentMarks = new int[0x40];
     this.textContentMarks[0] = 1;
     this.charEntityFallback = new CharEntityEncoderFallback();
     this.encoding = (Encoding) settings.Encoding.Clone();
     this.encoding.EncoderFallback = this.charEntityFallback;
     this.encoder = this.encoding.GetEncoder();
     if (!stream.CanSeek || (stream.Position == 0L))
     {
         byte[] preamble = this.encoding.GetPreamble();
         if (preamble.Length != 0)
         {
             this.stream.Write(preamble, 0, preamble.Length);
         }
     }
     if (settings.AutoXmlDeclaration)
     {
         this.WriteXmlDeclaration(this.standalone);
         this.autoXmlDeclaration = true;
     }
 }
Пример #5
0
 /// <summary>
 /// 构造函数
 /// </summary>
 /// <param name="encoding">编码</param>
 public ByteBuffer(Encoding encoding)
 {
     this.BaseStream = new MemoryStream();
     this._buffer = new byte[0x10];
     this._encoding = encoding;
     this._encoder = this._encoding.GetEncoder();
 }
 internal int SetChars(long fieldOffset, char[] buffer, int bufferOffset, int length)
 {
     if (MetaDataUtilsSmi.IsAnsiType(this._metaData.SqlDbType))
     {
         if (this._encoder == null)
         {
             this._encoder = this._stateObj.Parser._defaultEncoding.GetEncoder();
         }
         byte[] bytes = new byte[this._encoder.GetByteCount(buffer, bufferOffset, length, false)];
         this._encoder.GetBytes(buffer, bufferOffset, length, bytes, 0, false);
         this.SetBytesNoOffsetHandling(fieldOffset, bytes, 0, bytes.Length);
         return(length);
     }
     if (this._isPlp)
     {
         if (!this._plpUnknownSent)
         {
             this._stateObj.Parser.WriteUnsignedLong(18446744073709551614L, this._stateObj);
             this._plpUnknownSent = true;
         }
         this._stateObj.Parser.WriteInt(length * ADP.CharSize, this._stateObj);
         this._stateObj.Parser.WriteCharArray(buffer, length, bufferOffset, this._stateObj);
         return(length);
     }
     if (SqlDbType.Variant == this._metaData.SqlDbType)
     {
         this._stateObj.Parser.WriteSqlVariantValue(new string(buffer, bufferOffset, length), length, 0, this._stateObj);
         return(length);
     }
     this._stateObj.Parser.WriteShort(length * ADP.CharSize, this._stateObj);
     this._stateObj.Parser.WriteCharArray(buffer, length, bufferOffset, this._stateObj);
     return(length);
 }
Пример #7
0
 internal static string GetMd5Sum(string str)
 {
     System.Text.Encoder encoder = Encoding.Unicode.GetEncoder();
     byte[] numArray             = new byte[str.Length * 2];
     encoder.GetBytes(str.ToCharArray(), 0, str.Length, numArray, 0, true);
     return(GetMd5Sum(numArray));
 }
 public MemoryPoolTextWriter(IMemoryPool memory)
 {
     _memory = memory;
     _textArray = _memory.AllocChar(_textLength);
     _dataArray = _memory.Empty;
     _encoder = Encoding.UTF8.GetEncoder();
 }
Пример #9
0
 // Protected default constructor that sets the output stream
 // to a null stream (a bit bucket).
 protected BinaryWriter()
 {
     OutStream = Stream.Null;
     _buffer = new byte[16];
     _encoding = EncodingCache.UTF8NoBOM;
     _encoder = _encoding.GetEncoder();
 }
Пример #10
0
 // Protected default constructor that sets the output stream
 // to a null stream (a bit bucket).
 protected BinaryWriter()
 {
     OutStream = Stream.Null;
     _buffer = new byte[16];
     _encoding = new UTF8Encoding(false, true);
     _encoder = _encoding.GetEncoder();
 }
        public HttpResponseStreamWriter(Stream stream, Encoding encoding, int bufferSize)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            if (!stream.CanWrite)
            {
                throw new ArgumentException(Resources.HttpResponseStreamWriter_StreamNotWritable, nameof(stream));
            }

            if (encoding == null)
            {
                throw new ArgumentNullException(nameof(encoding));
            }

            _stream = stream;
            Encoding = encoding;
            _charBufferSize = bufferSize;

            if (bufferSize < MinBufferSize)
            {
                bufferSize = MinBufferSize;
            }

            _encoder = encoding.GetEncoder();
            _byteBuffer = new byte[encoding.GetMaxByteCount(bufferSize)];
            _charBuffer = new char[bufferSize];
        }
Пример #12
0
 /// <summary>
 /// 构造函数
 /// </summary>
 public ByteBuffer()
 {
     this.BaseStream = new MemoryStream();
     this._buffer = new byte[0x10];
     this._encoding = Encoding.Default;//(false, true);
     this._encoder = this._encoding.GetEncoder();
 }
Пример #13
0
 // Protected default constructor that sets the output stream
 // to a null stream (a bit bucket).
 protected EndianWriter()
 {
     OutStream = Stream.Null;
     buffer = new byte[16];
     Encoding = new UTF8Encoding(false, true);
     encoder = Encoding.GetEncoder();
 }
 public MemoryPoolTextWriter(IMemoryPool memory)
     : base(CultureInfo.InvariantCulture)
 {
     _memory = memory;
     _textArray = _memory.AllocChar(_textLength);
     _dataArray = MemoryPool.EmptyArray;
     _encoder = Encoding.UTF8.GetEncoder();
 }
 internal TdsValueSetter(TdsParserStateObject stateObj, SmiMetaData md)
 {
     this._stateObj       = stateObj;
     this._metaData       = md;
     this._isPlp          = MetaDataUtilsSmi.IsPlpFormat(md);
     this._plpUnknownSent = false;
     this._encoder        = null;
 }
Пример #16
0
 public static int Hash(string data, Encoder enc)
 {
     var arr = data.ToCharArray();
     int count = enc.GetByteCount(arr, 0, arr.Length, false);
     var bytes = new byte[count];
     enc.GetBytes(arr, 0, arr.Length, bytes, 0, false);
     return Hash(bytes);
 }
 protected BinaryWriter()
 {
     this._tmpOneCharBuffer = new char[1];
     this.OutStream         = Stream.Null;
     this._buffer           = new byte[0x10];
     this._encoding         = new UTF8Encoding(false, true);
     this._encoder          = this._encoding.GetEncoder();
 }
 internal TdsValueSetter(TdsParserStateObject stateObj, SmiMetaData md)
 {
     this._stateObj = stateObj;
     this._metaData = md;
     this._isPlp = MetaDataUtilsSmi.IsPlpFormat(md);
     this._plpUnknownSent = false;
     this._encoder = null;
 }
 protected BinaryWriter()
 {
     this._tmpOneCharBuffer = new char[1];
     this.OutStream = Stream.Null;
     this._buffer = new byte[0x10];
     this._encoding = new UTF8Encoding(false, true);
     this._encoder = this._encoding.GetEncoder();
 }
Пример #20
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ByteBuffer"/> class.
 /// </summary>
 /// <param name="buffer">The buffer.</param>
 /// <param name="start">The start.</param>
 /// <param name="count">The count.</param>
 public ByteBuffer(byte[] buffer, int start, int count)
 {
     this.BaseStream = new MemoryStream(buffer, start, count, false);
     this.BaseStream.Position = 0;
     this._buffer = new byte[0x10];
     this._encoding = Encoding.Default;
     this._encoder = this._encoding.GetEncoder();
 }
 public HttpResponseStreamWriter(Stream stream, Encoding encoding, int bufferSize)
 {
     _stream = stream;
     Encoding = encoding;
     _encoder = encoding.GetEncoder();
     _charBufferSize = bufferSize;
     _charBuffer = new ArraySegment<char>(new char[bufferSize]);
     _byteBuffer = new ArraySegment<byte>(new byte[encoding.GetMaxByteCount(bufferSize)]);
 }
 public HttpResponseStreamWriter([NotNull] Stream stream, [NotNull] Encoding encoding, int bufferSize)
 {
     _stream = stream;
     Encoding = encoding;
     _encoder = encoding.GetEncoder();
     _charBufferSize = bufferSize;
     _charBuffer = new char[bufferSize];
     _byteBuffer = new byte[encoding.GetMaxByteCount(bufferSize)];
 }
Пример #23
0
		public LineWriter(Stream stream, int bufferSize) {
			if (bufferSize < 1024)
				bufferSize = 1024;

			this.stream = stream;
			writeBuffer = new byte[bufferSize];
			writeBufferOffset = 0;
			encoder = Encoding.UTF8.GetEncoder();
		}
Пример #24
0
 public TextReaderStream(TextReader textReader, Encoding encoding, int bufferSize = 4096)
 {
     _textReader = textReader;
     _encoding = encoding;
     _maxByteCountPerChar = _encoding.GetMaxByteCount(1);
     _encoder = encoding.GetEncoder();
     if (bufferSize <= 0) throw new ArgumentOutOfRangeException("bufferSize", "zero or negative");
     _charBuffer = new char[bufferSize];
 }
Пример #25
0
		/// <summary>
		/// Loads the [HKEY_LOCAL_MACHINE\SOFTWARE\HolodeckEE\PaneSettings] registry value from the specified file
		/// </summary>
		/// <param name="inputFilePath">the path of the file to read from</param>
		public static void LoadPaneSettings (string inputFilePath)
		{
			System.IO.StreamReader inputFile = new System.IO.StreamReader (inputFilePath);
			string fileContent = inputFile.ReadToEnd ();
			char[] newRegPaneSettings = fileContent.ToCharArray ();
			System.Text.Encoder encoder = System.Text.Encoding.Unicode.GetEncoder ();
			byte[] encodedPaneSettings = new byte[newRegPaneSettings.Length * 2];
			encoder.GetBytes (newRegPaneSettings, 0, newRegPaneSettings.Length, encodedPaneSettings, 0, true);
			Microsoft.Win32.Registry.LocalMachine.OpenSubKey ("SOFTWARE\\HolodeckEE", true).SetValue ("PaneSettings", encodedPaneSettings);
		}
Пример #26
0
        public static byte[] EncodeString(string vValue)
        {
            string str = vValue;

            System.Text.Encoder encoder = m_encoding.GetEncoder();
            char[] chars = str.ToCharArray();
            byte[] bytes = new byte[encoder.GetByteCount(chars, 0, chars.Length, false)];
            encoder.GetBytes(chars, 0, chars.Length, bytes, 0, true);
            return(Mogo.RPC.Utils.FillLengthHead(bytes));
        }
        public MemoryPoolTextWriter(IMemoryPool memory)
        {
            _memory = memory;
            _textArray = _memory.AllocChar(_textLength);
            _dataArray = _memory.Empty;
            _encoder = Encoding.UTF8.GetEncoder();

            // Default new lines to \n
            NewLine = "\n";
        }
Пример #28
0
        private int                     _currentOffset; // for chunking, verify that caller is using correct offsets
#endif

        #endregion

        #region Exposed Construct/factory methods

        internal TdsValueSetter(TdsParserStateObject stateObj, SmiMetaData md) {
            _stateObj = stateObj;
            _metaData = md;
            _isPlp = MetaDataUtilsSmi.IsPlpFormat(md);
            _plpUnknownSent = false;
            _encoder = null;
#if DEBUG
            _currentOffset = 0;
#endif
        }
Пример #29
0
        private int GetByteCount(char[] chars, int index, int count,
            bool flush, Encoder encoder) {
            if (chars == null) {
                throw new ArgumentNullException("chars", 
                    Environment.GetResourceString("ArgumentNull_Array"));
            }
            if (index < 0 || count < 0) {
                throw new ArgumentOutOfRangeException((index<0 ? "index" : "count"),
                    Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
            }
            if (chars.Length - index < count) {
                throw new ArgumentOutOfRangeException("chars",
                      Environment.GetResourceString("ArgumentOutOfRange_IndexCountBuffer"));
            }
            int bitCount = -1;
            if (encoder != null) bitCount = encoder.bitCount;
            int end = index + count;
            int byteCount = 0;
            while (index < end && byteCount >= 0) {
                int c = chars[index++];
                if (c < 0x80 && directEncode[c]) {
                    if (bitCount >= 0) {
                        if (bitCount > 0) byteCount++;
                        byteCount++;
                        bitCount = -1;
                    }
                    byteCount++;
                }
                else if (bitCount < 0 && c == '+') {
                    byteCount += 2;
                }
                else {
                    if (bitCount < 0) {
                        byteCount++;
                        bitCount = 0;
                    }
                    bitCount += 16;
                    while (bitCount >= 6) {
                        bitCount -= 6;
                        byteCount++;
                    }
                }
            }
            if (flush && bitCount >= 0) {
                if (bitCount > 0) byteCount++;
                byteCount++;
            }

            // Check for overflows.
            if (byteCount < 0)
                throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_GetByteCountOverflow"));

            return byteCount;
        }
Пример #30
0
        private TranscodingStream(Stream stream, Encoding outEncoding)
        {
            if (stream == null)
                throw new ArgumentNullException("stream");

            if (outEncoding == null)
                throw new ArgumentNullException("outEncoding");

            this._stream = stream;
            this._encoder = outEncoding.GetEncoder();
        }
Пример #31
0
 public SourceTextStream(SourceText source, int bufferSize = 2048)
 {
     _source = source;
     _encoder = source.Encoding.GetEncoder();
     _sourceOffset = 0;
     _position = 0;
     _charBuffer = new char[Math.Min(bufferSize, _source.Length)];
     _bufferOffset = 0;
     _bufferUnreadChars = 0;
     _preambleWritten = false;
 }
 public HttpResponseStreamWriter(Stream stream, Encoding encoding, LeasedArraySegment<char> leasedCharBuffer, LeasedArraySegment<byte> leasedByteBuffer)
 {
     _stream = stream;
     Encoding = encoding;
     _encoder = encoding.GetEncoder();
     _charBufferSize = leasedCharBuffer.Data.Count;
     _charBuffer = leasedCharBuffer.Data;
     _byteBuffer = leasedByteBuffer.Data;
     _leasedCharBuffer = leasedCharBuffer;
     _leasedByteBuffer = leasedByteBuffer;
 }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <param name="encoding"></param>
        public SHA1TextReader(TextReader input, Encoding encoding)
            : base()
        {
            this.input = input;
            this.encoder = encoding.GetEncoder();

            this.sha1 = SHA1.Create();

            this.ibuf = new char[bufferSize];
            this.obuf = new byte[bufferSize];
            this.ibufIndex = 0;
        }
 private void InitForWriting(Stream outputStream, Encoding writeEncoding)
 {
     this.encoding     = writeEncoding;
     this.stream       = new BufferedStream(outputStream);
     this.encodingCode = GetSupportedEncoding(writeEncoding);
     if (this.encodingCode != SupportedEncoding.UTF8)
     {
         this.EnsureBuffers();
         this.dec = ValidatingUTF8.GetDecoder();
         this.enc = this.encoding.GetEncoder();
     }
 }
Пример #35
0
 static void Main(string[] arguments)
 {
     if (arguments.Length != 1)
     {
         Console.WriteLine("Usage:");
         Console.WriteLine("<path to release configuration file>");
     }
     var configuration = JsonFile.Read<GeneralConfiguration>();
     var encoder = new Encoder(configuration);
     string releaseConfigurationPath = arguments[0];
     encoder.Run(releaseConfigurationPath);
 }
Пример #36
0
 internal void UpdateResponseEncoding()
 {
     if (this._responseEncodingUpdated && (this._charBufferLength != this._charBufferFree))
     {
         this.FlushCharBuffer(true);
     }
     this._responseEncoding = this._response.ContentEncoding;
     this._responseEncoder  = this._response.ContentEncoder;
     this._responseCodePage = this._responseEncoding.CodePage;
     this._responseCodePageIsAsciiCompat = CodePageUtils.IsAsciiCompatibleCodePage(this._responseCodePage);
     this._responseEncodingUpdated       = true;
 }
Пример #37
0
        public unsafe BufferedBinaryWriter(Stream output, Encoding encoding)
            : base(output, encoding)
        {
            this.memoryPage = Pool.GetPage();

            this.basePtr = this.memoryPage.BasePtr;
            this.endPtr = this.memoryPage.EndPtr;

            this.ptr = this.basePtr;
            this.encoding = encoding;
            this.encoder = encoding.GetEncoder();
            this.charMaxByteCount = encoding.IsSingleByte ? 1 : encoding.GetMaxByteCount(1);
        }
Пример #38
0
        public AsyncStreamWriter(Stream stream, Encoding encoding, int initialBufferCapacity = DefaultIniBufferCapacity, int maxBufferCapacity = DefaultMaxBufferCapacity)
        {
            if (initialBufferCapacity < 4)
                throw new ArgumentOutOfRangeException("initialBufferCapacity");

            if (initialBufferCapacity > maxBufferCapacity)
                maxBufferCapacity = initialBufferCapacity;

            _stream = stream;
            _buffer = new byte[initialBufferCapacity];
            _maxBufferCapacity = maxBufferCapacity;
            _encoder = encoding.GetEncoder();
        }
Пример #39
0
        public unsafe BufferedBinaryWriter(Stream output, Encoding encoding, bool leaveOpen)
            : base(output, encoding, leaveOpen)
        {
            this.memoryPage = Pool.GetPage();

            this.basePtr = this.memoryPage.BasePtr;
            this.endPtr  = this.memoryPage.EndPtr;

            this.ptr              = this.basePtr;
            this.encoding         = encoding;
            this.encoder          = encoding.GetEncoder();
            this.charMaxByteCount = encoding.IsSingleByte ? 1 : encoding.GetMaxByteCount(1);
        }
Пример #40
0
     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(Environment.GetResourceString("Argument_StreamNotWritable"));
 
         OutStream = output;
         _buffer = new byte[16];
         _encoding = encoding;
         _encoder = _encoding.GetEncoder();
     }
Пример #41
0
        public unsafe HeapBinaryWriter(int capacity, Encoding encoding)
        {
            this.length = (capacity / PageSize + 1) * PageSize;
#if !HT4O_SERIALIZATION
            this.buffer = Hypertable.Heap.Alloc(this.length);
#else
            this.buffer = Marshal.AllocHGlobal(this.length);
#endif
            this.ptr = (byte *)this.buffer;

            this.encoding         = encoding;
            this.encoder          = encoding.GetEncoder();
            this.charMaxByteCount = encoding.IsSingleByte ? 1 : encoding.GetMaxByteCount(1);
        }
Пример #42
0
        public static string UrlEncode(string str, string encode)
        {
            int factor = 0;

            if (encode == "UTF-8")
            {
                factor = 3;
            }
            if (encode == "GB2312")
            {
                factor = 2;
            }
            //不需要编码的字符

            string okChar = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.";

            System.Text.Encoder encoder = System.Text.Encoding.GetEncoding(encode).GetEncoder();
            char[] c1 = str.ToCharArray();
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            //一个字符一个字符的编码

            for (int i = 0; i < c1.Length; i++)
            {
                //不需要编码

                if (okChar.IndexOf(c1[i]) > -1)
                {
                    sb.Append(c1[i]);
                }
                else
                {
                    byte[] c2 = new byte[factor];
                    int    charUsed, byteUsed; bool completed;

                    encoder.Convert(c1, i, 1, c2, 0, factor, true, out charUsed, out byteUsed, out completed);

                    foreach (byte b in c2)
                    {
                        if (b != 0)
                        {
                            sb.AppendFormat("%{0:X}", b);
                        }
                    }
                }
            }
            return(sb.ToString().Trim());
        }
Пример #43
0
        static string GetMd5Sum(string productIdentifier)
        {
            System.Text.Encoder enc = System.Text.Encoding.Unicode.GetEncoder();
            byte[] unicodeText      = new byte[productIdentifier.Length * 2];
            enc.GetBytes(productIdentifier.ToCharArray(), 0, productIdentifier.Length, unicodeText, 0, true);
            MD5 md5 = new MD5CryptoServiceProvider();

            byte[] result = md5.ComputeHash(unicodeText);

            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < result.Length; i++)
            {
                sb.Append(result[i].ToString("X2"));
            }
            return(sb.ToString());
        }
 public BinaryWriter(Stream output, Encoding encoding)
 {
     this._tmpOneCharBuffer = new char[1];
     if (output == null)
     {
         throw new ArgumentNullException("output");
     }
     if (encoding == null)
     {
         throw new ArgumentNullException("encoding");
     }
     if (!output.CanWrite)
     {
         throw new ArgumentException(Environment.GetResourceString("Argument_StreamNotWritable"));
     }
     this.OutStream = output;
     this._buffer   = new byte[0x10];
     this._encoding = encoding;
     this._encoder  = this._encoding.GetEncoder();
 }
 internal void SetCharsLength(long length)
 {
     if (0L == length)
     {
         if (this._isPlp)
         {
             this._stateObj.Parser.WriteLong(0L, this._stateObj);
             this._plpUnknownSent = true;
         }
         else
         {
             this._stateObj.Parser.WriteShort(0, this._stateObj);
         }
     }
     if (this._plpUnknownSent)
     {
         this._stateObj.Parser.WriteInt(0, this._stateObj);
         this._plpUnknownSent = false;
     }
     this._encoder = null;
 }
Пример #46
0
        /// <summary>
        /// Add a standard EXIF field to the image
        /// </summary>
        /// <param name="image"></param>
        /// <param name="field"></param>
        /// <param name="fieldValue"></param>
        private static void WriteEXIFField(Image image, ExifField field, string fieldValue)
        {
            Encoding asciiEncoding = new ASCIIEncoding();

            System.Text.Encoder encoder = asciiEncoding.GetEncoder();
            char[] tagTextChars         = fieldValue.ToCharArray();
            int    byteCount            = encoder.GetByteCount(tagTextChars, 0, tagTextChars.Length, true);

            byte[] tagTextBytes = new byte[byteCount];
            encoder.GetBytes(tagTextChars, 0, tagTextChars.Length, tagTextBytes, 0, true);

            if (image.PropertyItems != null && image.PropertyItems.Length > 0)
            {
                PropertyItem propertyItem = image.PropertyItems[0];
                propertyItem.Id    = (int)field;
                propertyItem.Type  = 2; // ASCII
                propertyItem.Len   = tagTextBytes.Length;
                propertyItem.Value = tagTextBytes;
                image.SetPropertyItem(propertyItem);
            }
        }
Пример #47
0
 public EncodingStreamWrapper(Stream stream, Encoding encoding, bool emitBOM)
 {
     this.byteBuffer   = new byte[1];
     this.isReading    = false;
     this.encoding     = encoding;
     this.stream       = new BufferedStream(stream);
     this.encodingCode = GetSupportedEncoding(encoding);
     if (this.encodingCode != SupportedEncoding.UTF8)
     {
         this.EnsureBuffers();
         this.dec = ValidatingUTF8.GetDecoder();
         this.enc = this.encoding.GetEncoder();
         if (emitBOM)
         {
             byte[] preamble = this.encoding.GetPreamble();
             if (preamble.Length > 0)
             {
                 this.stream.Write(preamble, 0, preamble.Length);
             }
         }
     }
 }
        // GET api/<controller>
        public HttpResponseMessage Get(string filename, string ext)
        {
            if (filename == null)
            {
                return(new HttpResponseMessage(HttpStatusCode.BadRequest));
            }

            string filePath = HostingEnvironment.MapPath("~/Videos/") + filename + "." + ext;

            if (Request.Headers.Range != null)
            {
                try
                {
                    System.Text.Encoder stringEncoder = Encoding.UTF8.GetEncoder();
                    byte[] stringBytes = new byte[stringEncoder.GetByteCount(filePath.ToCharArray(), 0, filePath.Length, true)];
                    stringEncoder.GetBytes(filePath.ToCharArray(), 0, filePath.Length, stringBytes, 0, true);
                    MD5CryptoServiceProvider MD5Enc = new MD5CryptoServiceProvider();
                    string hash = BitConverter.ToString(MD5Enc.ComputeHash(stringBytes)).Replace("-", string.Empty);

                    HttpResponseMessage partialResponse = Request.CreateResponse(HttpStatusCode.PartialContent);
                    partialResponse.Headers.AcceptRanges.Add("bytes");
                    partialResponse.Headers.ETag = new EntityTagHeaderValue("\"" + hash + "\"");
                    var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                    partialResponse.Content = new ByteRangeStreamContent(stream, Request.Headers.Range, new MediaTypeHeaderValue("video/mp4"));

                    return(partialResponse);
                }
                catch (Exception ex)
                {
                    return(new HttpResponseMessage(HttpStatusCode.InternalServerError));
                }
            }
            else
            {
                return(new HttpResponseMessage(HttpStatusCode.RequestedRangeNotSatisfiable));
            }
        }
 protected override void Dispose(bool disposing)
 {
     try
     {
         if ((this.stream != null) && (disposing || (!this.Closable && (this.stream is __ConsoleStream))))
         {
             this.Flush(true, true);
             if (this.mdaHelper != null)
             {
                 GC.SuppressFinalize(this.mdaHelper);
             }
         }
     }
     finally
     {
         if (this.Closable && (this.stream != null))
         {
             try
             {
                 if (disposing)
                 {
                     this.stream.Close();
                 }
             }
             finally
             {
                 this.stream     = null;
                 this.byteBuffer = null;
                 this.charBuffer = null;
                 this.encoding   = null;
                 this.encoder    = null;
                 this.charLen    = 0;
                 base.Dispose(disposing);
             }
         }
     }
 }
Пример #50
0
        public static string Utf8Encode(this string url)
        {
            int factor = 3;

            //不需要编码的字符
            System.Text.Encoder encoder = System.Text.Encoding.GetEncoding("UTF-8").GetEncoder();
            char[] c1 = url.ToCharArray();
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            //一个字符一个字符的编码
            for (int i = 0; i < c1.Length; i++)
            {
                //不需要编码
                if (c1[i] < 128)
                {
                    sb.Append(c1[i]);
                }
                else
                {
                    byte[] c2 = new byte[factor];
                    int    charUsed, byteUsed; bool completed;

                    encoder.Convert(c1, i, 1, c2, 0, factor, true, out charUsed, out byteUsed, out completed);

                    foreach (byte b in c2)
                    {
                        if (b != 0)
                        {
                            sb.AppendFormat("%{0:X}", b);
                        }
                    }
                }
            }
            return(sb.ToString().Trim());

            //            return HttpUtility.UrlEncode(url, System.Text.Encoding.UTF8);
        }
 internal abstract void AppendEncodedChars(char[] data, int offset, int size, System.Text.Encoder encoder, bool flushEncoder);
 /// <summary>
 /// Encodes a block of input characters to bytes
 /// </summary>
 /// <param name="encoder">The encoder performing the encoding operation</param>
 /// <param name="input">The input characters</param>
 /// <param name="output">The span that receives the output</param>
 /// <param name="flush">True to flush the encoder, otherwise False</param>
 /// <param name="charsRead">Receives the number of characters read</param>
 /// <param name="bytesWritten">Receives the number of bytes written</param>
 /// <param name="isCompleted">Receives whether all output bytes have been written</param>
 public static unsafe void Convert(this Encoder encoder, ReadOnlySpan <char> input, Span <byte> output, bool flush, out int charsRead, out int bytesWritten, out bool isCompleted)
 {
     fixed(char *pInput = &MemoryMarshal.GetReference(input))
     fixed(byte *pOutput = &MemoryMarshal.GetReference(output))
     encoder.Convert(pInput, input.Length, pOutput, output.Length, flush, out charsRead, out bytesWritten, out isCompleted);
 }
 public static int GetBytes(Encoder encoder, ReadOnlySpan <char> input, Span <byte> output, bool flush) => encoder.GetBytes(input, output, flush);
 /// <summary>
 /// Gets the encoded bytes as a Memory from a character Span
 /// </summary>
 /// <param name="encoder">The encoder performing the encoding operation</param>
 /// <param name="input">The input characters</param>
 /// <param name="output">The span that receives the output</param>
 /// <param name="flush">True to flush the encoder, otherwise False</param>
 /// <returns>The number of bytes written</returns>
 public static unsafe int GetBytes(this Encoder encoder, ReadOnlySpan <char> input, Span <byte> output, bool flush)
 {
     fixed(char *pInput = &MemoryMarshal.GetReference(input))
     fixed(byte *pOutput = &MemoryMarshal.GetReference(output))
     return(encoder.GetBytes(pInput, input.Length, pOutput, output.Length, flush));
 }
        internal override void AppendEncodedChars(char[] data, int offset, int size, System.Text.Encoder encoder, bool flushEncoder)
        {
            int num = UnsafeAppendEncodedChars(data, offset, size, this._data, base._size - base._free, base._free, encoder, flushEncoder);

            base._free -= num;
        }
        private static unsafe int UnsafeAppendEncodedChars(char[] src, int srcOffset, int srcSize, IntPtr dest, int destOffset, int destSize, System.Text.Encoder encoder, bool flushEncoder)
        {
            int   num   = 0;
            byte *bytes = (byte *)(((void *)dest) + destOffset);

            fixed(char *chRef = src)
            {
                num = encoder.GetBytes(chRef + srcOffset, srcSize, bytes, destSize, flushEncoder);
            }

            return(num);
        }
Пример #57
0
    public void CreateTableClass(string tableName, string text)
    {
        string[][] textData = ReadDataFromText(text);
        Dictionary <string, string> typex00 = new Dictionary <string, string>()
        {
            { "vec", "Vector3" },
            { "int", "int" },
            { "float", "float" },
            { "str", "string" },
            { "note", "string" },
            { "describe", "string" }
        };

        Dictionary <string, string> .KeyCollection keys = typex00.Keys;
        for (int i = 0; i < textData[2].Length; i++)
        {
            bool isArray   = textData[2][i].Contains("[]");
            bool isArray2D = isArray ? textData[2][i].Contains("[][]") : false;
            foreach (string str in keys)
            {
                textData[2][i] = textData[2][i].ToLower().Contains(str) ? typex00[str] : textData[2][i];
            }
            textData[2][i] = isArray ? (textData[2][i] + "[]") : textData[2][i];
            textData[2][i] = isArray2D ? (textData[2][i] + "[]") : textData[2][i];
        }
        byte[] byData;
        char[] writeCharData;
        string needStringx00 = "using UnityEngine;using System.Collections;using System.Collections.Generic;\n";
        string needStringx01 = "public class ";
        string className     = tableName;
        string classDataName = className + "Data";
        string baseName      = ": ScriptableObject";
        string startSymbol   = "\n{\n";
        string needStringx02 = "\tpublic List <" + classDataName + "> table = new List<" + classDataName + ">() ;\n";
        string endSymbol     = "\n}\n";
        string baseNamex02   = "";
        string needStringx03 = "[System.Serializable]\npublic class " + classDataName + baseNamex02 + startSymbol;
        string properties    = "";
        string formater;

        for (int i = 0; i < textData[2].Length; i++)
        {
            string dataType   = textData[2][i].TrimEnd('\r');
            string dataOrigin = textData[1][i].TrimEnd('\r');
            if (textData[2][i].Contains("[][]"))
            {
                if (textData[2][i].Contains("int"))
                {
                    formater = "ReadInt32Array2D";
                }
                else if (textData[2][i].Contains("float"))
                {
                    formater = "ReadFloatArray2D";
                }
                else
                {
                    formater = "ReadStringArray2D";
                }
                properties += "\tpublic string s_" + dataOrigin + ";\n"
                              + "\tprivate " + dataType + dataOrigin + "_=null;\n"
                              + "\tpublic " + dataType + " " + dataOrigin + "{get{if(" + dataOrigin + "_==null)" + dataOrigin + "_=ysDataFormater." + formater + "(s_" + dataOrigin + ");return " + dataOrigin + "_;}set{}}\n";
            }
            else
            {
                properties += "\tpublic " + dataType + " " + dataOrigin + ";\n";
            }
        }
        properties.TrimEnd('\n');
        string writeString = needStringx00 + needStringx01 + className + baseName + startSymbol + needStringx02 + endSymbol + needStringx03 + properties + "}\n";

#if !UNITY_EDITOR_OSX
        writeString.Replace("\n", "\r\n");
#endif
        //string s_LastCreateClassText = writeString;
        using (FileStream fs = System.IO.File.Create(data.paths.classSccriptPath + tableName + ".cs"))
        {
            writeCharData = writeString.ToCharArray();
            byData        = new byte[writeCharData.Length];
            System.Text.Encoder e = System.Text.Encoding.UTF8.GetEncoder();
            e.GetBytes(writeCharData, 0, writeCharData.Length, byData, 0, true);
            fs.Seek(0, SeekOrigin.Begin);
            fs.Write(byData, 0, byData.Length);
            fs.Close();
        }
    }
Пример #58
0
        private int GetByteCount(char[] chars, int index, int count,
                                 bool flush, Encoder encoder)
        {
            if (chars == null)
            {
                throw new ArgumentNullException("chars",
                                                Environment.GetResourceString("ArgumentNull_Array"));
            }
            if (index < 0 || count < 0)
            {
                throw new ArgumentOutOfRangeException((index < 0 ? "index" : "count"),
                                                      Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
            }
            if (chars.Length - index < count)
            {
                throw new ArgumentOutOfRangeException("chars",
                                                      Environment.GetResourceString("ArgumentOutOfRange_IndexCountBuffer"));
            }
            int bitCount = -1;

            if (encoder != null)
            {
                bitCount = encoder.bitCount;
            }
            int end       = index + count;
            int byteCount = 0;

            while (index < end && byteCount >= 0)
            {
                int c = chars[index++];
                if (c < 0x80 && directEncode[c])
                {
                    if (bitCount >= 0)
                    {
                        if (bitCount > 0)
                        {
                            byteCount++;
                        }
                        byteCount++;
                        bitCount = -1;
                    }
                    byteCount++;
                }
                else if (bitCount < 0 && c == '+')
                {
                    byteCount += 2;
                }
                else
                {
                    if (bitCount < 0)
                    {
                        byteCount++;
                        bitCount = 0;
                    }
                    bitCount += 16;
                    while (bitCount >= 6)
                    {
                        bitCount -= 6;
                        byteCount++;
                    }
                }
            }
            if (flush && bitCount >= 0)
            {
                if (bitCount > 0)
                {
                    byteCount++;
                }
                byteCount++;
            }

            // Check for overflows.
            if (byteCount < 0)
            {
                throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_GetByteCountOverflow"));
            }

            return(byteCount);
        }
Пример #59
0
        private int GetBytes(char[] chars, int charIndex, int charCount,
                             byte[] bytes, int byteIndex, bool flush, Encoder encoder)
        {
            if (chars == null || bytes == null)
            {
                throw new ArgumentNullException((chars == null ? "chars" : "bytes"),
                                                Environment.GetResourceString("ArgumentNull_Array"));
            }
            if (charIndex < 0 || charCount < 0)
            {
                throw new ArgumentOutOfRangeException((charIndex < 0 ? "charIndex" : "charCount"),
                                                      Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
            }
            if (chars.Length - charIndex < charCount)
            {
                throw new ArgumentOutOfRangeException("chars",
                                                      Environment.GetResourceString("ArgumentOutOfRange_IndexCountBuffer"));
            }
            if (byteIndex < 0 || byteIndex > bytes.Length)
            {
                throw new ArgumentOutOfRangeException("byteIndex",
                                                      Environment.GetResourceString("ArgumentOutOfRange_Index"));
            }
            int bits     = 0;
            int bitCount = -1;

            if (encoder != null)
            {
                bits     = encoder.bits;
                bitCount = encoder.bitCount;
            }
            int charEnd   = charIndex + charCount;
            int byteStart = byteIndex;

            try {
                while (charIndex < charEnd)
                {
                    int c = chars[charIndex++];
                    if (c < 0x80 && directEncode[c])
                    {
                        if (bitCount >= 0)
                        {
                            if (bitCount > 0)
                            {
                                bytes[byteIndex++] = base64Bytes[bits << 6 - bitCount & 0x3F];
                            }
                            bytes[byteIndex++] = (byte)'-';
                            bitCount           = -1;
                        }
                        bytes[byteIndex++] = (byte)c;
                    }
                    else if (bitCount < 0 && c == '+')
                    {
                        bytes[byteIndex++] = (byte)'+';
                        bytes[byteIndex++] = (byte)'-';
                    }
                    else
                    {
                        if (bitCount < 0)
                        {
                            bytes[byteIndex++] = (byte)'+';
                            bitCount           = 0;
                        }
                        bits      = bits << 16 | c;
                        bitCount += 16;
                        while (bitCount >= 6)
                        {
                            bitCount          -= 6;
                            bytes[byteIndex++] = base64Bytes[bits >> bitCount & 0x3F];
                        }
                    }
                }
                if (flush && bitCount >= 0)
                {
                    if (bitCount > 0)
                    {
                        bytes[byteIndex++] = base64Bytes[bits << 6 - bitCount & 0x3F];
                    }
                    bytes[byteIndex++] = (byte)'-';
                    bitCount           = -1;
                }
            }
            catch (IndexOutOfRangeException) {
                throw new ArgumentException(Environment.GetResourceString("Argument_ConversionOverflow"));
            }
            if (encoder != null)
            {
                encoder.bits     = bits;
                encoder.bitCount = bitCount;
            }
            return(byteIndex - byteStart);
        }
 /// <summary>
 /// Determines the number of bytes that result from an encoding operation
 /// </summary>
 /// <param name="encoder">The encoder performing the encoding operation</param>
 /// <param name="span">The input characters</param>
 /// <param name="flush">True to flush the encoder, otherwise False</param>
 /// <returns>The number of bytes that would be output</returns>
 public static unsafe int GetByteCount(this Encoder encoder, ReadOnlySpan <char> span, bool flush)
 {
     fixed(char *pSpan = &MemoryMarshal.GetReference(span))
     return(encoder.GetByteCount(pSpan, span.Length, flush));
 }