Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace SharedCode {
- /// <summary>
- /// In this frame any type of file can be encapsulated. After the header,
- /// 'Frame size' and 'Encoding' follows 'MIME type' [MIME] represented as
- /// as a terminated string encoded with ISO 8859-1 [ISO-8859-1]. The
- /// filename is case sensitive and is encoded as 'Encoding'. Then follows
- /// a content description as terminated string, encoded as 'Encoding'.
- /// The last thing in the frame is the actual object. The first two
- /// strings may be omitted, leaving only their terminations. MIME type is
- /// always an ISO-8859-1 text string. There may be more than one "GEOB"
- /// frame in each tag, but only one with the same content descriptor.
- ///
- /// <Header for 'General encapsulated object', ID: "GEOB">
- /// Text encoding $xx
- /// MIME type <text string> $00
- /// Filename <text string according to encoding> $00 (00)
- /// Content description <text string according to encoding> $00 (00)
- /// Encapsulated object <binary data>
- /// </summary>
- public class GeneralObjectFrameBody : FrameBody {
- public String MIMEType { get; set; }
- private byte[] mimeBytes = new byte[0];
- public String Filename { get; set; }
- private byte[] filenameBytes = new byte[0];
- public String Description { get; set; }
- private byte[] descriptionBytes = new byte[0];
- private byte[] binaryData = new byte[0];
- public GeneralObjectFrameBody (String content) {
- }
- public GeneralObjectFrameBody (byte[] bytes, int length) {
- int index = 0;
- // Generate Encoding Type
- Encoding = Convert.ToInt16 (bytes [index]);
- index += 1;
- // Mime Type
- for (int i = index; i < length; i++) {
- byte[] tempArray = new byte[mimeBytes.Length + 1];
- Array.Copy (mimeBytes, tempArray, mimeBytes.Length);
- tempArray [i - index] = bytes [i];
- mimeBytes = tempArray;
- if (mimeBytes [i - index] == 0) {
- break;
- }
- }
- this.MIMEType = TextEncoder.DecodeByteArray (mimeBytes, 0);
- index += mimeBytes.Length;
- // Filename
- for (int i = index; i < length; i++) {
- byte[] tempArray = new byte[filenameBytes.Length + 1];
- Array.Copy (filenameBytes, tempArray, filenameBytes.Length);
- tempArray [i - index] = bytes [i];
- filenameBytes = tempArray;
- if (Encoding == 0) {
- if (filenameBytes [i - index] == 0) {
- break;
- }
- } else if (Encoding == 1) {
- if (filenameBytes [(i - index)] == 0 && filenameBytes [(i - index - 1)] == 0) {
- break;
- }
- }
- }
- this.Filename = TextEncoder.DecodeByteArray (filenameBytes, Encoding);
- index += filenameBytes.Length;
- // Description
- for (int i = index; i < length; i++) {
- byte[] tempArray = new byte[descriptionBytes.Length + 1];
- Array.Copy (descriptionBytes, tempArray, descriptionBytes.Length);
- tempArray [i - index] = bytes [i];
- descriptionBytes = tempArray;
- if (descriptionBytes [i - index] == 0) {
- break;
- }
- }
- this.Description = TextEncoder.DecodeByteArray (descriptionBytes, 0);
- index += descriptionBytes.Length;
- // Content
- this.binaryData = new byte[length - index];
- Array.Copy (bytes, index, binaryData, 0, binaryData.Length);
- Content = TextEncoder.DecodeByteArray (binaryData, Encoding);
- Logger.output (Content);
- }
- public override string ToString () {
- return Content;
- }
- public override byte[] GetAsByteArray () {
- byte[] output = new byte[0];
- // Get Encoding
- byte bt = Convert.ToByte (Encoding);
- byte[] encoding = new byte[1];
- encoding [0] = bt;
- // Merge Arrays
- ArrayMerger.MergeArrays (ref output, output, encoding);
- ArrayMerger.MergeArrays (ref output, output, mimeBytes);
- ArrayMerger.MergeArrays (ref output, output, filenameBytes);
- ArrayMerger.MergeArrays (ref output, output, descriptionBytes);
- ArrayMerger.MergeArrays (ref output, output, binaryData);
- // Return the byte Array
- return output;
- }
- public override int GetContentLength () {
- return 1 + mimeBytes.Length + filenameBytes.Length + descriptionBytes.Length + binaryData.Length;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement