Skip to content

Commit

Permalink
SRCH-119: Ensuring MemoryStreams are disposed.
Browse files Browse the repository at this point in the history
  • Loading branch information
James Atherton authored and mausch committed Sep 11, 2011
1 parent fefda8e commit a774ca0
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 120 deletions.
40 changes: 20 additions & 20 deletions SolrNet.Tests/Utils/CompressionUtils.cs
Expand Up @@ -8,30 +8,30 @@ public class CompressionUtils
{
public static Stream GzipCompressStream(string textToCompress)
{
var data = Encoding.GetEncoding("iso-8859-1").GetBytes(textToCompress);

var ms = new MemoryStream();

using (var zip = new GZipStream(ms, CompressionMode.Compress))
{
zip.Write(data, 0, data.Length);
}

return new MemoryStream(ms.ToArray());
var data = Encoding.GetEncoding("iso-8859-1").GetBytes(textToCompress);

using (var ms = new MemoryStream()) {

using (var zip = new GZipStream(ms, CompressionMode.Compress)) {
zip.Write(data, 0, data.Length);
}

return new MemoryStream(ms.ToArray());
}
}

public static Stream DeflateCompressStream(string textToCompress)
{
var data = Encoding.GetEncoding("iso-8859-1").GetBytes(textToCompress);

var ms = new MemoryStream();

using (var zip = new DeflateStream(ms, CompressionMode.Compress))
{
zip.Write(data, 0, data.Length);
}

return new MemoryStream(ms.ToArray());
var data = Encoding.GetEncoding("iso-8859-1").GetBytes(textToCompress);

using (var ms = new MemoryStream()) {

using (var zip = new DeflateStream(ms, CompressionMode.Compress)) {
zip.Write(data, 0, data.Length);
}

return new MemoryStream(ms.ToArray());
}
}
}
}
34 changes: 18 additions & 16 deletions SolrNet/Utils/HttpEncoder.cs
Expand Up @@ -207,14 +207,15 @@ internal static
#endif
string UrlPathEncode(string value) {
if (String.IsNullOrEmpty(value))
return value;

MemoryStream result = new MemoryStream();
int length = value.Length;
for (int i = 0; i < length; i++)
UrlPathEncodeChar(value[i], result);

return Encoding.ASCII.GetString(result.ToArray());
return value;

using (MemoryStream result = new MemoryStream()) {
int length = value.Length;
for (int i = 0; i < length; i++)
UrlPathEncodeChar(value[i], result);

return Encoding.ASCII.GetString(result.ToArray());
}
}

internal static byte[] UrlEncodeToBytes(byte[] bytes, int offset, int count) {
Expand All @@ -229,14 +230,15 @@ internal static
throw new ArgumentOutOfRangeException("offset");

if (count < 0 || count > blen - offset)
throw new ArgumentOutOfRangeException("count");

MemoryStream result = new MemoryStream(count);
int end = offset + count;
for (int i = offset; i < end; i++)
UrlEncodeChar((char)bytes[i], result, false);

return result.ToArray();
throw new ArgumentOutOfRangeException("count");

using (MemoryStream result = new MemoryStream(count)) {
int end = offset + count;
for (int i = offset; i < end; i++)
UrlEncodeChar((char) bytes[i], result, false);

return result.ToArray();
}
}

