IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
EncodingExtensions.cs
Go to the documentation of this file.
1using System.Buffers;
2using System.Text;
3
4namespace System.Text
5{
6
10 static class EncodingExtensions
11 {
12
13#if NETFRAMEWORK
14
26 const int MaxInputElementsPerIteration = 1 * 1024 * 1024;
27
37 public static unsafe long GetBytes(this Encoding encoding, ReadOnlySpan<char> chars, IBufferWriter<byte> writer)
38 {
39 if (encoding is null)
40 throw new ArgumentNullException(nameof(encoding));
41 if (writer is null)
42 throw new ArgumentNullException(nameof(writer));
43
44 if (chars.Length <= MaxInputElementsPerIteration)
45 {
46 // The input span is small enough where we can one-shot this.
47
48 int byteCount = encoding.GetByteCount(chars);
49 Span<byte> scratchBuffer = writer.GetSpan(byteCount);
50
51 int actualBytesWritten = encoding.GetBytes(chars, scratchBuffer);
52
53 writer.Advance(actualBytesWritten);
54 return actualBytesWritten;
55 }
56 else
57 {
58 // Allocate a stateful Encoder instance and chunk this.
59
60 encoding.GetEncoder().Convert(chars, writer, flush: true, out long totalBytesWritten, out _);
61 return totalBytesWritten;
62 }
63 }
64
65 public static unsafe int GetBytes(this Encoding encoding, string chars, Span<byte> bytes)
66 {
67 return encoding.GetBytes(chars.AsSpan(), bytes);
68 }
69
70 public static unsafe int GetChars(this Encoding encoding, ReadOnlySpan<byte> bytes, Span<char> chars)
71 {
72 fixed (byte* bytesPtr = bytes)
73 fixed (char* charsPtr = chars)
74 return encoding.GetChars(bytesPtr, bytes.Length, charsPtr, chars.Length);
75 }
76
77 public static unsafe int GetCharCount(this Encoding encoding, ReadOnlySpan<byte> bytes)
78 {
79 fixed (byte* bytesPtr = bytes)
80 {
81 return encoding.GetCharCount(bytesPtr, bytes.Length);
82 }
83 }
84
85 public static unsafe int GetByteCount(this Encoder encoder, ReadOnlySpan<char> chars, bool flush)
86 {
87 fixed (char* charsPtr = chars)
88 {
89 return encoder.GetByteCount(charsPtr, chars.Length, flush);
90 }
91 }
92
107 public static void Convert(this Encoder encoder, ReadOnlySpan<char> chars, IBufferWriter<byte> writer, bool flush, out long bytesUsed, out bool completed)
108 {
109 if (encoder is null)
110 throw new ArgumentNullException(nameof(encoder));
111 if (writer is null)
112 throw new ArgumentNullException(nameof(writer));
113
114 // We need to perform at least one iteration of the loop since the encoder could have internal state.
115
116 long totalBytesWritten = 0;
117
118 do
119 {
120 // If our remaining input is very large, instead truncate it and tell the encoder
121 // that there'll be more data after this call. This truncation is only for the
122 // purposes of getting the required byte count. Since the writer may give us a span
123 // larger than what we asked for, we'll pass the entirety of the remaining data
124 // to the transcoding routine, since it may be able to make progress beyond what
125 // was initially computed for the truncated input data.
126
127 int byteCountForThisSlice = chars.Length <= MaxInputElementsPerIteration
128 ? encoder.GetByteCount(chars, flush)
129 : encoder.GetByteCount(chars.Slice(0, MaxInputElementsPerIteration), flush: false /* this isn't the end of the data */);
130
131 Span<byte> scratchBuffer = writer.GetSpan(byteCountForThisSlice);
132
133 encoder.Convert(chars, scratchBuffer, flush, out int charsUsedJustNow, out int bytesWrittenJustNow, out completed);
134
135 chars = chars.Slice(charsUsedJustNow);
136 writer.Advance(bytesWrittenJustNow);
137 totalBytesWritten += bytesWrittenJustNow;
138 } while (!chars.IsEmpty);
139
140 bytesUsed = totalBytesWritten;
141 }
142
143 public static unsafe void Convert(this Encoder encoder, ReadOnlySpan<char> chars, Span<byte> bytes, bool flush, out int charsUsed, out int bytesUsed, out bool completed)
144 {
145 fixed (char* charsPtr = chars)
146 fixed (byte* bytesPtr = bytes)
147 {
148 encoder.Convert(charsPtr, chars.Length, bytesPtr, bytes.Length, flush, out charsUsed, out bytesUsed, out completed);
149 }
150 }
151
152#endif
153
154 }
155
156}
Provides extension methods for Encoding, Encoder and Decoder.