Where possible, use the system Encoding.ASCII and Encoding.UTF8 rather than constructing fresh copies.

The encodings are thread-safe and already used in such a manner in other places.
This isn't done where Byte Order Mark output is suppressed, since Encoding.UTF8 is constructed to output the BOM.
0.7.4.1
Justin Clark-Casey (justincc) 2012-07-11 22:54:22 +01:00
parent 9c89ad9154
commit 916e3bf886
20 changed files with 52 additions and 81 deletions

View File

@ -50,7 +50,7 @@ namespace OpenSim.ConsoleClient
request.ContentType = "application/x-www-form-urlencoded";
byte[] buffer = new System.Text.ASCIIEncoding().GetBytes(data);
byte[] buffer = Encoding.ASCII.GetBytes(data);
int length = (int) buffer.Length;
request.ContentLength = length;

View File

@ -44,8 +44,6 @@ namespace OpenSim.Framework.Serialization.External
{
// private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
protected static UTF8Encoding m_utf8Encoding = new UTF8Encoding();
private static Dictionary<string, Action<LandData, XmlTextReader>> m_ldProcessors
= new Dictionary<string, Action<LandData, XmlTextReader>>();
@ -163,7 +161,7 @@ namespace OpenSim.Framework.Serialization.External
/// <exception cref="System.Xml.XmlException"></exception>
public static LandData Deserialize(byte[] serializedLandData)
{
return Deserialize(m_utf8Encoding.GetString(serializedLandData, 0, serializedLandData.Length));
return Deserialize(Encoding.UTF8.GetString(serializedLandData, 0, serializedLandData.Length));
}
/// <summary>

View File

@ -40,8 +40,6 @@ namespace OpenSim.Framework.Serialization.External
/// </summary>
public class RegionSettingsSerializer
{
protected static ASCIIEncoding m_asciiEncoding = new ASCIIEncoding();
/// <summary>
/// Deserialize settings
/// </summary>
@ -50,7 +48,7 @@ namespace OpenSim.Framework.Serialization.External
/// <exception cref="System.Xml.XmlException"></exception>
public static RegionSettings Deserialize(byte[] serializedSettings)
{
return Deserialize(m_asciiEncoding.GetString(serializedSettings, 0, serializedSettings.Length));
return Deserialize(Encoding.ASCII.GetString(serializedSettings, 0, serializedSettings.Length));
}
/// <summary>

View File

@ -53,8 +53,6 @@ namespace OpenSim.Framework.Serialization
TYPE_CONTIGUOUS_FILE = 8,
}
protected static ASCIIEncoding m_asciiEncoding = new ASCIIEncoding();
/// <summary>
/// Binary reader for the underlying stream
/// </summary>
@ -120,13 +118,13 @@ namespace OpenSim.Framework.Serialization
if (header[156] == (byte)'L')
{
int longNameLength = ConvertOctalBytesToDecimal(header, 124, 11);
tarHeader.FilePath = m_asciiEncoding.GetString(ReadData(longNameLength));
tarHeader.FilePath = Encoding.ASCII.GetString(ReadData(longNameLength));
//m_log.DebugFormat("[TAR ARCHIVE READER]: Got long file name {0}", tarHeader.FilePath);
header = m_br.ReadBytes(512);
}
else
{
tarHeader.FilePath = m_asciiEncoding.GetString(header, 0, 100);
tarHeader.FilePath = Encoding.ASCII.GetString(header, 0, 100);
tarHeader.FilePath = tarHeader.FilePath.Trim(m_nullCharArray);
//m_log.DebugFormat("[TAR ARCHIVE READER]: Got short file name {0}", tarHeader.FilePath);
}
@ -205,7 +203,7 @@ namespace OpenSim.Framework.Serialization
{
// Trim leading white space: ancient tars do that instead
// of leading 0s :-( don't ask. really.
string oString = m_asciiEncoding.GetString(bytes, startIndex, count).TrimStart(m_spaceCharArray);
string oString = Encoding.ASCII.GetString(bytes, startIndex, count).TrimStart(m_spaceCharArray);
int d = 0;

View File

@ -41,7 +41,6 @@ namespace OpenSim.Framework.Serialization
{
// private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
protected static ASCIIEncoding m_asciiEncoding = new ASCIIEncoding();
protected static UTF8Encoding m_utf8Encoding = new UTF8Encoding();
/// <summary>
@ -85,7 +84,7 @@ namespace OpenSim.Framework.Serialization
public void WriteFile(string filePath, byte[] data)
{
if (filePath.Length > 100)
WriteEntry("././@LongLink", m_asciiEncoding.GetBytes(filePath), 'L');
WriteEntry("././@LongLink", Encoding.ASCII.GetBytes(filePath), 'L');
char fileType;
@ -137,7 +136,7 @@ namespace OpenSim.Framework.Serialization
oString = "0" + oString;
}
byte[] oBytes = m_asciiEncoding.GetBytes(oString);
byte[] oBytes = Encoding.ASCII.GetBytes(oString);
return oBytes;
}
@ -156,20 +155,20 @@ namespace OpenSim.Framework.Serialization
byte[] header = new byte[512];
// file path field (100)
byte[] nameBytes = m_asciiEncoding.GetBytes(filePath);
byte[] nameBytes = Encoding.ASCII.GetBytes(filePath);
int nameSize = (nameBytes.Length >= 100) ? 100 : nameBytes.Length;
Array.Copy(nameBytes, header, nameSize);
// file mode (8)
byte[] modeBytes = m_asciiEncoding.GetBytes("0000777");
byte[] modeBytes = Encoding.ASCII.GetBytes("0000777");
Array.Copy(modeBytes, 0, header, 100, 7);
// owner user id (8)
byte[] ownerIdBytes = m_asciiEncoding.GetBytes("0000764");
byte[] ownerIdBytes = Encoding.ASCII.GetBytes("0000764");
Array.Copy(ownerIdBytes, 0, header, 108, 7);
// group user id (8)
byte[] groupIdBytes = m_asciiEncoding.GetBytes("0000764");
byte[] groupIdBytes = Encoding.ASCII.GetBytes("0000764");
Array.Copy(groupIdBytes, 0, header, 116, 7);
// file size in bytes (12)
@ -181,17 +180,17 @@ namespace OpenSim.Framework.Serialization
Array.Copy(fileSizeBytes, 0, header, 124, 11);
// last modification time (12)
byte[] lastModTimeBytes = m_asciiEncoding.GetBytes("11017037332");
byte[] lastModTimeBytes = Encoding.ASCII.GetBytes("11017037332");
Array.Copy(lastModTimeBytes, 0, header, 136, 11);
// entry type indicator (1)
header[156] = m_asciiEncoding.GetBytes(new char[] { fileType })[0];
header[156] = Encoding.ASCII.GetBytes(new char[] { fileType })[0];
Array.Copy(m_asciiEncoding.GetBytes("0000000"), 0, header, 329, 7);
Array.Copy(m_asciiEncoding.GetBytes("0000000"), 0, header, 337, 7);
Array.Copy(Encoding.ASCII.GetBytes("0000000"), 0, header, 329, 7);
Array.Copy(Encoding.ASCII.GetBytes("0000000"), 0, header, 337, 7);
// check sum for header block (8) [calculated last]
Array.Copy(m_asciiEncoding.GetBytes(" "), 0, header, 148, 8);
Array.Copy(Encoding.ASCII.GetBytes(" "), 0, header, 148, 8);
int checksum = 0;
foreach (byte b in header)

View File

@ -591,8 +591,8 @@ namespace OpenSim.Framework.Servers
{
string pidstring = System.Diagnostics.Process.GetCurrentProcess().Id.ToString();
FileStream fs = File.Create(path);
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
Byte[] buf = enc.GetBytes(pidstring);
Byte[] buf = Encoding.ASCII.GetBytes(pidstring);
fs.Write(buf, 0, buf.Length);
fs.Close();
m_pidFile = path;

View File

@ -1236,8 +1236,7 @@ namespace OpenSim.Framework
public static string Base64ToString(string str)
{
UTF8Encoding encoder = new UTF8Encoding();
Decoder utf8Decode = encoder.GetDecoder();
Decoder utf8Decode = Encoding.UTF8.GetDecoder();
byte[] todecode_byte = Convert.FromBase64String(str);
int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);

View File

@ -46,8 +46,6 @@ namespace OpenSim.Region.CoreModules.World.Archiver
{
private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
protected static ASCIIEncoding m_asciiEncoding = new ASCIIEncoding();
/// <summary>
/// Store for asset data we received before we get the metadata
/// </summary>

View File

@ -252,7 +252,7 @@ namespace OpenSim.Region.CoreModules.World.Terrain.FileLoaders
if (horizontalScale < 0.01d)
horizontalScale = 0.01d;
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
Encoding enc = Encoding.ASCII;
bs.Write(enc.GetBytes("TERRAGENTERRAIN "));

View File

@ -823,11 +823,10 @@ namespace OpenSim.Region.OptionalModules.Avatar.Voice.FreeSwitchVoice
m_log.DebugFormat("[FreeSwitchVoice]: Region:Parcel \"{0}\": parcel id {1}: using channel name {2}",
landName, land.LocalID, landUUID);
}
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
// slvoice handles the sip address differently if it begins with confctl, hiding it from the user in the friends list. however it also disables
// the personal speech indicators as well unless some siren14-3d codec magic happens. we dont have siren143d so we'll settle for the personal speech indicator.
channelUri = String.Format("sip:conf-{0}@{1}", "x" + Convert.ToBase64String(encoding.GetBytes(landUUID)), m_freeSwitchRealm);
channelUri = String.Format("sip:conf-{0}@{1}", "x" + Convert.ToBase64String(Encoding.ASCII.GetBytes(landUUID)), m_freeSwitchRealm);
lock (m_ParcelAddress)
{

View File

@ -1120,7 +1120,6 @@ namespace Nwc.XmlRpc
/// <summary>Class supporting the request side of an XML-RPC transaction.</summary>
public class ConfigurableKeepAliveXmlRpcRequest : XmlRpcRequest
{
private Encoding _encoding = new ASCIIEncoding();
private XmlRpcRequestSerializer _serializer = new XmlRpcRequestSerializer();
private XmlRpcResponseDeserializer _deserializer = new XmlRpcResponseDeserializer();
private bool _disableKeepAlive = true;
@ -1153,7 +1152,7 @@ namespace Nwc.XmlRpc
request.KeepAlive = !_disableKeepAlive;
Stream stream = request.GetRequestStream();
XmlTextWriter xml = new XmlTextWriter(stream, _encoding);
XmlTextWriter xml = new XmlTextWriter(stream, Encoding.ASCII);
_serializer.Serialize(xml, this);
xml.Flush();
xml.Close();

View File

@ -425,8 +425,7 @@ namespace OpenSim.Region.OptionalModules.Scripting.JsonStore
try
{
System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
string jsondata = SLUtil.ParseNotecardToString(enc.GetString(a.Data));
string jsondata = SLUtil.ParseNotecardToString(Encoding.UTF8.GetString(a.Data));
int result = m_store.SetValue(storeID, path, jsondata,true) ? 1 : 0;
m_comms.DispatchReply(scriptID,result, "", reqID.ToString());
return;

View File

@ -482,10 +482,7 @@ namespace OpenSim.Region.OptionalModules.Scripting.Minimodule
// Convert to base64
//
string filetext = Convert.ToBase64String(data);
ASCIIEncoding enc = new ASCIIEncoding();
Byte[] buf = enc.GetBytes(filetext);
Byte[] buf = Encoding.ASCII.GetBytes(filetext);
m_log.Info("MRM 9");

View File

@ -10536,9 +10536,7 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api
return;
}
System.Text.UTF8Encoding enc =
new System.Text.UTF8Encoding();
string data = enc.GetString(a.Data);
string data = Encoding.UTF8.GetString(a.Data);
//m_log.Debug(data);
NotecardCache.Cache(id, data);
AsyncCommands.
@ -10591,9 +10589,7 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api
return;
}
System.Text.UTF8Encoding enc =
new System.Text.UTF8Encoding();
string data = enc.GetString(a.Data);
string data = Encoding.UTF8.GetString(a.Data);
//m_log.Debug(data);
NotecardCache.Cache(id, data);
AsyncCommands.DataserverPlugin.DataserverReply(id.ToString(),

View File

@ -1811,8 +1811,7 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api
if (a == null)
return UUID.Zero;
System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
string data = enc.GetString(a.Data);
string data = Encoding.UTF8.GetString(a.Data);
NotecardCache.Cache(assetID, data);
};

View File

@ -31,6 +31,7 @@ using System.Collections.Generic;
using System.Globalization;
using System.Reflection;
using System.IO;
using System.Text;
using Microsoft.CSharp;
//using Microsoft.JScript;
using Microsoft.VisualBasic;
@ -711,9 +712,7 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
//
string filetext = System.Convert.ToBase64String(data);
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
Byte[] buf = enc.GetBytes(filetext);
Byte[] buf = Encoding.ASCII.GetBytes(filetext);
FileStream sfs = File.Create(assembly + ".text");
sfs.Write(buf, 0, buf.Length);
@ -804,8 +803,7 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
mapstring += String.Format("{0},{1},{2},{3}\n", k.Key, k.Value, v.Key, v.Value);
}
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
Byte[] mapbytes = enc.GetBytes(mapstring);
Byte[] mapbytes = Encoding.ASCII.GetBytes(mapstring);
FileStream mfs = File.Create(filename);
mfs.Write(mapbytes, 0, mapbytes.Length);
mfs.Close();

View File

@ -26,15 +26,16 @@
*/
using System;
using System.IO;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Lifetime;
using System.Threading;
using System.Collections;
using System.Collections.Generic;
using System.Security.Policy;
using System.Reflection;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Lifetime;
using System.Security.Policy;
using System.Text;
using System.Threading;
using System.Xml;
using OpenMetaverse;
using log4net;
@ -298,13 +299,10 @@ namespace OpenSim.Region.ScriptEngine.Shared.Instance
using (FileStream fs = File.Open(savedState,
FileMode.Open, FileAccess.Read, FileShare.None))
{
System.Text.UTF8Encoding enc =
new System.Text.UTF8Encoding();
Byte[] data = new Byte[size];
fs.Read(data, 0, size);
xml = enc.GetString(data);
xml = Encoding.UTF8.GetString(data);
ScriptSerializer.Deserialize(xml, this);
@ -954,8 +952,7 @@ namespace OpenSim.Region.ScriptEngine.Shared.Instance
try
{
FileStream fs = File.Create(Path.Combine(Path.GetDirectoryName(assembly), ItemID.ToString() + ".state"));
System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
Byte[] buf = enc.GetBytes(xml);
Byte[] buf = (new UTF8Encoding()).GetBytes(xml);
fs.Write(buf, 0, buf.Length);
fs.Close();
}

View File

@ -1763,7 +1763,7 @@ namespace OpenSim.Region.ScriptEngine.XEngine
tfs.Read(tdata, 0, tdata.Length);
}
assem = new System.Text.ASCIIEncoding().GetString(tdata);
assem = Encoding.ASCII.GetString(tdata);
}
catch (Exception e)
{

View File

@ -27,9 +27,10 @@
using System;
using System.IO;
using System.Xml;
using System.Threading;
using System.Reflection;
using System.Threading;
using System.Text;
using System.Xml;
using OpenSim.Framework;
using OpenSim.Framework.Console;
using log4net;
@ -335,8 +336,7 @@ namespace OpenSim.Server.Base
{
string pidstring = System.Diagnostics.Process.GetCurrentProcess().Id.ToString();
FileStream fs = File.Create(path);
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
Byte[] buf = enc.GetBytes(pidstring);
Byte[] buf = Encoding.ASCII.GetBytes(pidstring);
fs.Write(buf, 0, buf.Length);
fs.Close();
m_pidFile = path;

View File

@ -28,6 +28,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Microsoft.CSharp;
using OpenSim.Region.ScriptEngine.Shared.CodeTools;
using System.CodeDom.Compiler;
@ -201,11 +202,7 @@ namespace OpenSim.Tools.LSL.Compiler
// Convert to base64
//
string filetext = System.Convert.ToBase64String(data);
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
Byte[] buf = enc.GetBytes(filetext);
Byte[] buf = Encoding.ASCII.GetBytes(filetext);
FileStream sfs = File.Create(OutFile + ".text");
sfs.Write(buf, 0, buf.Length);
sfs.Close();
@ -222,7 +219,7 @@ namespace OpenSim.Tools.LSL.Compiler
// }
// }
buf = enc.GetBytes(posmap);
buf = Encoding.ASCII.GetBytes(posmap);
FileStream mfs = File.Create(OutFile + ".map");
mfs.Write(buf, 0, buf.Length);