示例#1
0
        private void AsyncStreamCallback(IAsyncResult ar)
        {
            RequestBodyState requestBodyState = ar.AsyncState as RequestBodyState;
            Stream           outputStream;

            try
            {
                outputStream = requestBodyState.outputStream;
                outputStream.EndWrite(ar);
                if (progressCallback != null)
                {
                    UpdateProgress(requestBodyState.complete, contentLength);
                }
                if (requestBodyState.complete < contentLength)
                {
                    long remain = contentLength - requestBodyState.complete;
                    int  count  = (int)(remain > SEGMENT_SIZE ? SEGMENT_SIZE : remain);
                    int  offset = (int)requestBodyState.complete;
                    requestBodyState.complete += count;
                    outputStream.BeginWrite(requestBodyState.buffer, offset, count, AsyncStreamCallback, requestBodyState);
                }
                else
                {
                    //write over
                    requestBodyState.endRequestBody(null);
                }
            }
            catch (Exception ex)
            {
                QLog.E(TAG, ex.Message, ex);
                requestBodyState.endRequestBody(ex);;
            }
        }
示例#2
0
        private void AsyncStreamCallback(IAsyncResult ar)
        {
            RequestBodyState requestBodyState = ar.AsyncState as RequestBodyState;
            Stream           outputStream;

            try
            {
                outputStream = requestBodyState.outputStream;
                outputStream.EndWrite(ar);
                if (progressCallback != null)
                {
                    UpdateProgress(requestBodyState.complete, realContentLength);
                }
                if (requestBodyState.complete < realContentLength)
                {
                    if (srcPath != null)
                    {
                        long remain = realContentLength - requestBodyState.complete;
                        int  count  = fileStream.Read(requestBodyState.buffer, 0, (int)(requestBodyState.buffer.Length > remain ? remain : requestBodyState.buffer.Length));
                        requestBodyState.complete += count;
                        outputStream.BeginWrite(requestBodyState.buffer, 0, count, AsyncStreamCallback, requestBodyState);
                    }
                    else if (data != null)
                    {
                        long remain = realContentLength - requestBodyState.complete;
                        int  count  = (int)(remain > SEGMENT_SIZE ? SEGMENT_SIZE : remain);
                        int  offset = (int)requestBodyState.complete;
                        requestBodyState.complete += count;
                        outputStream.BeginWrite(requestBodyState.buffer, offset, count, AsyncStreamCallback, requestBodyState);
                    }

                    outputStream.Flush();
                }
                else
                {
                    if (fileStream != null)
                    {
                        fileStream.Close();
                    }
                    WriteEndLine(outputStream);
                    outputStream.Flush();
                    //write over
                    requestBodyState.endRequestBody(null);
                }
            }
            catch (Exception ex)
            {
                if (fileStream != null)
                {
                    fileStream.Close();
                }
                QLog.E(TAG, ex.Message, ex);
                requestBodyState.endRequestBody(ex);
            }
        }
