Merging changes
This commit is contained in:
1 parent
c4216d2272
commit
797816be7e
437 files changed
+54228
-28
No files matched your search
@@ -0,0 +1,83 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Collections;
|
||||
|
||||
namespace BCG
|
||||
{
|
||||
/// <summary>
|
||||
/// Collection of utility methods used throughout
|
||||
/// </summary>
|
||||
public class StreamUtilityMethods
|
||||
{
|
||||
public StreamUtilityMethods() {}
|
||||
|
||||
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 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)};
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user