简体   繁体   English

将剩余的字节从一个字节[]连接到一个新的完整字节[]

[英]Concat remaining bytes from a byte[] to a new full byte[]

I have a TCP stream coming in and it fills up a 256 byte[]. 我有一个TCP流进来,它填充了256个字节[]。

From there I process the byte array parsing out messages etc. 从那里我处理字节数组解析出消息等。

Once I get to < 100 bytes in the first array, I want to take the remaining bytes add them to a new array and then append the new 256 byte array and continue processing so I don't miss messages. 一旦我在第一个数组中达到<100个字节,我想将剩余的字节添加到新数组中,然后追加新的256字节数组并继续处理,这样我就不会丢失消息。

    public static byte[] Combine(byte[] first, byte[] second)
    {
      byte[] ret = new byte[first.Length + second.Length];
      Buffer.BlockCopy(first, 0, ret, 0, first.Length);
      Buffer.BlockCopy(second, 0, ret, first.Length, second.Length);
      return ret;
    }        

I'm using this function (Taken from one of Jon Skeet's post) however there is an issue that happens in that the byte[] continually gets bigger. 我正在使用此函数(摘自Jon Skeet的一篇文章),但是发生了一个问题,即byte []不断变大。

For example if I have buffer[] that is 256 and newbuff[] 256 and pass it to the function above...I get back a ret of 512[]. 例如,如果我有256的buffer []和newbuff [] 256,并将其传递给上面的函数...我得到512 []的ret。

Now if I pass the Combine function again it adds on the 256 to the 512 and continuously grows causing a bit of an issue as I am processing a rather large tcp data feed. 现在,如果我再次传递Combine函数,它将256加到512上,并持续增长,这在我处理相当大的tcp数据馈送时会引起一些问题。

Any suggestions on how to make this more efficient? 关于如何提高效率的任何建议? Currently I have tried using this variation but it seems like I am caught in a circle. 目前,我已经尝试使用此变体,但是好像陷入了一个圈子。

    public static byte[] Combine(byte[] first, byte[] second, int srcOffset)
    {
     byte[] ret = new byte[first.Length -srcOffset + second.Length];
     Buffer.BlockCopy(first, srcOffset, ret, 0, first.Length - srcOffset);
     Buffer.BlockCopy(second, 0, ret, first.Length - srcOffset, second.Length);
     return ret;
   }

Thanks in advance guys! 在此先感谢大家!

Maybe this is over simplifying it you can do something like the below, I personally use memory streams withing TCP servers to keep all the data and it's worked out well. 也许这过于简化了,您可以执行以下操作,我个人将内存流与TCP服务器一起使用以保留所有数据,并且效果很好。

 public static byte[] Combine(byte[] first, byte[] second, int srcOffset)
        {
            MemoryStream result = new MemoryStream();
            result.Write(first, 0, first.Length);
            result.Write(second, 0, srcOffset);
            return result.ToArray();
        }

Your second function seems to do exactly what you want it to do. 您的第二个功能似乎完全按照您想要的去做。 I tried it out using this small test setup: 我使用此小型测试设置进行了尝试:

byte[] a = new byte[256];
byte[] b = new byte[256];
byte[] c = Combine(a,b, 100);
Console.WriteLine(c.Length);

The result was 412, so exactly what you would expect (512 bytes - 100 processed bytes = 412 bytes). 结果是412,因此完全符合您的期望(512字节-100个已处理字节= 412字节)。 Maybe there is an error in other parts of your code or i misunderstood you. 也许您的代码的其他部分有错误,或者我误解了您。 It would be very helpfull if you could provide more context for us. 如果您能为我们提供更多背景信息将非常有帮助。 (I dont have enough reputation to comment on your question so I'm posting this as an answer, sorry) (我没有足够的声誉来评论您的问题,抱歉,我将其发布为答案)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM