Files
spark/src/DataStreamSuite/PubSub/Misc/StreamUtilityMethods.cs
T

99 lines
3.4 KiB
C#

using System;
using System.IO;
using System.Collections;
namespace BCG {
/// <summary>
/// Collection of utility methods used throughout
/// </summary>
public static class StreamUtilityMethods {
// extension method to write a string C++ style (len prefixed)
public static void write_string_cpp(this BinaryWriter bw, string str) {
str = str ?? String.Empty;
int len = str.Length;
bw.Write(len);
foreach (char c in str) {
bw.Write(c);
}
}
public static string read_string_cpp(this BinaryReader br) {
int len = br.ReadInt32();
string rv = String.Empty;
char[] c_arr = br.ReadChars(len);
return new string(c_arr);
}
public static Stream getAggregatedStream(Stream[] streams) {
if (streams.Length == 1)
return streams[0];
else {
BinaryWriter bw = new BinaryWriter(new MemoryStream());
foreach (Stream s in streams) {
bw.Write(new BinaryReader(s).ReadBytes(Convert.ToInt32(s.Length - s.Position)));
}
bw.BaseStream.Position = 0;
return bw.BaseStream;
}
}
public static string first_n_bytes_to_string(byte[] b, int n) {
string rv = String.Empty;
var sz = Math.Min(n, b.Length);
for (int i = 0; i < sz; i++) {
rv += $"{b[i]} ";
}
rv.TrimEnd();
return rv;
}
public static void copyTo(Stream[] sourceStreams, Stream target) {
long initPos = target.Position;
BinaryWriter bw = new BinaryWriter(target);
foreach (Stream s in sourceStreams) {
bw.Write(new BinaryReader(s).ReadBytes(Convert.ToInt32(s.Length - s.Position)));
}
bw.BaseStream.Position = initPos;
}
public static byte[] getBytesFromStream(params Stream[] streams) {
long totalSize = 0;
BinaryWriter bw = new BinaryWriter(new MemoryStream());
foreach (Stream s in streams)
totalSize += s.Length - s.Position;
bw.Write(Convert.ToInt32(totalSize));
foreach (Stream s in streams) {
BinaryReader br = new BinaryReader(s);
int lengthToReadForThisStream = Convert.ToInt32(s.Length - s.Position);
bw.Write(br.ReadBytes(lengthToReadForThisStream));
}
return (bw.BaseStream as MemoryStream).ToArray();
}
/*takes the input stream and splits it into two streams at the given position. */
public static Stream[] splitStream(Stream stream, int splitLocation) {
stream.Position = 0;
byte[] streamBytes = getBytesFromStream(new Stream[] { stream });
byte[] firstStreamBytes = new byte[splitLocation];
byte[] secondStreamBytes = new byte[stream.Length - splitLocation];
for (int i = 0; i < stream.Length; ++i) {
//for some resaon, the 'getBytesFromStream' function prepends the length of the byte array to the bytes. This means
//we have to ignore those bytes here.
if (i < splitLocation)
firstStreamBytes[i] = streamBytes[i + 4];
else
secondStreamBytes[i - splitLocation] = streamBytes[i + 4];
}
return new Stream[] { new MemoryStream(firstStreamBytes), new MemoryStream(secondStreamBytes) };
}
}
}