有没有一种简单的方法或方法可以将其Stream转换为byte[]C#?
是否可以使用C#中的显式类型转换将基类对象分配给派生类引用?
我试过它,它会产生运行时错误.
在序列化期间,我们可以使用内存流或文件流.
这两者之间的基本区别是什么?记忆流意味着什么?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
namespace Serilization
{
class Program
{
static void Main(string[] args)
{
MemoryStream aStream = new MemoryStream();
BinaryFormatter aBinaryFormat = new BinaryFormatter();
aBinaryFormat.Serialize(aStream, person);
aStream.Close();
}
}
}
Run Code Online (Sandbox Code Playgroud) 我不太了解C#中的流.现在我有一个流放入流阅读器并阅读它.稍后在其他一些方法中我需要读取流(相同的流对象),但这次我得到了这个错误
System.ArgumentException was unhandled by user code
Message="Stream was not readable."
Source="mscorlib"
StackTrace:
at System.IO.StreamReader..ctor(Stream stream, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize)
at System.IO.StreamReader..ctor(Stream stream)
at ExtractTitle(Stream file) in :line 33
at GrabWebPage(String webPath) in :line 62
at lambda_method(ExecutionScope , ControllerBase , Object[] )
at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClassa.<InvokeActionMethodWithFilters>b__7()
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
InnerException:
Run Code Online (Sandbox Code Playgroud)
所以我想也许是通过阅读流程到最后.然后,当我尝试再次读取它时,它位于流的末尾,这就是为什么我收到此错误.
那么有人可以对此有所了解吗?
谢谢
可能重复:
从流创建字节数组
我正在尝试在内存中创建文本文件并编写它byte[].我怎样才能做到这一点?
public byte[] GetBytes()
{
MemoryStream fs = new MemoryStream();
TextWriter tx = new StreamWriter(fs);
tx.WriteLine("1111");
tx.WriteLine("2222");
tx.WriteLine("3333");
tx.Flush();
fs.Flush();
byte[] bytes = new byte[fs.Length];
fs.Read(bytes,0,fs.Length);
return bytes;
}
Run Code Online (Sandbox Code Playgroud)
但由于数据长度,它不起作用
我想知道如何将流转换为字节.
我发现这个代码,但在我的情况下,它不起作用:
var memoryStream = new MemoryStream();
paramFile.CopyTo(memoryStream);
byte[] myBynary = memoryStream.ToArray();
myBinary = memoryStream.ToArray();
Run Code Online (Sandbox Code Playgroud)
但在我的情况下,在行paramFile.CopyTo(memoryStream)它没有发生任何异常,应用程序仍然有效,但代码不能继续下一行.
谢谢.
以下代码将始终抛出UnuthorizedAccessException(无法访问MemoryStream的内部缓冲区.)
byte[] buf1 = { 2, 3, 5, 7, 11 };
var ms = new MemoryStream(buf1);
byte[] buf2 = ms.GetBuffer(); // exception will be thrown here
Run Code Online (Sandbox Code Playgroud)
这是一个普通的旧控制台应用程序,我作为管理员运行.我无法想象我可以给出这个代码更具特权的设置.那么为什么我不能进入这个缓冲区呢?(如果没有人可以,GetBuffer方法有什么意义?)
MSDN文档说
要使用公共可见缓冲区创建MemoryStream实例,请使用MemoryStream,MemoryStream(array [],Int32,Int32,Boolean,Boolean)或MemoryStream(Int32).
我不这样做吗?
PS我不想使用ToArray(),因为它会复制.
我正在使用VS 2010,并在我当前的项目中使用C#中的大量流.我已经编写了一些流转储实用程序来写出某些类型的流用于调试目的,但是当我调试时需要查看我正在调试的流时,我似乎一直磕磕绊绊,但我没有放我的在那里转储电话.看起来我应该能够以某种方式使用VS转储流,或者告诉它在调试器中调用我的一个转储方法.有没有想做到这一点?
我正在使用的流有一些描述blob数据的文本,然后是blob的字节,所以查看描述是有用的.我的转储方法通常只是将信息转储出来,然后跳过blob.
我需要类似于ReadToEnd或ReadAllBytes的东西来使用MappedViewAccessor读取MemoryMappedFile的所有内容,如果我不知道它的大小,我该怎么办呢?
我已经搜索过了,我已经看到了这个问题,但这不是我要找的东西:
编辑:
有一个问题,(int)stream.Length没有给我正确的长度,而是给出了内部缓冲区的大小!我需要刷新这个问题,因为它非常紧迫.
在你去使用Jon Skeet的回答之前,我不能.我正在使用这个开头很棒的流,但现在,我遇到了一个小问题.我现在处于一个我不知道我期望的数据量的地方,所以我认为我会使用Jon Skeet提供的方法但不幸的是它等待packet = mPackets.Take()所有数据包都已被读取.
我不知道如何解决这个问题,流做了它应该做的事情(在开始时),但不幸的是,这是一个我不希望它做它为...做的事情的场合.
我是否应该尝试重写waitingstream或者是否有一种不同的readall方法可以在我的情况下更好地工作?
我已经尝试在没有更多可用数据包的时候返回0(当我想要它全部读取而不是等待时)但是它会抛出一个IO end of stream exception.
我真的很茫然,我知道我为自己挖了一个深洞......
到目前为止,我还没有能够提出一个更好的解决方案,而不是从总长度中减去TLS标题和GCM IV和MAC(如下所述)这对于我现在使用的1套件工作正常,但不能很好地吹嘘用于未来的密码套件.
关于我为什么这样做的一些背景:
我使用https://github.com/bcgit/bc-csharp来创建基于TLS的加密,但是由于限制,我无法为其分配TCP流,所有我得到的是来自TCP层的字符串,我不能为它提供一个propper TCP流.
我正在使用mockPskTlsServer和客户端,我已经创建了它:
WaitingStream mStdin = new WaitingStream();
WaitingStream mStdout = new WaitingStream();
CryptoPskTlsClient mServer = new CryptoPskTlsClient(null);
SecureRandom secureRandom = new SecureRandom();
TlsClientProtocol TlsProtocol = new TlsServerProtocol(Stdin, Stdout, secureRandom);
TlsProtocol.Connect(mServer);
Run Code Online (Sandbox Code Playgroud)
这很好用,我还在等待流中添加了一个事件,当流的输出时会触发该事件.这样我就可以在创建它时获得多个握手消息.
这一切都运行良好,本来可以做得更好(少了......被黑了?)但它确实做了它应该做的事情.
我知道如何让它工作,但这是一个HACK,我正在寻找一种优雅的方式来做到这一点.以下是其中一个HACKS的代码示例:
public override string DecryptData(string ciphertext)
{
byte[] ciphertextBuff = ASCIIEncoding.Default.GetBytes(ciphertext);
mStdin.Write(ciphertextBuff, 0, …Run Code Online (Sandbox Code Playgroud) c# ×9
stream ×4
.net ×2
memorystream ×2
accessor ×1
base-class ×1
bouncycastle ×1
byte ×1
bytearray ×1
casting ×1
exception ×1
explicit ×1
filestream ×1
getbuffer ×1
inputstream ×1
ipc ×1
ssl ×1
windows ×1