internal static string HtmlEncode(string s) {
Expand Down
172 changes: 88 additions & 84 deletions SolrNet/Utils/HttpUtility.cs
Expand Up @@ -214,64 +214,66 @@ sealed class HttpQSCollection : NameValueCollection {
return val;
}

public static string UrlDecode(byte[] bytes, int offset, int count, Encoding e) {
if (bytes == null)
return null;
if (count == 0)
return String.Empty;

if (bytes == null)
throw new ArgumentNullException("bytes");

if (offset < 0 || offset > bytes.Length)
throw new ArgumentOutOfRangeException("offset");

if (count < 0 || offset + count > bytes.Length)
throw new ArgumentOutOfRangeException("count");

StringBuilder output = new StringBuilder();
MemoryStream acc = new MemoryStream();

int end = count + offset;
int xchar;
for (int i = offset; i < end; i++) {
if (bytes[i] == '%' && i + 2 < count && bytes[i + 1] != '%') {
if (bytes[i + 1] == (byte)'u' && i + 5 < end) {
if (acc.Length > 0) {
output.Append(GetChars(acc, e));
acc.SetLength(0);
}
xchar = GetChar(bytes, i + 2, 4);
if (xchar != -1) {
output.Append((char)xchar);
i += 5;
continue;
}
} else if ((xchar = GetChar(bytes, i + 1, 2)) != -1) {
acc.WriteByte((byte)xchar);
i += 2;
continue;
}
}

if (acc.Length > 0) {
output.Append(GetChars(acc, e));
acc.SetLength(0);
}

if (bytes[i] == '+') {
output.Append(' ');
} else {
output.Append((char)bytes[i]);
}
}

if (acc.Length > 0) {
output.Append(GetChars(acc, e));
}

acc = null;
return output.ToString();
public static string UrlDecode(byte[] bytes, int offset, int count, Encoding e) {
if (bytes == null)
return null;
if (count == 0)
return String.Empty;

if (bytes == null)
throw new ArgumentNullException("bytes");

if (offset < 0 || offset > bytes.Length)
throw new ArgumentOutOfRangeException("offset");

if (count < 0 || offset + count > bytes.Length)
throw new ArgumentOutOfRangeException("count");

StringBuilder output = new StringBuilder();
using(MemoryStream acc = new MemoryStream()){

int end = count + offset;
int xchar;
for (int i = offset; i < end; i++) {
if (bytes[i] == '%' && i + 2 < count && bytes[i + 1] != '%') {
if (bytes[i + 1] == (byte) 'u' && i + 5 < end) {
if (acc.Length > 0) {
output.Append(GetChars(acc, e));
acc.SetLength(0);
}
xchar = GetChar(bytes, i + 2, 4);
if (xchar != -1) {
output.Append((char) xchar);
i += 5;
continue;
}
} else if ((xchar = GetChar(bytes, i + 1, 2)) != -1) {
acc.WriteByte((byte) xchar);
i += 2;
continue;
}
}

if (acc.Length > 0) {
output.Append(GetChars(acc, e));
acc.SetLength(0);
}

if (bytes[i] == '+') {
output.Append(' ');
} else {
output.Append((char) bytes[i]);
}
}

if (acc.Length > 0) {
output.Append(GetChars(acc, e));
}


}

return output.ToString();
}

public static byte[] UrlDecodeToBytes(byte[] bytes) {
Expand Down Expand Up @@ -306,25 +308,26 @@ sealed class HttpQSCollection : NameValueCollection {
throw new ArgumentOutOfRangeException("offset");

if (count < 0 || offset > len - count)
throw new ArgumentOutOfRangeException("count");

MemoryStream result = new MemoryStream();
int end = offset + count;
for (int i = offset; i < end; i++) {
char c = (char)bytes[i];
if (c == '+') {
c = ' ';
} else if (c == '%' && i < end - 2) {
int xchar = GetChar(bytes, i + 1, 2);
if (xchar != -1) {
c = (char)xchar;
i += 2;
}
}
result.WriteByte((byte)c);
}

return result.ToArray();
throw new ArgumentOutOfRangeException("count");

using (MemoryStream result = new MemoryStream()) {
int end = offset + count;
for (int i = offset; i < end; i++) {
char c = (char) bytes[i];
if (c == '+') {
c = ' ';
} else if (c == '%' && i < end - 2) {
int xchar = GetChar(bytes, i + 1, 2);
if (xchar != -1) {
c = (char) xchar;
i += 2;
}
}
result.WriteByte((byte) c);
}

return result.ToArray();
}
}

public static string UrlEncode(string str) {
Expand Down Expand Up @@ -427,13 +430,14 @@ sealed class HttpQSCollection : NameValueCollection {
return null;

if (str.Length == 0)
return new byte[0];

MemoryStream result = new MemoryStream(str.Length);
foreach (char c in str) {
HttpEncoder.UrlEncodeChar(c, result, true);
}
return result.ToArray();
return new byte[0];

using (MemoryStream result = new MemoryStream(str.Length)) {
foreach (char c in str) {
HttpEncoder.UrlEncodeChar(c, result, true);
}
return result.ToArray();
}
}

/// <summary>
Expand Down

0 comments on commit a774ca0

Please sign in to comment.