forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamExt.cs
More file actions
154 lines (128 loc) · 4.94 KB
/
StreamExt.cs
File metadata and controls
154 lines (128 loc) · 4.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright (c) Service Stack LLC. All Rights Reserved.
// License: https://raw.github.com/ServiceStack/ServiceStack/master/license.txt
using System;
using System.IO;
using System.Text;
using ServiceStack.Caching;
using ServiceStack.Text;
#if !(NETFX_CORE || SL5 || PCL || NETSTANDARD1_1)
using System.Security.Cryptography;
#endif
namespace ServiceStack
{
public static class StreamExt
{
#if !(SL5 || XBOX || ANDROID || __IOS__ || __MAC__ || PCL)
/// <summary>
/// Compresses the specified text using the default compression method: Deflate
/// </summary>
/// <param name="text">The text.</param>
/// <param name="compressionType">Type of the compression.</param>
/// <returns></returns>
public static byte[] Compress(this string text, string compressionType)
{
if (compressionType == CompressionTypes.Deflate)
return Deflate(text);
if (compressionType == CompressionTypes.GZip)
return GZip(text);
throw new NotSupportedException(compressionType);
}
/// <summary>
/// Compresses the specified text using the default compression method: Deflate
/// </summary>
public static byte[] CompressBytes(this byte[] bytes, string compressionType)
{
if (compressionType == CompressionTypes.Deflate)
return DeflateProvider.Deflate(bytes);
if (compressionType == CompressionTypes.GZip)
return GZipProvider.GZip(bytes);
throw new NotSupportedException(compressionType);
}
public static IDeflateProvider DeflateProvider = new Support.NetDeflateProvider();
public static IGZipProvider GZipProvider = new Support.NetGZipProvider();
/// <summary>
/// Decompresses the specified gz buffer using the default compression method: Inflate
/// </summary>
/// <param name="gzBuffer">The gz buffer.</param>
/// <param name="compressionType">Type of the compression.</param>
/// <returns></returns>
public static string Decompress(this byte[] gzBuffer, string compressionType)
{
if (compressionType == CompressionTypes.Deflate)
return Inflate(gzBuffer);
if (compressionType == CompressionTypes.GZip)
return GUnzip(gzBuffer);
throw new NotSupportedException(compressionType);
}
/// <summary>
/// Decompresses the specified gz buffer using the default compression method: Inflate
/// </summary>
public static byte[] DecompressBytes(this byte[] gzBuffer, string compressionType)
{
if (compressionType == CompressionTypes.Deflate)
return DeflateProvider.InflateBytes(gzBuffer);
if (compressionType == CompressionTypes.GZip)
return GZipProvider.GUnzipBytes(gzBuffer);
throw new NotSupportedException(compressionType);
}
public static byte[] Deflate(this string text)
{
return DeflateProvider.Deflate(text);
}
public static string Inflate(this byte[] gzBuffer)
{
return DeflateProvider.Inflate(gzBuffer);
}
public static byte[] GZip(this string text)
{
return GZipProvider.GZip(text);
}
public static string GUnzip(this byte[] gzBuffer)
{
return GZipProvider.GUnzip(gzBuffer);
}
#endif
public static string ToUtf8String(this Stream stream)
{
if (stream == null)
throw new ArgumentNullException(nameof(stream));
using (var reader = new StreamReader(stream, Encoding.UTF8))
{
return reader.ReadToEnd();
}
}
public static byte[] ToBytes(this Stream stream)
{
if (stream == null)
throw new ArgumentNullException(nameof(stream));
return stream.ReadFully();
}
public static void Write(this Stream stream, string text)
{
var bytes = Encoding.UTF8.GetBytes(text);
stream.Write(bytes, 0, bytes.Length);
}
#if !(NETFX_CORE || SL5 || PCL || NETSTANDARD1_1)
public static string ToMd5Hash(this Stream stream)
{
var hash = MD5.Create().ComputeHash(stream);
var sb = StringBuilderCache.Allocate();
foreach (byte b in hash)
{
sb.Append(b.ToString("x2"));
}
return StringBuilderCache.ReturnAndFree(sb);
}
public static string ToMd5Hash(this byte[] bytes)
{
var hash = MD5.Create().ComputeHash(bytes);
var sb = StringBuilderCache.Allocate();
foreach (byte b in hash)
{
sb.Append(b.ToString("x2"));
}
return StringBuilderCache.ReturnAndFree(sb);
}
#endif
}
}