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;
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 "key",
42 "bak"
43 };
44
48 public string[] ContentTypes => BinaryContentTypes;
49
54
61 public bool Decodes(string ContentType, out Grade Grade)
62 {
63 if (Array.IndexOf(BinaryContentTypes, ContentType) >= 0)
64 {
65 Grade = Grade.Ok;
66 return true;
67 }
68 else
69 {
70 Grade = Grade.NotAtAll;
71 return false;
72 }
73 }
74
85 public Task<ContentResponse> DecodeAsync(string ContentType, byte[] Data, Encoding Encoding,
86 KeyValuePair<string, string>[] Fields, Uri BaseUri, ICodecProgress Progress)
87 {
88 return Task.FromResult(new ContentResponse(ContentType, Data, Data));
89 }
90
97 public bool TryGetContentType(string FileExtension, out string ContentType)
98 {
99 if (string.Compare(FileExtension, "bin", true) == 0)
100 {
101 ContentType = DefaultContentType;
102 return true;
103 }
104 else
105 {
106 ContentType = string.Empty;
107 return false;
108 }
109 }
110
117 public bool TryGetFileExtension(string ContentType, out string FileExtension)
118 {
119 switch (ContentType.ToLower())
120 {
122 case "binary":
123 FileExtension = "bin";
124 return true;
125
126 default:
127 FileExtension = string.Empty;
128 return false;
129 }
130 }
131
139 public bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
140 {
141 if (InternetContent.IsAccepted(BinaryContentTypes, AcceptedContentTypes) &&
142 (Object is byte[]))
143 {
144 Grade = Grade.Ok;
145 return true;
146 }
147 else if (Object is EncodedObject Obj && InternetContent.IsAccepted(new string[] { Obj.ContentType }, AcceptedContentTypes))
148 {
149 Grade = Grade.Ok;
150 return true;
151 }
152 else
153 {
154 Grade = Grade.NotAtAll;
155 return false;
156 }
157 }
158
167 public Task<ContentResponse> EncodeAsync(object Object, Encoding Encoding,
168 ICodecProgress Progress, params string[] AcceptedContentTypes)
169 {
170 if (Object is byte[] Bin)
171 return Task.FromResult(new ContentResponse(DefaultContentType, Bin, Bin));
172 else if (Object is EncodedObject Obj)
173 return Task.FromResult(new ContentResponse(Obj.ContentType, Obj.Data, Obj.Data));
174 else
175 return Task.FromResult(new ContentResponse(new ArgumentException("Unable to encode as binary.", nameof(Object))));
176 }
177 }
178}
Task< ContentResponse > DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair< string, string >[] Fields, Uri BaseUri, ICodecProgress Progress)
Decodes an object.
Definition: BinaryCodec.cs:85
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:97
Task< ContentResponse > EncodeAsync(object Object, Encoding Encoding, ICodecProgress Progress, params string[] AcceptedContentTypes)
Encodes an object.
Definition: BinaryCodec.cs:167
bool TryGetFileExtension(string ContentType, out string FileExtension)
Tries to get the file extension of an item, given its Content-Type.
Definition: BinaryCodec.cs:117
string[] FileExtensions
Supported file extensions.
Definition: BinaryCodec.cs:53
bool Decodes(string ContentType, out Grade Grade)
If the decoder decodes an object with a given content type.
Definition: BinaryCodec.cs:61
static readonly string[] BinaryContentTypes
Binary content types.
Definition: BinaryCodec.cs:29
string[] ContentTypes
Supported content types.
Definition: BinaryCodec.cs:48
bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
If the encoder encodes a given object.
Definition: BinaryCodec.cs:139
Represents an encoded object.
Definition: EncodedObject.cs:7
Contains information about a response to a content request.
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.
Interface for reporting progress about an encoding or decoding.
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