Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
BinaryCodec.cs
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Threading.Tasks;
6
8{
13 {
17 public BinaryCodec()
18 {
19 }
20
24 public const string DefaultContentType = "application/octet-stream";
25
29 public static readonly string[] BinaryContentTypes = new string[]
30 {
32 "binary"
33 };
34
38 public static readonly string[] BinaryFileExtensions = new string[]
39 {
40 "bin"
41 };
42
46 public string[] ContentTypes => BinaryContentTypes;
47
52
59 public bool Decodes(string ContentType, out Grade Grade)
60 {
61 if (Array.IndexOf(BinaryContentTypes, ContentType) >= 0)
62 {
63 Grade = Grade.Ok;
64 return true;
65 }
66 else
67 {
68 Grade = Grade.NotAtAll;
69 return false;
70 }
71 }
72
83 public Task<object> DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair<string, string>[] Fields, Uri BaseUri)
84 {
85 return Task.FromResult<object>(Data);
86 }
87
94 public bool TryGetContentType(string FileExtension, out string ContentType)
95 {
96 if (string.Compare(FileExtension, "bin", true) == 0)
97 {
98 ContentType = DefaultContentType;
99 return true;
100 }
101 else
102 {
103 ContentType = string.Empty;
104 return false;
105 }
106 }
107
114 public bool TryGetFileExtension(string ContentType, out string FileExtension)
115 {
116 switch (ContentType.ToLower())
117 {
119 case "binary":
120 FileExtension = "bin";
121 return true;
122
123 default:
124 FileExtension = string.Empty;
125 return false;
126 }
127 }
128
136 public bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
137 {
138 if (InternetContent.IsAccepted(BinaryContentTypes, AcceptedContentTypes) &&
139 (Object is byte[]))
140 {
141 Grade = Grade.Ok;
142 return true;
143 }
144 else if (Object is EncodedObject Obj && InternetContent.IsAccepted(new string[] { Obj.ContentType }, AcceptedContentTypes))
145 {
146 Grade = Grade.Ok;
147 return true;
148 }
149 else
150 {
151 Grade = Grade.NotAtAll;
152 return false;
153 }
154 }
155
164 public Task<KeyValuePair<byte[], string>> EncodeAsync(object Object, Encoding Encoding, params string[] AcceptedContentTypes)
165 {
166 if (Object is byte[] Bin)
167 return Task.FromResult(new KeyValuePair<byte[], string>(Bin, DefaultContentType));
168 else if (Object is EncodedObject Obj)
169 return Task.FromResult(new KeyValuePair<byte[], string>(Obj.Data, Obj.ContentType));
170 else
171 throw new ArgumentException("Unable to encode as binary.", nameof(Object));
172 }
173 }
174}
Task< KeyValuePair< byte[], string > > EncodeAsync(object Object, Encoding Encoding, params string[] AcceptedContentTypes)
Encodes an object.
Definition: BinaryCodec.cs:164
static readonly string[] BinaryFileExtensions
Binary content types.
Definition: BinaryCodec.cs:38
const string DefaultContentType
text/plain
Definition: BinaryCodec.cs:24
bool TryGetContentType(string FileExtension, out string ContentType)
Tries to get the content type of an item, given its file extension.
Definition: BinaryCodec.cs:94
Task< object > DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair< string, string >[] Fields, Uri BaseUri)
Decodes an object.
Definition: BinaryCodec.cs:83
bool TryGetFileExtension(string ContentType, out string FileExtension)
Tries to get the file extension of an item, given its Content-Type.
Definition: BinaryCodec.cs:114
string[] FileExtensions
Supported file extensions.
Definition: BinaryCodec.cs:51
bool Decodes(string ContentType, out Grade Grade)
If the decoder decodes an object with a given content type.
Definition: BinaryCodec.cs:59
static readonly string[] BinaryContentTypes
Binary content types.
Definition: BinaryCodec.cs:29
string[] ContentTypes
Supported content types.
Definition: BinaryCodec.cs:46
bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
If the encoder encodes a given object.
Definition: BinaryCodec.cs:136
Represents an encoded object.
Definition: EncodedObject.cs:7
Static class managing encoding and decoding of internet content.
static bool IsAccepted(string ContentType, params string[] AcceptedContentTypes)
Checks if a given content type is acceptable.
Basic interface for Internet Content decoders. A class implementing this interface and having a defau...
Basic interface for Internet Content encoders. A class implementing this interface and having a defau...
Grade
Grade enumeration
Definition: Grade.cs:7