本文整理汇总了C#中Stream.Write方法的典型用法代码示例。如果您正苦于以下问题:C# Stream.Write方法的具体用法?C# Stream.Write怎么用?C# Stream.Write使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Stream
的用法示例。
在下文中一共展示了Stream.Write方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Write
public static void Write(Stream stream, byte type, Stream exportStream)
{
stream.WriteByte(type);
stream.Write(NetworkConverter.GetBytes((int)exportStream.Length), 0, 4);
byte[] buffer = null;
try
{
buffer = _bufferManager.TakeBuffer(1024 * 4);
int length = 0;
while ((length = exportStream.Read(buffer, 0, buffer.Length)) > 0)
{
stream.Write(buffer, 0, length);
}
}
finally
{
if (buffer != null)
{
_bufferManager.ReturnBuffer(buffer);
}
}
}
示例2: WriteOutput
public static void WriteOutput(object data, Stream output, bool closeStreams )
{
if (data == output) return;
XmlDocument xml = data as XmlDocument;
if (xml != null)
{
XmlTextWriter xmlWriter = new XmlTextWriter(output, System.Text.Encoding.UTF8);
xml.WriteTo(xmlWriter);
xmlWriter.Flush();
}
Stream stream = data as Stream;
if (stream != null)
{
stream.Seek(0, SeekOrigin.Begin);
byte[] buffer = new byte[0x5000];
int read;
while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
output.Write(buffer, 0, read);
if ( closeStreams ) stream.Close();
}
byte[] block = data as byte[];
if ( block != null && block.Length > 0 )
output.Write( block, 0, block.Length );
}
示例3: CopyTo
/// <summary>
/// Reads all the bytes from the current stream and writes them to the destination stream.
/// </summary>
/// <param name="original">The current stream.</param>
/// <param name="destination">The stream that will contain the contents of the current stream.</param>
/// <exception cref="System.ArgumentNullException">Destination is null.</exception>
/// <exception cref="System.NotSupportedException">The current stream does not support reading.-or-destination does not support Writing.</exception>
/// <exception cref="System.ObjectDisposedException">Either the current stream or destination were closed before the System.IO.Stream.CopyTo(System.IO.Stream) method was called.</exception>
/// <exception cref="System.IO.IOException">An I/O error occurred.</exception>
public static void CopyTo(this Stream original, Stream destination)
{
if (destination == null)
{
throw new ArgumentNullException("destination");
}
if (!original.CanRead && !original.CanWrite)
{
throw new ObjectDisposedException("ObjectDisposedException");
}
if (!destination.CanRead && !destination.CanWrite)
{
throw new ObjectDisposedException("ObjectDisposedException");
}
if (!original.CanRead)
{
throw new NotSupportedException("NotSupportedException source");
}
if (!destination.CanWrite)
{
throw new NotSupportedException("NotSupportedException destination");
}
byte[] array = new byte[4096];
int count;
while ((count = original.Read(array, 0, array.Length)) != 0)
{
destination.Write(array, 0, count);
}
}
示例4: CompressStream
static void CompressStream(Stream from, Stream to)
{
byte[] src = new byte[from.Length];
// read file
if (from.Read(src, 0, src.Length) == src.Length)
{
int dstSize = DllInterface.aP_max_packed_size(src.Length);
int wrkSize = DllInterface.aP_workmem_size(src.Length);
// allocate mem
byte[] dst = new byte[dstSize];
byte[] wrk = new byte[wrkSize];
// compress data
int packedSize = DllInterface.aPsafe_pack(
src,
dst,
src.Length,
wrk,
new DllInterface.CompressionCallback(ShowProgress),
0
);
// write compressed data
to.Write(dst, 0, packedSize);
Console.WriteLine("compressed to {0} bytes", packedSize);
}
}
示例5: CopyStream
public static void CopyStream(Stream input, Stream output)
{
var buffer = new byte[1024];
int bytes;
while ((bytes = input.Read(buffer, 0, 1024)) > 0)
output.Write(buffer, 0, bytes);
}
示例6: CopyStream
private static void CopyStream(Stream source, Stream target)
{
const int bufSize = 0x1000;
byte[] buf = new byte[bufSize];
int bytesRead = 0;
while ((bytesRead = source.Read(buf, 0, bufSize)) > 0)
target.Write(buf, 0, bytesRead);
}
示例7: WriteTo
/// <summary>
/// Writes the input stream to the target stream.
/// </summary>
/// <param name="source" this="true">The source stream to write to the target stream.</param>
/// <param name="target">The target stream to write to.</param>
/// <returns>The written <paramref name="target"/> stream.</returns>
public static void WriteTo(this Stream source, Stream target)
{
var buffer = new byte[BufferSize];
var read = 0;
while ((read = source.Read(buffer, 0, buffer.Length)) != 0)
{
target.Write(buffer, 0, read);
}
}
示例8: CopyTo
// Summary:
// Reads all the bytes from the current stream and writes them to the destination
// stream.
//
// Parameters:
// destination:
// The stream that will contain the contents of the current stream.
//
// NOTE:
// This is a Stream member method in .NET 4
public static void CopyTo(this Stream source, Stream destination)
{
byte[] buffer = new byte[4096];
int n;
while ((n = source.Read(buffer, 0, buffer.Length)) != 0)
{
destination.Write(buffer, 0, n);
}
}
示例9: CopyTo
static void CopyTo(Stream source, Stream destination)
{
var array = new byte[81920];
int count;
while ((count = source.Read(array, 0, array.Length)) != 0)
{
destination.Write(array, 0, count);
}
}
示例10: CopyToStream
public static void CopyToStream(Stream src, Stream dst, byte[] buffer, int numBytes)
{
while (numBytes > 0)
{
int req = Math.Min(buffer.Length, numBytes);
int read = src.Read(buffer, 0, req);
dst.Write(buffer, 0, read);
numBytes -= read;
}
}
示例11: CopyTo
public static void CopyTo(this Stream from, Stream dest, byte[] buffer = null)
{
buffer = buffer ?? new byte[DefaultCopyBufferSize];
var bufferSize = buffer.Length;
int read;
while ((read = from.Read(buffer, 0, bufferSize)) > 0) {
dest.Write(buffer, 0, read);
}
}
示例12: writedouble
public static void writedouble(Stream output, double val)
{
byte[] bytes = System.BitConverter.GetBytes(val);
#if StandAlone
#else
if (System.BitConverter.IsLittleEndian)
System.Array.Reverse(bytes);
#endif
output.Write(bytes, 0, DOUBLE_LEN);
}
示例13: WriteLong
public static void WriteLong(long value, Stream stream, bool isLittleEndian)
{
byte[] buffer = BitConverter.GetBytes(value);
if (BitConverter.IsLittleEndian && !isLittleEndian)
{
Array.Reverse(buffer);
}
stream.Write(buffer, 0, buffer.Length);
}
示例14: CopyStream
public static void CopyStream(Stream src, Stream dst)
{
byte[] buff = new byte[4096];
while (true)
{
int read = src.Read(buff, 0, 4096);
if (read == 0)
break;
dst.Write(buff, 0, read);
}
}
示例15: WriteBeyondEndTest
public static bool WriteBeyondEndTest(Stream s)
{
Console.WriteLine("Write Beyond End test on "+s.GetType().Name);
FileStream fs = s as FileStream;
if (fs != null)
Console.WriteLine("FileStream type is: "+(fs.IsAsync ? "asynchronous" : "synchronous"));
long origLength = s.Length;
byte[] bytes = new byte[10];
for(int i=0; i<bytes.Length; i++)
bytes[i] = (byte) i;
int spanPastEnd = 5;
s.Seek(spanPastEnd, SeekOrigin.End);
if (s.Position != s.Length + spanPastEnd)
throw new Exception("Position is incorrect! Seek(5, SeekOrigin.End) should leave us at s.Length + spanPastEnd ("+(s.Length + spanPastEnd)+"), but got: "+s.Position);
Console.WriteLine("Original Length: "+origLength);
s.Write(bytes, 0, bytes.Length);
long pos = s.Position;
if (pos != origLength + spanPastEnd + bytes.Length)
throw new Exception(String.Format("After asynchronously writing beyond end of the stream, position is now incorrect! origLength: {0} pos: {1}", origLength, pos));
if (s.Length != origLength + spanPastEnd + bytes.Length)
throw new Exception(String.Format("After asynchronously writing beyond end of the stream, Length is now incorrect! origLength: {0} pos: {1}", origLength, pos));
WritePastEndHelper(s, bytes, origLength, spanPastEnd, false);
origLength = s.Length;
s.Position = s.Length + spanPastEnd;
s.WriteByte(0x42);
long expected = origLength + spanPastEnd + 1;
if (s.Position != expected)
{
iCountErrors++ ;
throw new Exception("After WriteByte, Position was wrong! got: "+s.Position+" expected: "+expected);
}
if (s.Length != expected)
{
iCountErrors++ ;
throw new Exception("After WriteByte, Length was wrong! got: "+s.Length+" expected: "+expected);
}
origLength = s.Length;
s.Position = s.Length + spanPastEnd;
IAsyncResult ar = s.BeginWrite(bytes, 0, bytes.Length, null, null);
s.EndWrite(ar);
pos = s.Position;
if (pos != origLength + spanPastEnd + bytes.Length)
{
iCountErrors++ ;
throw new Exception(String.Format("After writing beyond end of the stream, position is now incorrect! origLength: {0} pos: {1}", origLength, pos));
}
if (s.Length != origLength + spanPastEnd + bytes.Length)
{
iCountErrors++;
throw new Exception(String.Format("After writing beyond end of the stream, Length is now incorrect! origLength: {0} pos: {1}", origLength, pos));
}
WritePastEndHelper(s, bytes, origLength, spanPastEnd, true);
return true;
}