Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit fa455d2

Browse files
committed
Add byte[].Combine extension method
1 parent 70ae213 commit fa455d2

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/ServiceStack.Text/StreamExtensions.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.IO;
7+
using System.Linq;
78
using System.Text;
89
using ServiceStack.Text;
910

@@ -246,7 +247,7 @@ private static byte[] ReadExactlyFast(Stream fromStream, byte[] intoBuffer, int
246247

247248
public static string CollapseWhitespace(this string str)
248249
{
249-
if (str == null)
250+
if (str == null)
250251
return null;
251252

252253
var sb = new StringBuilder();
@@ -267,5 +268,22 @@ public static string CollapseWhitespace(this string str)
267268

268269
return sb.ToString();
269270
}
271+
272+
public static byte[] Combine(this byte[] bytes, params byte[][] withBytes)
273+
{
274+
var combinedLength = bytes.Length + withBytes.Sum(b => b.Length);
275+
var to = new byte[combinedLength];
276+
277+
Buffer.BlockCopy(bytes, 0, to, 0, bytes.Length);
278+
var pos = bytes.Length;
279+
280+
foreach (var b in withBytes)
281+
{
282+
Buffer.BlockCopy(b, 0, to, pos, b.Length);
283+
pos += b.Length;
284+
}
285+
286+
return to;
287+
}
270288
}
271289
}

tests/ServiceStack.Text.Tests/DataTests.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ public void serialize_GetValuesResponse()
8181

8282
Assert.That(response.Values, Has.Count.EqualTo(9));
8383
}
84-
84+
85+
[Test]
86+
public void Does_Combine_Byte()
87+
{
88+
var wordBytes = "HELLO".ToUtf8Bytes().Combine(" ".ToUtf8Bytes(), "WORLD".ToUtf8Bytes());
89+
var word = wordBytes.FromUtf8Bytes();
90+
Assert.That(word, Is.EqualTo("HELLO WORLD"));
91+
}
8592
}
8693
}

0 commit comments

Comments
 (0)