示例#3
0
        private void AsyncStreamCallback(IAsyncResult ar)
        {
            RequestBodyState requestBodyState = ar.AsyncState as RequestBodyState;
            Stream           outputStream     = null;

            try
            {
                outputStream = requestBodyState.outputStream;
                outputStream.EndWrite(ar);
                outputStream.Flush();
                if (progressCallback != null)
                {
                    UpdateProgress(requestBodyState.complete, contentLength);
                }
                if (requestBodyState.complete < contentLength)
                {
                    long remain = contentLength - requestBodyState.complete;
                    int  count  = fileStream.Read(requestBodyState.buffer, 0, (int)(requestBodyState.buffer.Length > remain ? remain : requestBodyState.buffer.Length));
                    requestBodyState.complete += count;
                    outputStream.BeginWrite(requestBodyState.buffer, 0, count, AsyncStreamCallback, requestBodyState);
                }
                else
                {
                    if (fileStream != null)
                    {
                        fileStream.Close();
                    }
                    if (outputStream != null)
                    {
                        outputStream.Close();
                    }
                    //write over
                    requestBodyState.endRequestBody(null);
                    requestBodyState = null;
                }
            }
            catch (Exception ex)
            {
                if (fileStream != null)
                {
                    fileStream.Close();
                }
                if (outputStream != null)
                {
                    outputStream.Close();
                }
                QLog.E(TAG, ex.Message, ex);
                requestBodyState.endRequestBody(ex);
                requestBodyState = null;
            }
        }
        public override void StartHandleRequestBody(Stream outputStream, EndRequestBody endRequestBody)
        {
            RequestBodyState requestBodyState = new RequestBodyState();

            requestBodyState.endRequestBody = endRequestBody;
            requestBodyState.outputStream   = outputStream;
            requestBodyState.complete       = 0;
            int size = (int)(contentLength > SEGMENT_SIZE ? SEGMENT_SIZE : contentLength);

            fileStream = new FileStream(srcPath, FileMode.Open, FileAccess.Read);
            fileStream.Seek(fileOffset, SeekOrigin.Begin);
            try
            {
                requestBodyState.buffer = new byte[size];// 4kb
                long remain = contentLength - requestBodyState.complete;
                if (remain > 0)
                {
                    int count = fileStream.Read(requestBodyState.buffer, 0, (int)(requestBodyState.buffer.Length > remain ? remain : requestBodyState.buffer.Length));
                    requestBodyState.complete = count;
                    outputStream.BeginWrite(requestBodyState.buffer, 0, count, AsyncStreamCallback, requestBodyState);
                }
                else
                {
                    if (fileStream != null)
                    {
                        fileStream.Close();
                    }
                    if (outputStream != null)
                    {
                        outputStream.Close();
                    }
                    //write over
                    requestBodyState.endRequestBody(null);
                    requestBodyState = null;
                }
            }
            catch (Exception ex)
            {
                if (fileStream != null)
                {
                    fileStream.Close();
                }
                if (outputStream != null)
                {
                    outputStream.Close();
                }
                QLog.E(TAG, ex.Message, ex);
                requestBodyState.endRequestBody(ex);
                requestBodyState = null;
            }
        }
        public override void StartHandleRequestBody(Stream outputStream, EndRequestBody endRequestBody)
        {
            RequestBodyState requestBodyState = new RequestBodyState();

            requestBodyState.endRequestBody = endRequestBody;
            requestBodyState.outputStream   = outputStream;
            requestBodyState.buffer         = data;
            int count = (int)(contentLength > SEGMENT_SIZE ? SEGMENT_SIZE : contentLength);

            try
            {
                requestBodyState.complete = count;
                outputStream.BeginWrite(requestBodyState.buffer, 0, count, AsyncStreamCallback, requestBodyState);
            }
            catch (Exception ex)
            {
                if (outputStream != null)
                {
                    outputStream.Close();
                }
                QLog.E(TAG, ex.Message, ex);
                requestBodyState.endRequestBody(ex);
                requestBodyState = null;
            }
        }
示例#6
0
        public override void StartHandleRequestBody(Stream outputStream, EndRequestBody endRequestBody)
        {
            requestBodyState = new RequestBodyState();
            requestBodyState.endRequestBody = endRequestBody;
            requestBodyState.outputStream   = outputStream;
            requestBodyState.complete       = 0;

            //write paramters
            WriteParameters(outputStream);

            //写入content-disposition: form-data; name = "file"; filename = "xxx"\r\n
            WriteFileParameters(outputStream);

            outputStream.Flush();


            int size = (int)(realContentLength > SEGMENT_SIZE ? SEGMENT_SIZE : realContentLength);

            if (srcPath != null)
            {
                try
                {
                    fileStream = new FileStream(srcPath, FileMode.Open, FileAccess.Read);
                    fileStream.Seek(fileOffset, SeekOrigin.Begin);
                    requestBodyState.buffer = new byte[size];// 4kb
                    long remain = realContentLength - requestBodyState.complete;
                    if (remain > 0)
                    {
                        int count = fileStream.Read(requestBodyState.buffer, 0, (int)(requestBodyState.buffer.Length > remain ? remain : requestBodyState.buffer.Length));
                        requestBodyState.complete = count;
                        outputStream.BeginWrite(requestBodyState.buffer, 0, count, AsyncStreamCallback, requestBodyState);
                    }
                    else
                    {
                        if (fileStream != null)
                        {
                            fileStream.Close();
                        }
                        WriteEndLine(outputStream);
                        outputStream.Flush();
                        //write over
                        requestBodyState.endRequestBody(null);
                    }
                }
                catch (Exception ex)
                {
                    if (fileStream != null)
                    {
                        fileStream.Close();
                    }
                    QLog.E(TAG, ex.Message, ex);
                    requestBodyState.endRequestBody(ex);
                }
            }
            else if (data != null)
            {
                requestBodyState.buffer = data;
                try
                {
                    requestBodyState.complete = size;
                    outputStream.BeginWrite(requestBodyState.buffer, 0, size, AsyncStreamCallback, requestBodyState);
                }
                catch (Exception ex)
                {
                    QLog.E(TAG, ex.Message, ex);
                    requestBodyState.endRequestBody(ex);
                }
            }
        }