/// <summary> /// Will write the data describing this metadata block to the given stream. /// </summary> /// <param name="targetStream">Stream to write the data to.</param> public override void WriteBlockData(Stream targetStream) { // This is where the header will come long headerPosition = targetStream.Position; // Moving along, we'll write the header last! targetStream.Seek(4, SeekOrigin.Current); // 32-bit picture type targetStream.Write(BinaryDataHelper.GetBytesUInt32((uint)this.pictureType), 0, 4); byte[] mimeTypeData = Encoding.ASCII.GetBytes(this.mimeType); // Length of the MIME type string (in bytes ...) targetStream.Write(BinaryDataHelper.GetBytesUInt32((uint)mimeTypeData.Length), 0, 4); // Only allows printable ascii characters (0x20 - 0x7e) for (int i = 0; i < mimeTypeData.Length; i++) { if (mimeTypeData[i] < 0x20 || mimeTypeData[i] > 0x7e) { // Make sure we write the text correctly as specified by the format. mimeTypeData[i] = 0x20; } } targetStream.Write(mimeTypeData, 0, mimeTypeData.Length); byte[] descriptionData = Encoding.UTF8.GetBytes(this.description); // Length of the description string (in bytes ...) targetStream.Write(BinaryDataHelper.GetBytesUInt32((uint)descriptionData.Length), 0, 4); // The description of the picture (in UTF-8) targetStream.Write(descriptionData, 0, descriptionData.Length); targetStream.Write(BinaryDataHelper.GetBytesUInt32((uint)this.width), 0, 4); targetStream.Write(BinaryDataHelper.GetBytesUInt32((uint)this.height), 0, 4); targetStream.Write(BinaryDataHelper.GetBytesUInt32((uint)this.colorDepth), 0, 4); targetStream.Write(BinaryDataHelper.GetBytesUInt32((uint)this.colors), 0, 4); // If --> is the mime type, the data contains the URL ... if (this.mimeType == "-->") { this.data = Encoding.ASCII.GetBytes(this.url); } if (this.data == null) { this.data = new byte[] { }; } targetStream.Write(BinaryDataHelper.GetBytesUInt32((uint)this.data.Length), 0, 4); targetStream.Write(this.data, 0, this.data.Length); // Writing the header, now we have the required information on the variable length fields CalculateMetadataBlockLength((uint)mimeTypeData.Length, (uint)descriptionData.Length, (uint)this.data.Length); long currentPosition = targetStream.Position; targetStream.Position = headerPosition; this.Header.WriteHeaderData(targetStream); targetStream.Position = currentPosition; }
/// <summary> /// Will write the data describing this metadata block to the given stream. /// </summary> /// <param name="targetStream">Stream to write the data to.</param> public override void WriteBlockData(Stream targetStream) { this.Header.MetaDataBlockLength = 4 + (uint)this.applicationData.Length; this.Header.WriteHeaderData(targetStream); targetStream.Write(BinaryDataHelper.GetBytesUInt32(this.applicationID), 0, 4); targetStream.Write(this.applicationData, 0, this.applicationData.Length); }
/// <summary> /// Will write the data describing this metadata block to the given stream. /// </summary> /// <param name="targetStream">Stream to write the data to.</param> public override void WriteBlockData(Stream targetStream) { uint totalLength = 0; long headerPosition = targetStream.Position; this.Header.WriteHeaderData(targetStream); // Write the vendor string (first write the length as a 32-bit uint and then the actual bytes byte[] vendorData = System.Text.Encoding.UTF8.GetBytes(this.vendor); byte[] number = BinaryDataHelper.GetBytesUInt32((uint)vendorData.Length); targetStream.Write(BinaryDataHelper.SwitchEndianness(number, 0, 4), 0, 4); targetStream.Write(vendorData, 0, vendorData.Length); totalLength += 4 + (uint)vendorData.Length; // Length of list of user comments (first a 32-bit uint, then the actual comments) number = BinaryDataHelper.GetBytesUInt32((uint)this.comments.Count); targetStream.Write(BinaryDataHelper.SwitchEndianness(number, 0, 4), 0, 4); totalLength += 4; foreach (var comment in this.comments) { foreach (var value in comment.Value) { string commentText = string.Format("{0}={1}", comment.Key, value); byte[] commentData = System.Text.Encoding.UTF8.GetBytes(commentText); number = BinaryDataHelper.GetBytesUInt32((uint)commentData.Length); targetStream.Write(BinaryDataHelper.SwitchEndianness(number, 0, 4), 0, 4); targetStream.Write(commentData, 0, commentData.Length); totalLength += 4 + (uint)commentData.Length; } } long endPosition = targetStream.Position; targetStream.Seek(headerPosition, SeekOrigin.Begin); this.Header.MetaDataBlockLength = totalLength; this.Header.WriteHeaderData(targetStream); targetStream.Seek(endPosition, SeekOrigin.Begin); // Note: FLAC does NOT have the framing bit for vorbis so we don't have to write this. }