1
#if !SILVERLIGHT && !XBOX
5
using ServiceStack.CacheAccess;
6
using ServiceStack.Common.Support;
7
using ServiceStack.Common.Web;
8
using ServiceStack.Text;
10
namespace ServiceStack.Common
12
public static class StreamExtensions
16
/// Compresses the specified text using the default compression method: Deflate
18
/// <param name="text">The text.</param>
19
/// <param name="compressionType">Type of the compression.</param>
20
/// <returns></returns>
21
public static byte[] Compress(this string text, string compressionType)
23
if (compressionType == CompressionTypes.Deflate)
26
if (compressionType == CompressionTypes.GZip)
29
throw new NotSupportedException(compressionType);
32
public static IDeflateProvider DeflateProvider = new NetDeflateProvider();
34
public static IGZipProvider GZipProvider = new NetGZipProvider();
37
/// Decompresses the specified gz buffer using the default compression method: Inflate
39
/// <param name="gzBuffer">The gz buffer.</param>
40
/// <param name="compressionType">Type of the compression.</param>
41
/// <returns></returns>
42
public static string Decompress(this byte[] gzBuffer, string compressionType)
44
if (compressionType == CompressionTypes.Deflate)
45
return Inflate(gzBuffer);
47
if (compressionType == CompressionTypes.GZip)
48
return GUnzip(gzBuffer);
50
throw new NotSupportedException(compressionType);
53
public static byte[] Deflate(this string text)
55
return DeflateProvider.Deflate(text);
58
public static string Inflate(this byte[] gzBuffer)
60
return DeflateProvider.Inflate(gzBuffer);
63
public static byte[] GZip(this string text)
65
return GZipProvider.GZip(text);
68
public static string GUnzip(this byte[] gzBuffer)
70
return GZipProvider.GUnzip(gzBuffer);
74
public static string ToUtf8String(this Stream stream)
76
if (stream == null) throw new ArgumentNullException("stream");
78
using (var reader = new StreamReader(stream, Encoding.UTF8))
80
return reader.ReadToEnd();
84
public static byte[] ToBytes(this Stream stream)
86
if (stream == null) throw new ArgumentNullException("stream");
88
return stream.ReadFully();
91
public static void Write(this Stream stream, string text)
93
var bytes = Encoding.ASCII.GetBytes(text);
94
stream.Write(bytes, 0, bytes.Length);