| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.IO; |
| | | 5 | | using System.Text; |
| | | 6 | | |
| | | 7 | | namespace CoreWCF.IdentityModel |
| | | 8 | | { |
| | | 9 | | internal abstract class CanonicalFormWriter |
| | | 10 | | { |
| | 0 | 11 | | internal static readonly Encoding Utf8WithoutPreamble = Encoding.UTF8; |
| | | 12 | | |
| | | 13 | | protected static void EncodeAndWrite(Stream stream, byte[] workBuffer, string s) |
| | | 14 | | { |
| | 0 | 15 | | if (s.Length > workBuffer.Length) |
| | | 16 | | { |
| | 0 | 17 | | EncodeAndWrite(stream, s); |
| | 0 | 18 | | return; |
| | | 19 | | } |
| | | 20 | | |
| | 0 | 21 | | for (int i = 0; i < s.Length; i++) |
| | | 22 | | { |
| | 0 | 23 | | char c = s[i]; |
| | 0 | 24 | | if (c < 127) |
| | | 25 | | { |
| | 0 | 26 | | workBuffer[i] = (byte)c; |
| | | 27 | | } |
| | | 28 | | else |
| | | 29 | | { |
| | 0 | 30 | | EncodeAndWrite(stream, s); |
| | 0 | 31 | | return; |
| | | 32 | | } |
| | | 33 | | } |
| | | 34 | | |
| | 0 | 35 | | stream.Write(workBuffer, 0, s.Length); |
| | 0 | 36 | | } |
| | | 37 | | |
| | | 38 | | protected static void EncodeAndWrite(Stream stream, byte[] workBuffer, char[] chars) |
| | | 39 | | { |
| | 0 | 40 | | EncodeAndWrite(stream, workBuffer, chars, chars.Length); |
| | 0 | 41 | | } |
| | | 42 | | |
| | | 43 | | protected static void EncodeAndWrite(Stream stream, byte[] workBuffer, char[] chars, int count) |
| | | 44 | | { |
| | 0 | 45 | | if (count > workBuffer.Length) |
| | | 46 | | { |
| | 0 | 47 | | EncodeAndWrite(stream, chars, count); |
| | 0 | 48 | | return; |
| | | 49 | | } |
| | | 50 | | |
| | 0 | 51 | | for (int i = 0; i < count; i++) |
| | | 52 | | { |
| | 0 | 53 | | char c = chars[i]; |
| | 0 | 54 | | if (c < 127) |
| | | 55 | | { |
| | 0 | 56 | | workBuffer[i] = (byte)c; |
| | | 57 | | } |
| | | 58 | | else |
| | | 59 | | { |
| | 0 | 60 | | EncodeAndWrite(stream, chars, count); |
| | 0 | 61 | | return; |
| | | 62 | | } |
| | | 63 | | } |
| | | 64 | | |
| | 0 | 65 | | stream.Write(workBuffer, 0, count); |
| | 0 | 66 | | } |
| | | 67 | | |
| | | 68 | | private static void EncodeAndWrite(Stream stream, string s) |
| | | 69 | | { |
| | 0 | 70 | | byte[] buffer = Utf8WithoutPreamble.GetBytes(s); |
| | 0 | 71 | | stream.Write(buffer, 0, buffer.Length); |
| | 0 | 72 | | } |
| | | 73 | | |
| | | 74 | | private static void EncodeAndWrite(Stream stream, char[] chars, int count) |
| | | 75 | | { |
| | 0 | 76 | | byte[] buffer = Utf8WithoutPreamble.GetBytes(chars, 0, count); |
| | 0 | 77 | | stream.Write(buffer, 0, buffer.Length); |
| | 0 | 78 | | } |
| | | 79 | | } |
| | | 80 | | } |