diff options
author | Oren Hurvitz | 2014-06-01 17:39:11 +0300 |
---|---|---|
committer | Oren Hurvitz | 2014-07-21 08:30:03 +0100 |
commit | 99ac770abbe3a95887c4b10c82f3985aa878eeef (patch) | |
tree | 8c946dab083dd50a352f3861415eca43185d8d95 /OpenSim/Framework/Util.cs | |
parent | Set "[Terrain]SendTerrainUpdatesByViewDistance=true" by default. (diff) | |
download | opensim-SC_OLD-99ac770abbe3a95887c4b10c82f3985aa878eeef.zip opensim-SC_OLD-99ac770abbe3a95887c4b10c82f3985aa878eeef.tar.gz opensim-SC_OLD-99ac770abbe3a95887c4b10c82f3985aa878eeef.tar.bz2 opensim-SC_OLD-99ac770abbe3a95887c4b10c82f3985aa878eeef.tar.xz |
Close streams immediately when we finish using them
Diffstat (limited to 'OpenSim/Framework/Util.cs')
-rw-r--r-- | OpenSim/Framework/Util.cs | 53 |
1 files changed, 13 insertions, 40 deletions
diff --git a/OpenSim/Framework/Util.cs b/OpenSim/Framework/Util.cs index 9d7de97..5c429ee 100644 --- a/OpenSim/Framework/Util.cs +++ b/OpenSim/Framework/Util.cs | |||
@@ -509,6 +509,19 @@ namespace OpenSim.Framework | |||
509 | return sb.ToString(); | 509 | return sb.ToString(); |
510 | } | 510 | } |
511 | 511 | ||
512 | public static byte[] DocToBytes(XmlDocument doc) | ||
513 | { | ||
514 | using (MemoryStream ms = new MemoryStream()) | ||
515 | using (XmlTextWriter xw = new XmlTextWriter(ms, null)) | ||
516 | { | ||
517 | xw.Formatting = Formatting.Indented; | ||
518 | doc.WriteTo(xw); | ||
519 | xw.Flush(); | ||
520 | |||
521 | return ms.ToArray(); | ||
522 | } | ||
523 | } | ||
524 | |||
512 | /// <summary> | 525 | /// <summary> |
513 | /// Is the platform Windows? | 526 | /// Is the platform Windows? |
514 | /// </summary> | 527 | /// </summary> |
@@ -1307,46 +1320,6 @@ namespace OpenSim.Framework | |||
1307 | return ret; | 1320 | return ret; |
1308 | } | 1321 | } |
1309 | 1322 | ||
1310 | public static string Compress(string text) | ||
1311 | { | ||
1312 | byte[] buffer = Util.UTF8.GetBytes(text); | ||
1313 | MemoryStream memory = new MemoryStream(); | ||
1314 | using (GZipStream compressor = new GZipStream(memory, CompressionMode.Compress, true)) | ||
1315 | { | ||
1316 | compressor.Write(buffer, 0, buffer.Length); | ||
1317 | } | ||
1318 | |||
1319 | memory.Position = 0; | ||
1320 | |||
1321 | byte[] compressed = new byte[memory.Length]; | ||
1322 | memory.Read(compressed, 0, compressed.Length); | ||
1323 | |||
1324 | byte[] compressedBuffer = new byte[compressed.Length + 4]; | ||
1325 | Buffer.BlockCopy(compressed, 0, compressedBuffer, 4, compressed.Length); | ||
1326 | Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, compressedBuffer, 0, 4); | ||
1327 | return Convert.ToBase64String(compressedBuffer); | ||
1328 | } | ||
1329 | |||
1330 | public static string Decompress(string compressedText) | ||
1331 | { | ||
1332 | byte[] compressedBuffer = Convert.FromBase64String(compressedText); | ||
1333 | using (MemoryStream memory = new MemoryStream()) | ||
1334 | { | ||
1335 | int msgLength = BitConverter.ToInt32(compressedBuffer, 0); | ||
1336 | memory.Write(compressedBuffer, 4, compressedBuffer.Length - 4); | ||
1337 | |||
1338 | byte[] buffer = new byte[msgLength]; | ||
1339 | |||
1340 | memory.Position = 0; | ||
1341 | using (GZipStream decompressor = new GZipStream(memory, CompressionMode.Decompress)) | ||
1342 | { | ||
1343 | decompressor.Read(buffer, 0, buffer.Length); | ||
1344 | } | ||
1345 | |||
1346 | return Util.UTF8.GetString(buffer); | ||
1347 | } | ||
1348 | } | ||
1349 | |||
1350 | /// <summary> | 1323 | /// <summary> |
1351 | /// Copy data from one stream to another, leaving the read position of both streams at the beginning. | 1324 | /// Copy data from one stream to another, leaving the read position of both streams at the beginning. |
1352 | /// </summary> | 1325 | /// </summary> |