1- #region License
2- // Copyright 2010 John Sheehan
3- //
4- // Licensed under the Apache License, Version 2.0 (the "License");
5- // you may not use this file except in compliance with the License.
6- // You may obtain a copy of the License at
7- //
8- // http://www.apache.org/licenses/LICENSE-2.0
9- //
10- // Unless required by applicable law or agreed to in writing, software
11- // distributed under the License is distributed on an "AS IS" BASIS,
12- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13- // See the License for the specific language governing permissions and
14- // limitations under the License.
15- #endregion
16-
17- using System . Globalization ;
18- using System . IO ;
19- using System . Text ;
20-
21- namespace RestSharp . Extensions
22- {
23- /// <summary>
24- /// Extension method overload!
25- /// </summary>
26- public static class MiscExtensions
27- {
28- #if ! WINDOWS_PHONE
29- /// <summary>
30- /// Save a byte array to a file
31- /// </summary>
32- /// <param name="input">Bytes to save</param>
33- /// <param name="path">Full path to save file to</param>
34- public static void SaveAs ( this byte [ ] input , string path )
35- {
36- File . WriteAllBytes ( path , input ) ;
37- }
38- #endif
39-
40- /// <summary>
41- /// Read a stream into a byte array
42- /// </summary>
43- /// <param name="input">Stream to read</param>
44- /// <returns>byte[]</returns>
45- public static byte [ ] ReadAsBytes ( this Stream input )
46- {
47- byte [ ] buffer = new byte [ 16 * 1024 ] ;
48- using ( MemoryStream ms = new MemoryStream ( ) )
49- {
50- int read ;
51- while ( ( read = input . Read ( buffer , 0 , buffer . Length ) ) > 0 )
52- {
53- ms . Write ( buffer , 0 , read ) ;
54- }
55- return ms . ToArray ( ) ;
56- }
57- }
58-
59- /// <summary>
60- /// Copies bytes from one stream to another
61- /// </summary>
62- /// <param name="input">The input stream.</param>
63- /// <param name="output">The output stream.</param>
64- public static void CopyTo ( this Stream input , Stream output )
65- {
66- var buffer = new byte [ 32768 ] ;
67- while ( true )
68- {
69- var read = input . Read ( buffer , 0 , buffer . Length ) ;
70- if ( read <= 0 )
71- return ;
72- output . Write ( buffer , 0 , read ) ;
73- }
74- }
75-
76- /// <summary>
77- /// Converts a byte array to a string, using its byte order mark to convert it to the right encoding.
78- /// http://www.shrinkrays.net/code-snippets/csharp/an-extension-method-for-converting-a-byte-array-to-a-string.aspx
79- /// </summary>
80- /// <param name="buffer">An array of bytes to convert</param>
81- /// <returns>The byte as a string.</returns>
82- public static string AsString ( this byte [ ] buffer )
1+ #region License
2+ // Copyright 2010 John Sheehan
3+ //
4+ // Licensed under the Apache License, Version 2.0 (the "License");
5+ // you may not use this file except in compliance with the License.
6+ // You may obtain a copy of the License at
7+ //
8+ // http://www.apache.org/licenses/LICENSE-2.0
9+ //
10+ // Unless required by applicable law or agreed to in writing, software
11+ // distributed under the License is distributed on an "AS IS" BASIS,
12+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ // See the License for the specific language governing permissions and
14+ // limitations under the License.
15+ #endregion
16+
17+ using System . Globalization ;
18+ using System . IO ;
19+ using System . Text ;
20+
21+ namespace RestSharp . Extensions
22+ {
23+ /// <summary>
24+ /// Extension method overload!
25+ /// </summary>
26+ public static class MiscExtensions
27+ {
28+ #if ! WINDOWS_PHONE
29+ /// <summary>
30+ /// Save a byte array to a file
31+ /// </summary>
32+ /// <param name="input">Bytes to save</param>
33+ /// <param name="path">Full path to save file to</param>
34+ public static void SaveAs ( this byte [ ] input , string path )
8335 {
84- if ( buffer == null ) return "" ;
85-
86- // Ansi as default
87- Encoding encoding = Encoding . UTF8 ;
88-
89- #if FRAMEWORK
90- return encoding . GetString ( buffer ) ;
91- #else
92- if ( buffer == null || buffer . Length == 0 )
93- return "" ;
94-
95- /*
96- EF BB BF UTF-8
97- FF FE UTF-16 little endian
98- FE FF UTF-16 big endian
99- FF FE 00 00 UTF-32, little endian
100- 00 00 FE FF UTF-32, big-endian
101- */
102-
103- if ( buffer [ 0 ] == 0xef && buffer [ 1 ] == 0xbb && buffer [ 2 ] == 0xbf )
104- {
105- encoding = Encoding . UTF8 ;
106- }
107- else if ( buffer [ 0 ] == 0xfe && buffer [ 1 ] == 0xff )
108- {
109- encoding = Encoding . Unicode ;
110- }
111- else if ( buffer [ 0 ] == 0xfe && buffer [ 1 ] == 0xff )
112- {
113- encoding = Encoding . BigEndianUnicode ; // utf-16be
114- }
115-
116- using ( MemoryStream stream = new MemoryStream ( ) )
117- {
118- stream . Write ( buffer , 0 , buffer . Length ) ;
119- stream . Seek ( 0 , SeekOrigin . Begin ) ;
120- using ( StreamReader reader = new StreamReader ( stream , encoding ) )
121- {
122- return reader . ReadToEnd ( ) ;
123- }
124- }
125- #endif
126- }
127- }
36+ File . WriteAllBytes ( path , input ) ;
37+ }
38+ #endif
39+
40+ /// <summary>
41+ /// Read a stream into a byte array
42+ /// </summary>
43+ /// <param name="input">Stream to read</param>
44+ /// <returns>byte[]</returns>
45+ public static byte [ ] ReadAsBytes ( this Stream input )
46+ {
47+ byte [ ] buffer = new byte [ 16 * 1024 ] ;
48+ using ( MemoryStream ms = new MemoryStream ( ) )
49+ {
50+ int read ;
51+ while ( ( read = input . Read ( buffer , 0 , buffer . Length ) ) > 0 )
52+ {
53+ ms . Write ( buffer , 0 , read ) ;
54+ }
55+ return ms . ToArray ( ) ;
56+ }
57+ }
58+
59+ /// <summary>
60+ /// Copies bytes from one stream to another
61+ /// </summary>
62+ /// <param name="input">The input stream.</param>
63+ /// <param name="output">The output stream.</param>
64+ public static void CopyTo ( this Stream input , Stream output )
65+ {
66+ var buffer = new byte [ 32768 ] ;
67+ while ( true )
68+ {
69+ var read = input . Read ( buffer , 0 , buffer . Length ) ;
70+ if ( read <= 0 )
71+ return ;
72+ output . Write ( buffer , 0 , read ) ;
73+ }
74+ }
75+
76+ /// <summary>
77+ /// Converts a byte array to a string, using its byte order mark to convert it to the right encoding.
78+ /// http://www.shrinkrays.net/code-snippets/csharp/an-extension-method-for-converting-a-byte-array-to-a-string.aspx
79+ /// </summary>
80+ /// <param name="buffer">An array of bytes to convert</param>
81+ /// <returns>The byte as a string.</returns>
82+ public static string AsString ( this byte [ ] buffer )
83+ {
84+ if ( buffer == null ) return "" ;
85+
86+ // Ansi as default
87+ Encoding encoding = Encoding . UTF8 ;
88+
89+ #if FRAMEWORK
90+ return encoding . GetString ( buffer ) ;
91+ #else
92+ if ( buffer == null || buffer . Length == 0 )
93+ return "" ;
94+
95+ /*
96+ EF BB BF UTF-8
97+ FF FE UTF-16 little endian
98+ FE FF UTF-16 big endian
99+ FF FE 00 00 UTF-32, little endian
100+ 00 00 FE FF UTF-32, big-endian
101+ */
102+
103+ if ( buffer [ 0 ] == 0xef && buffer [ 1 ] == 0xbb && buffer [ 2 ] == 0xbf )
104+ {
105+ encoding = Encoding . UTF8 ;
106+ }
107+ else if ( buffer [ 0 ] == 0xfe && buffer [ 1 ] == 0xff )
108+ {
109+ encoding = Encoding . Unicode ;
110+ }
111+ else if ( buffer [ 0 ] == 0xfe && buffer [ 1 ] == 0xff )
112+ {
113+ encoding = Encoding . BigEndianUnicode ; // utf-16be
114+ }
115+
116+ using ( MemoryStream stream = new MemoryStream ( ) )
117+ {
118+ stream . Write ( buffer , 0 , buffer . Length ) ;
119+ stream . Seek ( 0 , SeekOrigin . Begin ) ;
120+ using ( StreamReader reader = new StreamReader ( stream , encoding ) )
121+ {
122+ return reader . ReadToEnd ( ) ;
123+ }
124+ }
125+ #endif
126+ }
127+ }
128128}
0 commit comments