public sealed class MimeType : IEquatable<MimeType>
Public NotInheritable Class MimeType
Implements IEquatable(Of MimeType)
public ref class MimeType sealed : IEquatable<MimeType^>
[<SealedAttribute>]
type MimeType =
class
interface IEquatable<MimeType>
end
Building, serializing, parsing, and editing of MimeType instances:
using FolkerKinzel.MimeTypes;
namespace Examples;
public static class BuildAndParseExample
{
public static void Example()
{
const string longParameterValue = """
This is a very long parameter that will be wrapped according to RFC 2231.
It also contains a few Non-ASCII-Characters: äöß.
""";
MimeType mimeType1 =
MimeType.Create("application", "x-stuff")
.AppendParameter("first-parameter", longParameterValue, "en")
.AppendParameter("second-parameter", "Parameter with \\, = and \".");
string s = mimeType1.ToString(MimeFormats.LineWrapping);
Console.WriteLine(s);
var mimeType2 = MimeType.Parse(s);
Console.WriteLine();
Console.WriteLine("Media Type: {0}", mimeType2.MediaType);
Console.WriteLine("Sub Type: {0}", mimeType2.SubType);
Console.WriteLine();
Console.WriteLine("Is text: {0}", mimeType2.IsText);
Console.WriteLine("Is plain text: {0}", mimeType2.IsTextPlain);
Console.WriteLine("Is octet stream: {0}", mimeType2.IsOctetStream);
int parameterCounter = 1;
foreach (MimeTypeParameter parameter in mimeType2.Parameters)
{
Console.WriteLine();
Console.WriteLine($"Parameter {parameterCounter++}:");
Console.WriteLine("============");
Console.WriteLine($"Key: {parameter.Key}");
Console.WriteLine($"Language: {parameter.Language}");
Console.WriteLine("Value:");
Console.WriteLine(parameter.Value);
Console.WriteLine();
Console.WriteLine("Is charset parameter: {0}", parameter.IsCharSetParameter);
Console.WriteLine("Is ASCII charset parameter: {0}", parameter.IsAsciiCharSetParameter);
Console.WriteLine("Is access type parameter: {0}", parameter.IsAccessTypeParameter);
Console.WriteLine("Is value case sensitive: {0}", parameter.IsValueCaseSensitive);
}
// The MimeType class allows to modify the parameters:
mimeType2.RemoveParameter("first-parameter")
.AppendParameter("Second-Parameter", "normal");
Console.WriteLine();
Console.Write("mimeType2 modified: ");
Console.WriteLine(mimeType2);
}
}
/*
Console Output:
application/x-stuff;
first-parameter*0*=utf-8'en'This%20is%20a%20very%20long%20param;
first-parameter*1*=eter%20that%20will%20be%20wrapped%20accordin;
first-parameter*2*=g%20to%20RFC%202231.%0D%0AIt%20also%20contai;
first-parameter*3*=ns%20a%20few%20Non-ASCII-Characters%3A%20%C3;
first-parameter*4*=%A4%C3%B6%C3%9F.;
second-parameter="Parameter with \\, = and \"."
Media Type: application
Sub Type: x-stuff
Is text: False
Is plain text: False
Is octet stream: False
Parameter 1:
============
Key: first-parameter
Language: en
Value:
This is a very long parameter that will be wrapped according to RFC 2231.
It also contains a few Non-ASCII-Characters: äöß.
Is charset parameter: False
Is ASCII charset parameter: False
Is access type parameter: False
Is value case sensitive: True
Parameter 2:
============
Key: second-parameter
Language:
Value:
Parameter with \, = and ".
Is charset parameter: False
Is ASCII charset parameter: False
Is access type parameter: False
Is value case sensitive: True
mimeType2 modified: application/x-stuff; second-parameter=normal
*/
Formatting a MimeType instance into a standards-compliant String using several options:
using FolkerKinzel.MimeTypes;
namespace Examples;
public static class FormattingOptionsExample
{
public static void Example()
{
MimeType mimeType =
MimeType.Create("application", "x-stuff")
.AppendParameter("short", "s")
.AppendParameter("key-long",
"Very very loooong value in order to show the line wrapping");
Console.WriteLine("MimeFormats.Default:");
Console.WriteLine(mimeType.ToString());
Console.WriteLine();
Console.WriteLine("MimeFormats.IgnoreParameters:");
Console.WriteLine(mimeType.ToString(MimeFormats.IgnoreParameters));
Console.WriteLine();
Console.WriteLine("MimeFormats.AvoidSpace:");
Console.WriteLine(mimeType.ToString(MimeFormats.AvoidSpace));
Console.WriteLine();
Console.WriteLine("MimeFormats.LineWrapping:");
Console.WriteLine(mimeType.ToString(MimeFormats.LineWrapping));
Console.WriteLine();
Console.WriteLine("MimeFormats.LineWrapping | MimeFormats.AvoidSpace:");
Console.WriteLine(mimeType.ToString(MimeFormats.LineWrapping | MimeFormats.AvoidSpace));
Console.WriteLine();
Console.WriteLine("MimeFormats.Url:");
Console.WriteLine(mimeType.ToString(MimeFormats.Url));
Console.WriteLine();
}
}
/*
Console Output:
MimeFormats.Default:
application/x-stuff; short=s; key-long="Very very loooong value in order to show the line wrapping"
MimeFormats.IgnoreParameters:
application/x-stuff
MimeFormats.AvoidSpace:
application/x-stuff;short=s;key-long="Very very loooong value in order to show the line wrapping"
MimeFormats.LineWrapping:
application/x-stuff; short=s;
key-long*0="Very very loooong value in order to show the line ";
key-long*1="wrapping"
MimeFormats.LineWrapping | MimeFormats.AvoidSpace:
application/x-stuff;short=s;
key-long*0="Very very loooong value in order to show the line ";
key-long*1="wrapping"
MimeFormats.Url:
application/x-stuff;short=s;key-long*=utf-8''Very%20very%20loooong%20value%20in%20order%20to%20show%20the%20line%20wrapping
*/
Comparison of MimeType instances:
using FolkerKinzel.MimeTypes;
namespace Examples;
public static class EqualityExample
{
public static void Example()
{
const string media1 = "text/plain; charset=us-ascii";
const string media2 = "TEXT/PLAIN";
const string media3 = "TEXT/HTML";
const string media4 = "text/plain; charset=iso-8859-1";
const string media5 = "TEXT/PLAIN; CHARSET=ISO-8859-1";
const string media6 = "text/plain; charset=iso-8859-1; other-parameter=other_value";
const string media7 = "text/plain; OTHER-PARAMETER=other_value; charset=ISO-8859-1";
const string media8 = "text/plain; charset=iso-8859-1; other-parameter=OTHER_VALUE";
if (MimeType.Parse(media1) == MimeType.Parse(media2) &&
MimeType.Parse(media2) != MimeType.Parse(media3) &&
MimeType.Parse(media2) != MimeType.Parse(media4) &&
MimeType.Parse(media2).Equals(MimeType.Parse(media4), ignoreParameters: true) &&
MimeType.Parse(media4) == MimeType.Parse(media5) &&
MimeType.Parse(media4) != MimeType.Parse(media6) &&
MimeType.Parse(media6) == MimeType.Parse(media7) &&
MimeType.Parse(media6) != MimeType.Parse(media8))
{
Console.WriteLine("Success");
}
else
{
Console.WriteLine("Error");
}
}
}
// Console Output: Success
IsOctetStream | Indicates whether this instance is equal to OctetStream. The parameters are not taken into account. The comparison is case-insensitive. |
IsText | Determines whether the MediaType of this instance equals "text". The comparison is case-insensitive. |
IsTextPlain | Indicates whether this instance is equal to the MIME type "text/plain". The parameters are not taken into account. The comparison is case-insensitive. |
MediaType | Gets the Top-Level Media Type. (The left part of a MIME-Type.) |
Parameters | Gets the parameters. |
SubType | Gets the Sub Type. (The right part of a MIME-Type.) |
AppendParameter | Appends a MimeTypeParameter to the end of the MimeType. |
AppendTo | Appends a String representation of this instance according to RFC 2045 and RFC 2231 to the end of a StringBuilder. |
AsInfo | Gets the MimeTypeInfo of the MimeTypes content. |
ClearParameters | Removes all MimeTypeParameters. |
Create(MimeTypeInfo) | Creates a new MimeType object that's filled with the data of an existing MimeTypeInfo instance. |
Create(String, String) | Creates a new MimeType object. |
Equals(MimeType) | Determines whether the value of this instance is equal to the value of other. The Parameters are taken into account. |
Equals(Object) |
Determines whether obj is a MimeType object
whose
value is equal to that of this instance. The Parameters
are taken into account.
(Overrides ObjectEquals(Object)) |
Equals(MimeType, Boolean) | Determines whether this instance is equal to other and allows to specify whether or not the Parameters are taken into account. |
FromFileName(ReadOnlySpanChar) | Creates an appropriate MimeType instance for a given file name. |
FromFileName(String) | Creates an appropriate MimeType instance for a given file name. |
GetFileTypeExtension | Gets an appropriate file type extension. |
GetHashCode |
Creates a hash code for this instance, which takes the Parameters
into account.
(Overrides ObjectGetHashCode) |
GetHashCode(Boolean) | Creates a hash code for this instance and allows to specify whether or not the Parameters are taken into account. |
GetType | Gets the Type of the current instance. (Inherited from Object) |
Parse(ReadOnlyMemoryChar) | Parses a ReadOnlyMemory<Char> as MimeType. |
Parse(String) | Parses a String as MimeType. |
RemoveParameter | Removes the MimeTypeParameter with the specified Key from the MimeType if such a parameter has existed. |
ToString |
Serializes the instance as Internet Media Type String ("MIME type") using
the Default format.
(Overrides ObjectToString) |
ToString(MimeFormats, Int32) | Serializes the instance as Internet Media Type String ("MIME type") with several options. |
TryParse(ReadOnlyMemoryChar, MimeType) | Tries to parse a ReadOnlyMemory<Char> as MimeType. |
TryParse(String, MimeType) | Tries to parse a String as MimeType. |
Equality(MimeType, MimeType) | Returns a value that indicates whether two specified MimeType instances are equal. The Parameters are taken into account. |
Inequality(MimeType, MimeType) | Returns a value that indicates whether two specified MimeType instances are not equal. The Parameters are taken into account. |
MinLineLength | Minimum count of characters at which a line of an Internet Media Type String is wrapped. |