Beispiel #1
0
 /// <summary>
 /// 设置输出数据
 /// </summary>
 /// <param name="charStream"></param>
 /// <param name="encoding"></param>
 internal unsafe void SetBody(CharStream charStream, ref EncodingCache encoding)
 {
     if (charStream.Data.CurrentIndex == 0)
     {
         SetBody();
     }
     else
     {
         freeBody();
         int size = encoding.GetByteCountNotNull(charStream);
         AutoCSer.SubBuffer.Pool.GetBuffer(ref SubBuffer, size);
         if (SubBuffer.PoolBuffer.Pool == null)
         {
             encoding.WriteBytes(charStream, Body.Array = SubBuffer.Buffer);
             SubBuffer.Buffer = null;
             Type             = ResponseType.ByteArray;
         }
         else
         {
             Body.Set(SubBuffer.Buffer, SubBuffer.StartIndex, size);
             encoding.WriteBytes(charStream, ref Body);
             Type = ResponseType.SubBuffer;
         }
     }
 }
Beispiel #2
0
 /// <summary>
 /// 设置输出数据
 /// </summary>
 /// <param name="value"></param>
 /// <param name="isAscii"></param>
 /// <param name="encoding"></param>
 internal unsafe void SetBody(string value, bool isAscii, ref EncodingCache encoding)
 {
     if (value.Length == 0)
     {
         SetBody();
     }
     else
     {
         freeBody();
         if (isAscii && encoding.IsCompatibleAscii != 0)
         {
             int size = value.Length;
             AutoCSer.SubBuffer.Pool.GetBuffer(ref SubBuffer, size);
             fixed(char *textFixed = value)
             fixed(byte *bufferFixed = SubBuffer.GetFixedBuffer())
             {
                 if (SubBuffer.PoolBuffer.Pool == null)
                 {
                     Body.Array = SubBuffer.Buffer;
                     AutoCSer.Extensions.StringExtension.WriteBytes(textFixed, size, bufferFixed);
                     SubBuffer.Buffer = null;
                     Type             = ResponseType.ByteArray;
                 }
                 else
                 {
                     Body.Set(SubBuffer.Buffer, SubBuffer.StartIndex, size);
                     AutoCSer.Extensions.StringExtension.WriteBytes(textFixed, size, bufferFixed + Body.Start);
                     Type = ResponseType.SubBuffer;
                 }
             }
         }
         else
         {
             int size = encoding.GetByteCountNotNull(value);
             AutoCSer.SubBuffer.Pool.GetBuffer(ref SubBuffer, size);
             if (SubBuffer.PoolBuffer.Pool == null)
             {
                 encoding.WriteBytesNotEmpty(value, Body.Array = SubBuffer.Buffer);
                 SubBuffer.Buffer = null;
                 Type             = ResponseType.ByteArray;
             }
             else
             {
                 Body.Set(SubBuffer.Buffer, SubBuffer.StartIndex, size);
                 encoding.WriteBytesNotEmpty(value, Body.Array, Body.Start);
                 Type = ResponseType.SubBuffer;
             }
         }
     }
 }
        /// <summary>
        /// 写入字符串
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        internal long WriteNotEmpty(string value)
        {
            int dataLength = encoding.GetByteCountNotNull(value);

            Monitor.Enter(bufferLock);
            if (isDisposed || isError)
            {
                Monitor.Exit(bufferLock);
                return(-1);
            }
            long fileBufferLength = this.fileBufferLength;
            bool isWritting       = this.isWritting;

            if (buffer.IsBuffer)
            {
                if (buffer.CanWrite(dataLength))
                {
                    buffer.Write(value, ref encoding);
                    this.fileBufferLength += dataLength;
                    this.isWritting        = true;
                    Monitor.Exit(bufferLock);
                }
                else
                {
                    try
                    {
                        buffers.PrepLength(2);
                        buffers.Array[buffers.Length++] = buffer;
                        buffer.Clear();
                        buffers.Array[buffers.Length++].WriteNotPool(value, new byte[dataLength], ref encoding);
                        this.fileBufferLength += dataLength;
                        this.isWritting        = true;
                    }
                    finally { Monitor.Exit(bufferLock); }
                }
            }
            else
            {
                try
                {
                    if (dataLength < bufferPool.Size)
                    {
                        buffer.Get(bufferPool);
                        buffer.Write(value, ref encoding);
                    }
                    else
                    {
                        buffers.PrepLength(1);
                        buffers.Array[buffers.Length++].WriteNotPool(value, new byte[dataLength], ref encoding);
                    }
                    this.fileBufferLength += dataLength;
                    this.isWritting        = true;
                }
                finally { Monitor.Exit(bufferLock); }
            }
            if (!isWritting)
            {
                AutoCSer.Threading.ThreadPool.Tiny.FastStart(this, AutoCSer.Threading.Thread.CallType.FileStreamWriteFile);
            }
            return(fileBufferLength);
        }