/// <summary> /// This method automatically increases the length of the data by the proper length, /// and copies the data from the given byte array into the underlying buffer. /// The data is copied starting at the given offset up to the given length. /// The data is inserted into the underlying buffer at the given index. /// </summary> /// <param name="index"> /// The position in this instance where insertion begins. /// </param> /// <param name="data"> /// The data to insert into the underlying buffer. /// </param> public void InsertData(int index, Data data) { InsertData(index, data.ByteArray); }
/// <summary> /// This method automatically increases the length of the data by the proper length, /// and copies the data from the given byte array into the underlying buffer. /// The data is copied starting at the given offset up to the given length. /// The data is inserted into the underlying buffer at the given index. /// </summary> /// <param name="index"> /// The position in this instance where insertion begins. /// </param> /// <param name="data"> /// The data to insert into the underlying buffer. /// </param> /// <param name="offset"> /// The offset from which to start copying data from the given data. /// </param> /// <param name="length"> /// The amount of data to copy from the given data. /// </param> public void InsertData(int index, Data data, int offset, int length) { InsertData(index, data.ByteArray, offset, length); }
/// <summary> /// This method automatically increases the length of the data by the proper length, /// and copies the bytes from the given data object into the mutable data array. /// </summary> /// <param name="data"> /// A Data object to copy bytes from. /// </param> public void AppendData(Data data) { // We're not going to bother checking to see if data is null. // The NullReferenceException will automatically get thrown for us if it is. AppendData(data.ByteArray); }
/// <summary> /// Reads from the given data, starting at the given offset and reading the given length, /// and appends the read data to the underlying buffer. /// The underlying buffer length is automatically increased as needed. /// /// This method properly handles reading from stream data (data.IsStream == true). /// </summary> /// <param name="data"> /// The data to append to the end of the underlying buffer. /// </param> /// <param name="offset"> /// The offset from which to start copying from the given data. /// </param> /// <param name="length"> /// The amount to copy from the given data. /// </param> public void AppendData(Data data, int offset, int length) { AppendData(data.ByteArray, offset, length); }
public MutableData(Data data, bool copy) : base(data, copy) { // Nothing to do here }
public MutableData(Data data, int offset, int length) : base(data, offset, length) { // Nothing to do here }
public static bool IsEqual(Data d1, int d1Offset, Data d2, int d2Offset, int length) { return IsEqual(d1.ByteArray, d1Offset, d2.ByteArray, d2Offset, length); }
public MutableData(Data data) : base(data, false) { // Nothing to do here }
public static bool IsEqual(Data d1, Data d2) { return IsEqual(d1.ByteArray, d2.ByteArray); }
/// <summary> /// Creates a new Data object using a specified subset of the given data. /// The data must necessarily be copied (otherwise it would be unsafe). /// </summary> /// <param name="buffer"> /// Byte array to use for underlying data. /// </param> /// <param name="offset"> /// The offset within data to start reading from. /// </param> /// <param name="data"> /// The amount to read from data. /// </param> public Data(Data data, int offset, int length) : this(data.ByteArray, offset, length) { // Nothing to do here }
/// <summary> /// Creates a new Data object using the given data. /// If the copy flag is set, this method will create a new buffer, and copy the buffer from the given data into it. /// Thus changes to the given data will not affect this Data object. /// Otherwise the new Data object will simply form a wrapper around the given data (without copying anything). /// /// Note: If you pass a Data object which uses an internal stream (IsStream = true), the data is always copied. /// </summary> /// <param name="buffer"> /// Byte array to use for underlying data. /// </param> /// <param name="copy"> /// Whether or not to copy data from the given buffer into a new buffer. /// </param> public Data(Data data, bool copy) : this(data.ByteArray, copy) { // Nothing to do here }
/// <summary> /// Creates a new Data object using the data. /// The data is not copied. /// That is, the new Data object is simply a wrapper around that same data. /// </summary> /// <param name="buffer"> /// Data to use as underlying data. /// </param> public Data(Data data) : this(data.ByteArray, false) { // Nothing to do here }
/// <summary> /// Reads the entire file, and stores the result in a Data object wrapping the read bytes. /// Warning: This method is only to be used for small files. /// </summary> /// <param name="filePath"> /// Relative or absolute path to file. /// </param> /// <returns> /// A regular Data object, which wraps the read bytes from the file. /// </returns> public static Data ReadFile(string filePath) { Data result = null; FileStream fs = null; try { fs = new FileStream(filePath, FileMode.Open, FileAccess.Read); result = new Data((int)fs.Length); int amountRead = fs.Read(result.ByteArray, 0, result.Length); int totalAmountRead = amountRead; while ((amountRead > 0) && (totalAmountRead < result.Length)) { amountRead = fs.Read(result.ByteArray, totalAmountRead, result.Length - totalAmountRead); totalAmountRead += amountRead; } if (totalAmountRead < result.Length) { result = null; } } catch { result = null; } finally { if (fs != null) fs.Close(); } return result; }