Nearly all warnings were due to nullability issues, since the concept of nullable reference types did not exist when the DataStream code was originally written. * Compression related files - BZip2InputStream (disable nullability warnings) - BZip2OutputStream (disable nullability warnings) - DeflaterHuffman (disable nullability warnings) - Inflater (disable nullability warnings) - InflaterDynHeader (disable nullability warnings) - ParallelJoinTest (disable nullability warnings) - TarHeader - TarInputStream (disable nullability warnings) - ZipEntry - ZipFile - ZipInputStream * AsyncTCPConnection_APM * AsyncTCPConnection_APM * InflaterHuffmanTree * ManagedSubscriber * ManagedTCPMessageHub * MapStream * PubSubClientMessage - Normalize indentation/whitespace - Fix some typos - Address nullability issues to fix build warnings - Modernize code * PubSubMessageReactor * PubSubServerManagedTCPConnection * PubSubSingleTagStateEvent * PubSubState * PublisherCore * PubsubDeserializationController * RemoteMessage - make 'message' nullable to resolve warnings elsewhere (in calling code) * SharedStreamConfig * SingleTagData * SubscriberCore * SuperMapStream * SystemHeartbeatPublisher * SystemStatsPublisher * SystemSubscriptionMapPublisher * ThreadSafeQueue * TradeBarsStream Only use Registry on windows.
115 lines
3.9 KiB
C#
115 lines
3.9 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Collections;
|
|
using bts;
|
|
using System.Threading;
|
|
using System.Reflection;
|
|
|
|
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 ??= string.Empty;
|
|
int len = str.Length;
|
|
bw.Write(len);
|
|
foreach (char c_orig in str) {
|
|
char c = c_orig > 127 ? 'A' : c_orig;
|
|
long p0 = bw.BaseStream.Position;
|
|
bw.Write(c);
|
|
long p1 = bw.BaseStream.Position;
|
|
int mv = Convert.ToInt32(p1 - p0);
|
|
if (mv > 1) {
|
|
Log.assert(false);
|
|
}
|
|
}
|
|
}
|
|
|
|
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();
|
|
}
|
|
|
|
public static byte[] read_bytes(Stream str) {
|
|
var init_pos = str.Position;
|
|
str.Position = 0;
|
|
BinaryReader br = new BinaryReader(str);
|
|
return br.ReadBytes((int)(str.Length - str.Position));
|
|
}
|
|
|
|
/*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) };
|
|
|
|
|
|
}
|
|
}
|
|
}
|