本文整理汇总了C#中Stream.EndRead方法的典型用法代码示例。如果您正苦于以下问题:C# Stream.EndRead方法的具体用法?C# Stream.EndRead怎么用?C# Stream.EndRead使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Stream
的用法示例。
在下文中一共展示了Stream.EndRead方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: StreamByteCounter
public StreamByteCounter(Stream src)
{
AsyncCallback onEndRead = null;
onEndRead = delegate(IAsyncResult ar) {
int c = src.EndRead(ar);
if (c != 0) {
lock(this) { pendingReads++; }
//
// For each read, we must allocate a new buffer.
//
byte[] nb = new byte[CHUNK_SIZE];
src.BeginRead(nb, 0, nb.Length, onEndRead, nb);
//
// The buffer of the current read is passed through the
// IAsyncResult.AsyncState property.
//
byte[] cb = (byte[])ar.AsyncState;
//
// Increment occurrence counters.
//
lock(this) {
for (int i = 0; i < c; i++) {
counters[cb[i]]++;
}
}
}
//
// The data of the current read processed; so, decrement
// the pending counter, and if the counter reaches zero,
// signal the "done" event.
//
lock(this) {
if (--pendingReads == 0) {
done.Set();
}
}
};
//
// Allocate the buffer for the first read, and issue it.
//
byte[] fb = new byte[CHUNK_SIZE];
pendingReads = 1;
src.BeginRead(fb, 0, fb.Length, onEndRead, fb);
}
示例2: ReadBeyondEndTest
public static bool ReadBeyondEndTest(Stream s)
{
Console.WriteLine("Read Beyond End test on "+s.GetType().Name);
byte[] bytes = new byte[10];
for(int i=0; i<bytes.Length; i++)
bytes[i] = (byte) i;
s.Seek(5, SeekOrigin.End);
if (s.Position != s.Length + 5)
{
iCountErrors++;
throw new Exception("Position is incorrect! Seek(5, SeekOrigin.End) should leave us at s.Length + 5, but got: "+s.Position);
}
int numRead = s.Read(bytes, 0, bytes.Length);
if (numRead != 0)
{
iCountErrors++ ;
throw new Exception("Reading from past end of stream is broken! Expected 0, got: "+numRead);
}
for(int i=0; i<bytes.Length; i++)
{
if (bytes[i] != (byte) i)
{
iCountErrors++ ;
throw new Exception("Error in byte[] - Read overwrote it! pos: "+i+" got: "+bytes[i]);
}
}
numRead = s.ReadByte();
if (numRead != -1)
{
iCountErrors++ ;
throw new Exception("ReadByte didn't return -1! got: "+numRead);
}
IAsyncResult ar = s.BeginRead(bytes, 0, bytes.Length, null, null);
numRead = s.EndRead(ar);
if (numRead != 0)
{
iCountErrors++ ;
throw new Exception("Reading from past end of stream with BeginRead is broken! Expected 0, got: "+numRead);
}
for(int i=0; i<bytes.Length; i++)
if (bytes[i] != (byte) i)
{
iCountErrors++ ;
throw new Exception("Error in byte[] - BeginRead overwrote it! pos: "+i+" got: "+bytes[i]);
}
return true;
}
示例3: SetLengthTest
public static bool SetLengthTest(Stream s)
{
Console.WriteLine(" (10) SetLengthTest on "+s.GetType( ).Name);
if(!s.CanSeek)
{
Console.WriteLine("SetLengthTest shouldn't run on non-seekable stream "+s.GetType( ).Name);
try
{
s.SetLength(0);
throw new Exception("SetLength to 0 on a non-seekable stream should have failed!");
}
catch(NotSupportedException) { }
return true;
}
if(!s.CanWrite)
{
Console.WriteLine("SetLengthTest shouldn't run on non-writable stream "+s.GetType( ).Name);
try
{
s.SetLength(0);
throw new Exception("SetLength to 0 on a non-writable stream should have failed! "
+"s.Length: "+s.Length);
}
catch(NotSupportedException) { }
return true;
}
s.SetLength(0);
if(s.Length!=0)
throw new Exception("SetLength to 0, but Length is: "+s.Length);
if(s.Position!=0)
throw new Exception("Set length to 0. Position should be zero too: "+s.Position);
s.SetLength(10);
if(s.Length!=10)
throw new Exception("SetLength to 10, but Length is: "+s.Length);
if(s.Position!=0)
throw new Exception("Set length to 10, yet Position should be zero still: "+s.Position);
if(s.CanRead)
{
byte[] bytes = new byte[500];
IAsyncResult asyncResult = s.BeginRead(bytes,0,500,null,null);
int numBytes = s.EndRead(asyncResult);
if(numBytes!=10)
throw new Exception("Number of bytes got back from EndRead was wrong! "
+"should have been 10, but got: "+numBytes);
if(s.Position!=10)
throw new Exception("After async read, position should be 10, but was: "+s.Position);
}
return true;
}
示例4: ErrorTest
public static bool ErrorTest(Stream s)
{
Console.WriteLine(" (09) Error test on stream: "+s.GetType( ).Name);
// Test EndRead & EndWrite's Type safety
byte[] bytes = new byte[0];
IAsyncResult asyncResult;
BogusIAsyncResult bogus = new BogusIAsyncResult( );
if(s.CanRead)
{
asyncResult = s.BeginRead(bytes,0,0,null,null); /* BeginRead */
try
{
s.EndWrite(asyncResult); /* EndWrite */
throw new Exception("EndWrite with an asyncResult from BeginRead should have thrown!");
}
catch(ArgumentException) { }
}
if(s.CanWrite)
{
asyncResult=s.BeginWrite(bytes,0,0,null,null); /* BeginWrite */
try
{
s.EndRead(asyncResult); /* EndRead */
throw new Exception("EndRead with an asyncResult from BeginWrite should have thrown!");
}
catch(ArgumentException) { }
// Verify EndWrite doesn't allow using the same asyncResult twice.
s.EndWrite(asyncResult); /* EndWrite */
try
{
s.EndWrite(asyncResult); /* EndWrite */
throw new Exception("Exception EndWrite was called twice w/ same IAsyncResult from "
+s.GetType( ).Name+", but didn't throw!");
}
catch(InvalidOperationException) { }
}
try
{
s.EndRead(bogus); /* EndRead */
throw new Exception("EndRead with a bogus IAsyncResult object should have thrown!");
}
catch(ArgumentException) { }
try
{
s.EndWrite(bogus); /* EndWrite */
throw new Exception("EndWrite with a bogus IAsyncResult object should have thrown!");
}
catch(ArgumentException) { }
return true;
}
示例5: AsyncTest
public static bool AsyncTest(Stream s)
{
byte[] bigArr = new byte[80000];
byte[] smArr = new byte[128];
int numBytes;
AsyncCallback cb;
IAsyncResult asyncResult;
DateTime startWait;
TimeSpan thirtySecs = new TimeSpan(0,0,30);
bool firstTime;
TimeSpan diff;
if(s.GetType( )==typeof(IsolatedStorageFileStream))
{
Console.WriteLine("AsyncTest won't run on an IsolatedStorageFileStream "
+"since it doesn't support async operations.");
return true;
}
Console.WriteLine(" (08) Async test on "+s.GetType( ).Name);
if(s.Position!=0)
throw new Exception("AsyncTest assumes stream's position will be 0 when starting.");
for(int i=0;i<smArr.Length;i++) smArr[i] = (byte)i;
// Try writing something more than 64KB so we have a chance of doing an async write.
for(int i=0;i<bigArr.Length;i++) bigArr[i] = (byte)((i%26)+(byte)'A');
// Ensure that we do run our async callback at some point.
didAsyncCallbackRun=false;
cb = new AsyncCallback(AsyncTestCallback);
asyncResult=s.BeginWrite(smArr,0,smArr.Length,cb,String.Empty); /* BeginWrite */
s.EndWrite(asyncResult); /* EndWrite */
if(s.Position!=smArr.Length)
throw new Exception("After first BeginWrite call, (s.Position!=smArr.Length) got: "
+s.Position);
asyncResult=s.BeginWrite(bigArr,0,bigArr.Length,null,String.Empty);
s.EndWrite(asyncResult);
if(s.Position!=bigArr.Length+smArr.Length)
throw new Exception("After second BeginWrite call, s.Position wasn't right! expected: "
+(bigArr.Length+smArr.Length)+" got: "+s.Position);
// And to be sure things work, test write with an offset.
asyncResult=s.BeginWrite(smArr,smArr.Length/2,smArr.Length/2,null,null); /* BeginWrite */
s.EndWrite(asyncResult); /* EndWrite */
if(s.Position!=(smArr.Length/2+bigArr.Length+smArr.Length))
throw new Exception("After third BeginWrite call, s.Position wasn't correct! expected: "
+(smArr.Length/2+bigArr.Length+smArr.Length)
+" got: "+s.Position);
startWait=DateTime.Now;
firstTime=true;
while(!didAsyncCallbackRun)
{
if(firstTime)
{
Console.WriteLine("Waiting for async callback to be run from first BeginWrite call.");
firstTime=false;
}
else
Console.Write(".");
Thread.Sleep(20);
diff=DateTime.Now-startWait;
if(diff>thirtySecs)
throw new Exception("Async callback didn't run yet after 2 BeginWRITE calls and "
+"30 seconds of blocking! "
+"This could be a bug in a particular stream class, "
+"or an extremely pathetic race condition in this test.");
}
didAsyncCallbackRun=false;
s.Position=0;
byte[] input = new byte[(int)s.Length];
// Retest running the async callback here.
asyncResult=s.BeginRead(input,0,smArr.Length,cb,String.Empty); /* BeginRead */
numBytes=s.EndRead(asyncResult); /* BeginRead */
if(numBytes!=smArr.Length)
throw new Exception("After first BeginRead call, (numBytes!=smArr.Length) got: "
+numBytes);
if(s.Position!=smArr.Length)
throw new Exception("After first BeginRead call, (s.Position!=smArr.Length) got: "
+s.Position);
asyncResult=s.BeginRead(input,smArr.Length,bigArr.Length,null,String.Empty); /* BeginRead */
numBytes=s.EndRead(asyncResult); /* EndRead */
if(numBytes!=bigArr.Length)
throw new Exception("After second BeginRead call, (numBytes!=bigArr.Length) got: "
+numBytes);
if(s.Position!=bigArr.Length+smArr.Length)
throw new Exception("After second BeginRead call, s.Position wasn't right! expected: "
+(bigArr.Length+smArr.Length)+" got: "+s.Position);
asyncResult=s.BeginRead(input,smArr.Length+bigArr.Length,smArr.Length/2,null,null); /* BeginRead */
numBytes=s.EndRead(asyncResult); /* EndRead */
if(numBytes!=smArr.Length/2)
throw new Exception("After third BeginRead call, (numBytes!=smArr.Length/2) got: "
+numBytes);
if(s.Position!=(smArr.Length/2+bigArr.Length+smArr.Length))
throw new Exception("After third BeginRead call, s.Position wasn't correct! expected: "
+(smArr.Length/2+bigArr.Length+smArr.Length)
+" got: "+s.Position);
for(int i=0;i<smArr.Length;i++)
if (smArr[i]!=input[i])
throw new Exception("When reading first smArr copy, position "
+i+" was wrong! got: "
+input[i]+" expected: "+smArr[i]);
int offset = smArr.Length;
for(int i=0;i<bigArr.Length;i++)
if (bigArr[i]!=input[i+offset])
throw new Exception("When reading bigArr copy, position "
//.........这里部分代码省略.........
示例6: 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!");
//.........这里部分代码省略.........
示例7: StreamTest
//.........这里部分代码省略.........
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));
stream.Seek(0, SeekOrigin.Begin);
for(int i=0; i<iLength; i++){
if(stream.ReadByte() != i%256){
return false;
}
}
btArr = new Byte[iLength];
stream.Read(btArr, 0, iLength);
for(int i=0; i<iLength; i++){
if(btArr[i] != (Byte)i){
Console.WriteLine(i + " " + btArr[i] + " " + (Byte)i);
return false;
}
}
BinaryReader br1 = new BinaryReader(stream);
if(br1.ReadBoolean())
return false;
if(!br1.ReadBoolean())
return false;
for(int i=0; i<10; i++){
if(br1.ReadByte() != (Byte)i)
return false;
if(br1.ReadSByte() != (SByte)i)
return false;
if(br1.ReadInt16() != (Int16)i)
return false;
if(br1.ReadChar() != (Char)i)
return false;
if(br1.ReadUInt16() != (UInt16)i)
return false;
if(br1.ReadInt32() != i)
return false;
if(br1.ReadUInt32() != (UInt32)i)
return false;
if(br1.ReadInt64() != (Int64)i)
return false;
if(br1.ReadUInt64() != (UInt64)i)
return false;
if(br1.ReadSingle() != (Single)i)
return false;
if(br1.ReadDouble() != (Double)i)
return false;
}
chArr = br1.ReadChars(iLength);
for(int i=0; i<iLength;i++){
if(chArr[i] != (Char)i)
return false;
}
chArr = new Char[512];
chArr = br1.ReadChars(iLength/2);
for(int i=0; i<iLength/2;i++){
if(chArr[i] != (Char)(iLength/2+i))
return false;
}
chArr = new Char[iLength];
for(int i=0; i<iLength;i++)
chArr[i] = (Char)i;
strValue = br1.ReadString();
if(!strValue.Equals(new String(chArr)))
return false;
strValue = br1.ReadString();
if(!strValue.Equals(new String(chArr))){
return false;
}
try{
stream.Seek(1, SeekOrigin.Current);
return true;
}catch(Exception){
}
stream.Position = 0;
btArr = new Byte[iLength];
for(int i=0; i<iLength; i++)
btArr[i] = (Byte)(i + 5);
AsyncCallback acb1 = new AsyncCallback(new Co3965StreamMethods().AsyncTestCB);
IAsyncResult isync1 = stream.BeginWrite(btArr, 0, btArr.Length, acb1, stream.GetType().ToString());
stream.EndWrite(isync1);
stream.Position = 0;
for(int i=0; i<iLength; i++){
if(stream.ReadByte() != (Byte)(i+5))
return false;
}
stream.Position = 0;
AsyncCallback acb2 = new AsyncCallback(new Co3965StreamMethods().AsyncTestCB);
Byte[] btArr1 = new Byte[iLength];
IAsyncResult isync2 = stream.BeginRead(btArr1, 0, btArr1.Length, acb2, stream.GetType().ToString());
iValue = stream.EndRead(isync2);
if(iValue!=btArr.Length)
return false;
for(int i=0; i<iLength; i++){
if(btArr[i] != btArr1[i])
return false;
}
return true;
}
示例8: ApmFileCopy2
//
// File copy using APM performs asynchronous reads and asynchronous
// write operations.
//
static long ApmFileCopy2(Stream src, Stream dst)
{
var done = new ManualResetEventSlim(false);
long fileSize = 0;
IOException ioex = null;
int pendingWrites = 1; // Account for the last read of 0 bytes.
AsyncCallback onReadCompleted = null, onWriteCompleted = null;
onReadCompleted = delegate (IAsyncResult ar) {
int bytesRead = 0;
byte[] _rbuffer = (byte[])ar.AsyncState;
try {
bytesRead = src.EndRead(ar);
} catch (IOException _ioex) {
src.Close();
dst.Close();
ioex = _ioex;
done.Set();
return;
}
if (bytesRead != 0) {
//
// Allocate a buffer for the next read, and start write the current
// read buffer.
//
var writeFrom = (byte[])ar.AsyncState;
//
// Increment the pending writes counter and start writing
// the current read block.
//
Interlocked.Increment(ref pendingWrites);
dst.BeginWrite(writeFrom, 0, bytesRead, onWriteCompleted, bytesRead);
//
// After the write is taking place, start a new read.
// We ensure that the read block are written with the proper order.
//
var readTo = new byte[BUFFER_SIZE];
src.BeginRead(readTo, 0, readTo.Length, onReadCompleted, readTo);
} else {
// End of source file.
src.Close();
//
// Decrement the pending writes count and terminate copy if
// all writes are done.
//
if (Interlocked.Decrement(ref pendingWrites) == 0) {
//
// No writes are pending, close the destination file and
// set the *done* event.
//
dst.Close();
done.Set();
}
}
};
onWriteCompleted = delegate (IAsyncResult ar) {
int bytesWritten = (int)ar.AsyncState;
try {
dst.EndWrite(ar);
fileSize += bytesWritten;
} catch (IOException _ioex) {
dst.Close();
src.Close();
ioex = _ioex;
done.Set();
return;
}
//
// Decrement the pending writes count and terminate copy if
// all writes are done.
//
if (Interlocked.Decrement(ref pendingWrites) == 0) {
//
// No writes are pending, close the destination file and
// set the *done* event.
//
dst.Close();
done.Set();
}
//.........这里部分代码省略.........
示例9: ApmFileCopy
//
// File copy using APM asynchronous read and synchronous write operations.
//
static long ApmFileCopy(Stream src, Stream dst)
{
var done = new ManualResetEventSlim(false);
long fileSize = 0;
IOException ioex = null;
AsyncCallback onReadCompleted = null;
onReadCompleted = delegate(IAsyncResult iar) {
int bytesRead = 0;
try {
bytesRead = src.EndRead(iar);
} catch (IOException _ioex) {
src.Close();
dst.Close();
ioex = _ioex;
done.Set();
return;
}
if (bytesRead != 0) {
//
// The read buffer is passed through IasyncResult.AsyncState.
// Allocate a new buffer for the next read and issue an synchronous write.
//
// The lock ensures that we can't process the completion of the new read
// completion before terminate the current write.
//
var writeFrom = (byte[])iar.AsyncState;
var readTo = new byte[BUFFER_SIZE];
lock(dst) {
src.BeginRead(readTo, 0, readTo.Length, onReadCompleted, readTo);
dst.Write(writeFrom, 0, bytesRead);
fileSize += bytesRead;
}
} else {
//
// We reach the EOF on the source stream.
// We must ensure that the write of the last block is done,
// before close the destination stream and set the event.
//
src.Close();
lock(dst) {}
dst.Close();
done.Set();
return;
}
};
//
// Start the copy process, issuingthe first asynchronous read.
//
var firstBuf = new byte[BUFFER_SIZE];
src.BeginRead(firstBuf, 0, firstBuf.Length, onReadCompleted, firstBuf);
// Wait until completion.
done.Wait();
if (ioex != null) {
throw ioex;
}
return fileSize;
}