本文整理汇总了C#中Stream.WriteByte方法的典型用法代码示例。如果您正苦于以下问题:C# Stream.WriteByte方法的具体用法?C# Stream.WriteByte怎么用?C# Stream.WriteByte使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Stream
的用法示例。
在下文中一共展示了Stream.WriteByte方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: EndMessage
public void EndMessage(Stream stream)
{
SDebug.Assert(message != RemoteMessage.Invalid);
// Write message header
stream.WriteByte((byte)message);
uint len = (uint)packet.Length;
stream.WriteByte((byte)(len & 0xFF));
stream.WriteByte((byte)((len >> 8) & 0xFF));
stream.WriteByte((byte)((len >> 16) & 0xFF));
stream.WriteByte((byte)((len >> 24) & 0xFF));
// Write the message
packet.Position = 0;
Utils.CopyToStream(packet, stream, buffer, (int)packet.Length);
message = RemoteMessage.Invalid;
}
示例3: 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;
}
示例4: ByteTest
public static void ByteTest(Stream s)
{
Console.WriteLine(" (01) ByteTest on "+s.GetType( ).Name);
int size = 0x25000;
int r;
for(int i=0;i<size;i++) s.WriteByte((byte)i);
s.Position=0;
for(int i=0;i<size;i++)
{
r=s.ReadByte( );
if(r!=i%256)
throw new Exception("Didn't get the correct value for i at: "
+i+" expected: "+(i%256)+" got: "+r);
}
}
示例5: SpanTest
public static void SpanTest(Stream s)
{
Console.WriteLine(" (02) SpanTest on "+s.GetType( ).Name);
// If a read request spans across two buffers, make sure we get the data
// back in one read request, since it would be silly to not do so. This
// caught a bug in BufferedStream, which was missing a case to handle the
// second read request.
// Here's a legend. The space is at the 8K buffer boundary.
// /-------------------|------- -------|------------------\
// First for loop Read(bytes) Second for loop
int max;
int r;
byte[] bytes;
int numBytes;
const int bufferSize = 8*(1<<10);
for(int i=0;i<bufferSize*2;i++) s.WriteByte((byte)(i % 256));
s.Position=0;
max = bufferSize-(bufferSize>>2);
for(int i=0;i<max;i++)
{
r=s.ReadByte( );
if(r!=i%256)
throw new Exception("Didn't get the correct value for i at: "
+i+" expected: "+(i%256)+" got: "+r);
}
bytes = new byte[bufferSize>>1];
numBytes=s.Read(bytes,0,bytes.Length);
if (numBytes!=bytes.Length)
throw new Exception("While reading into a byte[] spanning a buffer starting at position "
+max+", didn't get the right number of bytes! asked for: "
+bytes.Length+" got: "+numBytes);
for(int i=0;i<bytes.Length;i++)
{
if(bytes[i]!=((i+max)%256))
throw new Exception("When reading in an array that spans across two buffers, "+
"got an incorrect value at index: "+i+
" expected: "+((i+max)%256)+" got: "+bytes[i]);
}
// Read rest of data
for(int i=0;i<max;i++)
{
r=s.ReadByte( );
if(r!=(i+max+bytes.Length)%256)
throw new Exception("While reading the last part of the buffer, "
+"didn't get the correct value for i at: "+i
+" expected: "+((i+max+bytes.Length)%256)+" got: "+r);
}
}
示例6: CompressedOutputStream
public CompressedOutputStream(Stream baseStream, CompressionType compressionType)
{
_baseStream = baseStream;
_compressionType = compressionType;
_baseStream.WriteByte((byte)_compressionType);
switch (_compressionType)
{
case CompressionType.NONE:
_compressor = _baseStream;
break;
case CompressionType.LZF:
_compressor = new LzfOutputStream(_baseStream);
break;
case CompressionType.BZIP2:
_compressor = new BZip2OutputStream(_baseStream);
break;
}
}
示例7: Serialize
/// <summary>Serialize the instance into the stream</summary>
public static void Serialize(Stream stream, TheirMessage instance)
{
// Key for field: 1, Varint
stream.WriteByte(8);
global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt64(stream,(ulong)instance.FieldA);
}
示例8: ReadWriteBufferSwitchTest
public static bool ReadWriteBufferSwitchTest(Stream s)
{
String writableStr = "read-only";
if(s.CanWrite)
writableStr="read/write";
Console.WriteLine(" (05) ReadWriteBufferSwitch test for a "+writableStr+" "+s.GetType( ).Name);
if(s.Length<8200)
Console.WriteLine("ReadWriteBufferSwitchTest assumes stream len is at least 8200");
s.Position = 0;
// Assume buffer size is 8192
for(int i=0;i<8192;i++) s.ReadByte( );
// Console.WriteLine(" (05) Pos: "+s.Position+" s.Length: "+s.Length);
s.WriteByte(0);
// Calling the position property will verify internal buffer state.
if(8193!=s.Position)
throw new Exception("Position should be 8193! Position: "+s.Position);
s.ReadByte( );
return true;
}
示例9: StreamTest
public static async Task StreamTest(Stream stream, Boolean fSuppress)
{
string strValue;
int iValue;
//[] We will first use the stream's 2 writing methods
int iLength = 1 << 10;
stream.Seek(0, SeekOrigin.Begin);
for (int i = 0; i < iLength; i++)
stream.WriteByte((byte)i);
byte[] btArr = new byte[iLength];
for (int i = 0; i < iLength; i++)
btArr[i] = (byte)i;
stream.Write(btArr, 0, iLength);
//we will write many things here using a binary writer
BinaryWriter bw1 = new BinaryWriter(stream);
bw1.Write(false);
bw1.Write(true);
for (int i = 0; i < 10; i++)
{
bw1.Write((byte)i);
bw1.Write((sbyte)i);
bw1.Write((short)i);
bw1.Write((char)i);
bw1.Write((UInt16)i);
bw1.Write(i);
bw1.Write((uint)i);
bw1.Write((Int64)i);
bw1.Write((ulong)i);
bw1.Write((float)i);
bw1.Write((double)i);
}
//Some strings, chars and Bytes
char[] chArr = new char[iLength];
for (int i = 0; i < iLength; i++)
chArr[i] = (char)i;
bw1.Write(chArr);
bw1.Write(chArr, 512, 512);
bw1.Write(new string(chArr));
bw1.Write(new string(chArr));
//[] we will now read
stream.Seek(0, SeekOrigin.Begin);
for (int i = 0; i < iLength; i++)
{
Assert.Equal(i % 256, stream.ReadByte());
}
btArr = new byte[iLength];
stream.Read(btArr, 0, iLength);
for (int i = 0; i < iLength; i++)
{
Assert.Equal((byte)i, btArr[i]);
}
//Now, for the binary reader
BinaryReader br1 = new BinaryReader(stream);
Assert.False(br1.ReadBoolean());
Assert.True(br1.ReadBoolean());
for (int i = 0; i < 10; i++)
{
Assert.Equal( (byte)i, br1.ReadByte());
Assert.Equal((sbyte)i, br1.ReadSByte());
Assert.Equal((short)i, br1.ReadInt16());
Assert.Equal((char)i, br1.ReadChar());
Assert.Equal((UInt16)i, br1.ReadUInt16());
Assert.Equal(i, br1.ReadInt32());
Assert.Equal((uint)i, br1.ReadUInt32());
Assert.Equal((Int64)i, br1.ReadInt64());
Assert.Equal((ulong)i, br1.ReadUInt64());
Assert.Equal((float)i, br1.ReadSingle());
Assert.Equal((double)i, br1.ReadDouble());
}
chArr = br1.ReadChars(iLength);
for (int i = 0; i < iLength; i++)
{
Assert.Equal((char)i, chArr[i]);
}
chArr = new char[512];
chArr = br1.ReadChars(iLength / 2);
for (int i = 0; i < iLength / 2; i++)
{
Assert.Equal((char)(iLength / 2 + i), chArr[i]);
}
chArr = new char[iLength];
for (int i = 0; i < iLength; i++)
//.........这里部分代码省略.........
示例10: WriteData
public static void WriteData(Stream s)
{
BinaryWriter bw;
int offset = 10;
byte[] bytes = new byte[256 + offset];
for (int i=0;i<256;i++) bytes[i+offset] = (byte)i;
// Test reading & writing at an offset into the user array...
s.Write(bytes,offset,bytes.Length-offset);
// Test WriteByte methods.
s.WriteByte(0);
s.WriteByte(65);
s.WriteByte(255);
s.Flush( );
bw = new BinaryWriter(s);
for(int i=0;i<1000;i++) bw.Write(i);
bw.Write(false);
bw.Write(true);
for(char ch='A';ch<='Z';ch++) bw.Write(ch);
for(short i=-32000;i<=32000;i+=1000) bw.Write(i);
for(int i=-2000000000;i<=2000000000;i+=100000000) bw.Write(i);
bw.Write(0x0123456789ABCDE7L);
bw.Write(0x7EDCBA9876543210L);
bw.Write("Hello world");
bw.Write(Math.PI);
bw.Write((float)(-2.0*Math.PI));
bw.Flush( );
}
示例11: CanPropertiesTest
public static bool CanPropertiesTest(Stream s)
{
Console.WriteLine(" (06) Can-Properties Test on "+s.GetType( ).Name);
byte[] bytes = new byte[1];
int bytesTransferred;
IAsyncResult asyncResult;
if(s.CanRead)
{ // Ensure all Read methods work, if CanRead is true.
int n = s.ReadByte( );
if(n==-1)
throw new Exception("On a readable stream, ReadByte returned -1...");
bytesTransferred=s.Read(bytes,0,1);
if(bytesTransferred!=1)
throw new Exception("Read(byte[],0,1) should have returned 1! got: "+bytesTransferred);
asyncResult=s.BeginRead(bytes,0,1,null,null); /* BeginRead */
bytesTransferred=s.EndRead(asyncResult); /* EndRead */
if(bytesTransferred!=1)
throw new Exception("BeginRead(byte[],0,1) should have returned 1! got: "
+bytesTransferred);
} // End of (s.CanRead) Block
else
{ // Begin of (!s.CanRead) Block
try
{
s.ReadByte( );
throw new Exception("ReadByte on an unreadable stream should have thrown!");
}
catch(NotSupportedException) { }
try
{
s.Read(bytes,0,1);
throw new Exception("Read(bytes,0,1) on an unreadable stream should have thrown!");
}
catch(NotSupportedException) { }
try
{
s.BeginRead(bytes,0,1,null,null); /* BeginRead */
throw new Exception("BeginRead on an unreadable stream should have thrown!");
}
catch(NotSupportedException) { }
} // End of (!s.CanRead) Block
if(s.CanWrite)
{ // Ensure write methods work if CanWrite returns true.
s.WriteByte(0);
s.Write(bytes,0,1);
asyncResult = s.BeginWrite(bytes,0,1,null,null); /* BeginWrite */
s.EndWrite(asyncResult); /* EndWrite */
} // End of (s.CanWrite) Block
else
{ // Begin of (!s.CanWrite) Block
try
{
s.WriteByte(2);
throw new Exception("WriteByte on an unreadable stream should have thrown!");
}
catch(NotSupportedException) { }
try
{
s.Write(bytes,0,1);
throw new Exception("Write(bytes,0,1) on an unreadable stream should have thrown!");
}
catch(NotSupportedException) { }
try
{
s.BeginWrite(bytes,0,1,null,null); /* BeginWrite */
throw new Exception("BeginWrite on an unreadable stream should have thrown!");
}
catch(NotSupportedException) { }
} // End of (!s.CanWrite) Block
if(s.CanSeek)
{ // Ensure length-related methods work if CanSeek returns true
long n = s.Length;
n=s.Position;
if(s.Position>s.Length)
throw new Exception("Position is beyond the length of the stream!");
s.Position=0;
s.Position=s.Length;
if(s.Position!=s.Seek(0,SeekOrigin.Current))
throw new Exception("s.Position!=s.Seek(0,SeekOrigin.Current)");
if(s.CanWrite) // Verify you can set the length, if it's writable.
s.SetLength(s.Length);
} // End of (s.CanSeek) Block
else
{ // Begin of (!s.CanSeek) Block
try
{
s.Position=5;
throw new Exception("set_Position should throw on a non-seekable stream!");
}
catch(NotSupportedException) { }
try
{
long n = s.Position;
throw new Exception("get_Position should throw on a non-seekable stream!");
}
catch(NotSupportedException) { }
try
{
long n = s.Length;
throw new Exception("get_Length should throw on a non-seekable stream!");
//.........这里部分代码省略.........