Advertisement
Guest User

GeneralObjectFrameBody

a guest
Oct 9th, 2015
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.15 KB | None | 0 0
  1. using System;
  2.  
  3. namespace SharedCode {
  4.     /// <summary>
  5.     /// In this frame any type of file can be encapsulated. After the header,
  6.     /// 'Frame size' and 'Encoding' follows 'MIME type' [MIME] represented as
  7.     /// as a terminated string encoded with ISO 8859-1 [ISO-8859-1]. The
  8.     /// filename is case sensitive and is encoded as 'Encoding'. Then follows
  9.     /// a content description as terminated string, encoded as 'Encoding'.
  10.     /// The last thing in the frame is the actual object. The first two
  11.     /// strings may be omitted, leaving only their terminations. MIME type is
  12.     /// always an ISO-8859-1 text string. There may be more than one "GEOB"
  13.     /// frame in each tag, but only one with the same content descriptor.
  14.     ///
  15.     /// <Header for 'General encapsulated object', ID: "GEOB">
  16.     ///     Text encoding          $xx
  17.     ///     MIME type              <text string> $00
  18.     ///     Filename               <text string according to encoding> $00 (00)
  19.     ///     Content description    <text string according to encoding> $00 (00)
  20.     ///     Encapsulated object    <binary data>
  21.     /// </summary>
  22.     public class GeneralObjectFrameBody : FrameBody {
  23.  
  24.         public String MIMEType { get; set; }
  25.  
  26.         private byte[] mimeBytes = new byte[0];
  27.  
  28.         public String Filename { get; set; }
  29.  
  30.         private byte[] filenameBytes = new byte[0];
  31.  
  32.         public String Description { get; set; }
  33.  
  34.         private byte[] descriptionBytes = new byte[0];
  35.  
  36.         private byte[] binaryData = new byte[0];
  37.  
  38.         public GeneralObjectFrameBody (String content) {
  39.         }
  40.  
  41.         public GeneralObjectFrameBody (byte[] bytes, int length) {
  42.             int index = 0;
  43.  
  44.             // Generate Encoding Type
  45.             Encoding = Convert.ToInt16 (bytes [index]);
  46.  
  47.             index += 1;
  48.  
  49.             // Mime Type
  50.             for (int i = index; i < length; i++) {
  51.                 byte[] tempArray = new byte[mimeBytes.Length + 1];
  52.                 Array.Copy (mimeBytes, tempArray, mimeBytes.Length);
  53.                 tempArray [i - index] = bytes [i];
  54.  
  55.                 mimeBytes = tempArray;
  56.                 if (mimeBytes [i - index] == 0) {
  57.                     break;
  58.                 }
  59.             }
  60.  
  61.             this.MIMEType = TextEncoder.DecodeByteArray (mimeBytes, 0);
  62.             index += mimeBytes.Length;
  63.  
  64.             // Filename
  65.             for (int i = index; i < length; i++) {
  66.                 byte[] tempArray = new byte[filenameBytes.Length + 1];
  67.                 Array.Copy (filenameBytes, tempArray, filenameBytes.Length);
  68.                 tempArray [i - index] = bytes [i];
  69.  
  70.                 filenameBytes = tempArray;
  71.  
  72.                 if (Encoding == 0) {
  73.                     if (filenameBytes [i - index] == 0) {
  74.                         break;
  75.                     }
  76.                 } else if (Encoding == 1) {
  77.                     if (filenameBytes [(i - index)] == 0 && filenameBytes [(i - index - 1)] == 0) {
  78.                         break;
  79.                     }
  80.                 }
  81.             }
  82.  
  83.             this.Filename = TextEncoder.DecodeByteArray (filenameBytes, Encoding);
  84.             index += filenameBytes.Length;
  85.  
  86.  
  87.             // Description
  88.             for (int i = index; i < length; i++) {
  89.                 byte[] tempArray = new byte[descriptionBytes.Length + 1];
  90.                 Array.Copy (descriptionBytes, tempArray, descriptionBytes.Length);
  91.                 tempArray [i - index] = bytes [i];
  92.  
  93.                 descriptionBytes = tempArray;
  94.                 if (descriptionBytes [i - index] == 0) {
  95.                     break;
  96.                 }
  97.             }
  98.  
  99.             this.Description = TextEncoder.DecodeByteArray (descriptionBytes, 0);
  100.             index += descriptionBytes.Length;
  101.  
  102.  
  103.             // Content
  104.             this.binaryData = new byte[length - index];
  105.             Array.Copy (bytes, index, binaryData, 0, binaryData.Length);
  106.  
  107.             Content = TextEncoder.DecodeByteArray (binaryData, Encoding);
  108.             Logger.output (Content);
  109.         }
  110.  
  111.         public override string ToString () {
  112.             return Content;
  113.         }
  114.  
  115.         public override byte[] GetAsByteArray () {
  116.             byte[] output = new byte[0];
  117.  
  118.             // Get Encoding
  119.             byte bt = Convert.ToByte (Encoding);
  120.             byte[] encoding = new byte[1];
  121.             encoding [0] = bt;
  122.  
  123.             // Merge Arrays
  124.             ArrayMerger.MergeArrays (ref output, output, encoding);
  125.             ArrayMerger.MergeArrays (ref output, output, mimeBytes);
  126.             ArrayMerger.MergeArrays (ref output, output, filenameBytes);
  127.             ArrayMerger.MergeArrays (ref output, output, descriptionBytes);
  128.             ArrayMerger.MergeArrays (ref output, output, binaryData);
  129.  
  130.             // Return the byte Array
  131.             return output;
  132.         }
  133.  
  134.         public override int GetContentLength () {
  135.             return 1 + mimeBytes.Length + filenameBytes.Length + descriptionBytes.Length + binaryData.Length;
  136.         }
  137.     }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement