मैं के इस संयोजन का उपयोग करें:
public MyClass(Stream inputStream) {
byte[] inputBuffer = new byte[inputStream.Length];
inputStream.Read(inputBuffer, 0, inputBuffer.Length);
_ms = new MemoryStream(inputBuffer);
}
Stream
वस्तु Length
विशेषता को लागू नहीं करता है, तो आप कुछ इस तरह लागू करना होगा विस्तार विधियां:
public static Stream Copy(this Stream source)
{
if (source == null)
return null;
long originalPosition = -1;
if (source.CanSeek)
originalPosition = source.Position;
MemoryStream ms = new MemoryStream();
try
{
Copy(source, ms);
if (originalPosition > -1)
ms.Seek(originalPosition, SeekOrigin.Begin);
else
ms.Seek(0, SeekOrigin.Begin);
return ms;
}
catch
{
ms.Dispose();
throw;
}
}
public static void Copy(this Stream source, Stream target)
{
if (source == null)
throw new ArgumentNullException("source");
if (target == null)
throw new ArgumentNullException("target");
long originalSourcePosition = -1;
int count = 0;
byte[] buffer = new byte[0x1000];
if (source.CanSeek)
{
originalSourcePosition = source.Position;
source.Seek(0, SeekOrigin.Begin);
}
while ((count = source.Read(buffer, 0, buffer.Length)) > 0)
target.Write(buffer, 0, count);
if (originalSourcePosition > -1)
{
source.Seek(originalSourcePosition, SeekOrigin.Begin);
}
}
एक बार स्ट्रीम होने के बाद, इसे मेमोरीस्ट्रीम में परिवर्तित करने में परेशानी क्यों होती है? क्या आप सीधे स्ट्रीम के साथ काम नहीं कर सकते? –
मुझे अन्य निर्भरताओं के कारण मेमोरीस्ट्रीम की आवश्यकता है। – fearofawhackplanet