當前位置: 首頁>>代碼示例>>C#>>正文


C# Stream.Write方法代碼示例

本文整理匯總了C#中System.Stream.Write方法的典型用法代碼示例。如果您正苦於以下問題:C# Stream.Write方法的具體用法?C# Stream.Write怎麽用?C# Stream.Write使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Stream的用法示例。


在下文中一共展示了Stream.Write方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: ExtractFile

        /// <summary>
        /// Copy the contents of a stored file into an opened stream
        /// </summary>
        /// <param name="_zfe">Entry information of file to extract</param>
        /// <param name="_stream">Stream to store the uncompressed data</param>
        /// <returns>True if success, false if not.</returns>
        /// <remarks>Unique compression methods are Store and Deflate</remarks>
        public bool ExtractFile(ZipFileEntry _zfe, Stream _stream)
        {
            if (!_stream.CanWrite)
                 throw new InvalidOperationException("Stream cannot be written");

             // check signature
             byte[] signature = new byte[4];
             this.ZipFileStream.Seek(_zfe.HeaderOffset, SeekOrigin.Begin);
             this.ZipFileStream.Read(signature, 0, 4);
             if (BitConverter.ToUInt32(signature, 0) != 0x04034b50)
                 return false;

             // Select input stream for inflating or just reading
             Stream inStream;
             if (_zfe.Method == Compression.Store)
                 inStream = this.ZipFileStream;
             else if (_zfe.Method == Compression.Deflate)
                 inStream = new DeflateStream(this.ZipFileStream, CompressionMode.Decompress, true);
             else
                 return false;

             // Buffered copy
             byte[] buffer = new byte[16384];
             this.ZipFileStream.Seek(_zfe.FileOffset, SeekOrigin.Begin);
             uint bytesPending = _zfe.FileSize;
             while (bytesPending > 0)
             {
                 int bytesRead = inStream.Read(buffer, 0, (int)Math.Min(bytesPending, buffer.Length));
                 _stream.Write(buffer, 0, bytesRead);
                 bytesPending -= (uint)bytesRead;
             }
             _stream.Flush();

             if (_zfe.Method == Compression.Deflate)
                 inStream.Dispose();
             return true;
        }
開發者ID:aswartzbaugh,項目名稱:biketour,代碼行數:44,代碼來源:ZipStorer.cs

示例2: GetRes


//.........這裏部分代碼省略.........
                        StringUtil.RemoveFromInList("metadata",
                            true,
                            ref strStyle);
                        bHasMetadataStyle = false;
                    }


                    lTotalLength = result.Value;


                    if (StringUtil.IsInList("timestamp", strStyle) == true
                        /*
                        && lTotalLength > 0
                         * */ )    // 2012/1/11
                    {
                        if (input_timestamp != null)
                        {
                            if (ByteArray.Compare(input_timestamp, timestamp) != 0)
                            {
                                strError = "下載過程中發現時間戳和input_timestamp參數中的時間戳不一致,下載失敗 ...";
                                return -1;
                            }
                        }
                        if (old_timestamp != null)
                        {
                            if (ByteArray.Compare(old_timestamp, timestamp) != 0)
                            {
                                strError = "下載過程中發現時間戳變化,下載失敗 ...";
                                return -1;
                            }
                        }
                    }

                    old_timestamp = timestamp;

                    if (fileTarget == null)
                        break;

                    // 寫入文件
                    if (StringUtil.IsInList("attachment", strStyle) == true)
                    {
                        Debug.Assert(false, "attachment style暫時不能使用");
                        /*
						Attachment attachment = ws.ResponseSoapContext.Attachments[id];
						if (attachment == null)
						{
							strError = "id為 '" +id+ "' 的attachment在WebService響應中沒有找到...";
							return -1;
						}
						StreamUtil.DumpStream(attachment.Stream, fileTarget);
						nStart += (int)attachment.Stream.Length;

						Debug.Assert(attachment.Stream.Length <= result.Value, "每次返回的包尺寸["+Convert.ToString(attachment.Stream.Length)+"]應當小於result.Value["+Convert.ToString(result.Value)+"]");
                         */

                    }
                    else
                    {
                        Debug.Assert(StringUtil.IsInList("content", strStyle) == true,
                            "不是attachment風格,就應是content風格");

                        Debug.Assert(baContent != null, "返回的baContent不能為null");
                        Debug.Assert(baContent.Length <= result.Value, "每次返回的包尺寸[" + Convert.ToString(baContent.Length) + "]應當小於result.Value[" + Convert.ToString(result.Value) + "]");

                        fileTarget.Write(baContent, 0, baContent.Length);
                        if (flushOutputMethod != null)
                        {
                            if (flushOutputMethod() == false)
                            {
                                strError = "FlushOutputMethod()用戶中斷";
                                return -1;
                            }
                        }
                        lStart += baContent.Length;
                    }

                    if (lStart >= result.Value)
                        break;	// 結束

                } // end try


                catch (Exception ex)
                {
                    /*
                    strError = ConvertWebError(ex);
                    return -1;
                     * */
                    int nRet = ConvertWebError(ex, out strError);
                    if (nRet == 0)
                        return -1;
                    goto REDO;
                }

            } // end of for

            baOutputTimeStamp = timestamp;
            this.ClearRedoCount();
            return 0;
        }
開發者ID:renyh1013,項目名稱:dp2,代碼行數:101,代碼來源:KernelChannel.cs

示例3: WriteString

        public static void WriteString(Stream s, string v)
        {
            MemoryStream baos = new MemoryStream();
            for (int index = 0; index < v.Length; index++)
            {
#if CODE_ANALYSIS
                char c = v.CharAt(index);
#else
                char c = v[index];
#endif
                if ((c > 0) && (c < 80))
                    baos.WriteByte((byte)c);
                else if (c < '\u0800')
                {
                    baos.WriteByte((byte)(0xc0 | (0x1f & (c >> 6))));
                    baos.WriteByte((byte)(0x80 | (0x3f & c)));
                }
                else
                {
                    baos.WriteByte((byte)(0xe0 | (0x0f & (c >> 12))));
                    baos.WriteByte((byte)(0x80 | (0x3f & (c >> 6))));
                    baos.WriteByte((byte)(0x80 | (0x3f & c)));
                }
            }
            WriteUInt16(s, (ushort)baos.Length);
            s.Write(baos.GetBuffer(), 0, (int)baos.Length);
        }
開發者ID:divyang4481,項目名稱:bclcontrib-scriptsharp,代碼行數:27,代碼來源:SE.cs

示例4: WriteTo

 /// <summary>
 /// Convenience method which writes the contents of the ZipEntry
 /// to the specified stream and returns the number of bytes written.
 /// </summary>
 /// <param name="stream">The stream.</param>
 /// <returns></returns>
 public int WriteTo(Stream stream)
 {
     byte[] buffer = new byte[GlobalConstants.DefaultStreamBlockSize];
     int bytesRead, totalBytesRead = 0;
     while ((bytesRead = Read(buffer, 0, GlobalConstants.DefaultStreamBlockSize)) > -1)
     {
         stream.Write(buffer, 0, bytesRead);
         totalBytesRead += bytesRead;
     }
     return totalBytesRead;
 }
開發者ID:Yitzchok,項目名稱:PublicDomain,代碼行數:17,代碼來源:Archiver.cs


注:本文中的System.Stream.Write方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。