Rather than instantiating a UTF8 encoding everywhere when we want to supress the BOM, use a single Util.UTF8NoBomEncoding.

This class is thread-safe (as evidenced by the provision of the system-wide Encoding.UTF8 which does not suppress BOM on output).
0.7.4.1
Justin Clark-Casey (justincc) 2012-07-13 01:03:28 +01:00
parent 9ccb578721
commit 884d603cac
15 changed files with 68 additions and 85 deletions

View File

@ -66,9 +66,7 @@ namespace OpenSim.Framework.Capabilities
TResponse response = m_method(llsdRequest);
Encoding encoding = new UTF8Encoding(false);
return encoding.GetBytes(LLSDHelpers.SerialiseLLSDReply(response));
return Util.UTF8NoBomEncoding.GetBytes(LLSDHelpers.SerialiseLLSDReply(response));
}
}
}

View File

@ -41,8 +41,6 @@ namespace OpenSim.Framework.Serialization
{
// private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
protected static UTF8Encoding m_utf8Encoding = new UTF8Encoding();
/// <summary>
/// Binary writer for the underlying stream
/// </summary>
@ -73,7 +71,7 @@ namespace OpenSim.Framework.Serialization
/// <param name="data"></param>
public void WriteFile(string filePath, string data)
{
WriteFile(filePath, m_utf8Encoding.GetBytes(data));
WriteFile(filePath, Util.UTF8NoBomEncoding.GetBytes(data));
}
/// <summary>

View File

@ -148,6 +148,7 @@ namespace OpenSim.Framework
}
public static Encoding UTF8 = Encoding.UTF8;
public static Encoding UTF8NoBomEncoding = new UTF8Encoding(false);
/// <value>
/// Well known UUID for the blank texture used in the Linden SL viewer version 1.20 (and hopefully onwards)

View File

@ -952,7 +952,7 @@ namespace OpenSim.Region.ScriptEngine.Shared.Instance
try
{
FileStream fs = File.Create(Path.Combine(Path.GetDirectoryName(assembly), ItemID.ToString() + ".state"));
Byte[] buf = (new UTF8Encoding()).GetBytes(xml);
Byte[] buf = Util.UTF8NoBomEncoding.GetBytes(xml);
fs.Write(buf, 0, buf.Length);
fs.Close();
}

View File

@ -321,8 +321,7 @@ namespace OpenSim.Server.Handlers.Authentication
private byte[] ResultToBytes(Dictionary<string, object> result)
{
string xmlString = ServerUtils.BuildXmlResponse(result);
UTF8Encoding encoding = new UTF8Encoding();
return encoding.GetBytes(xmlString);
return Util.UTF8NoBomEncoding.GetBytes(xmlString);
}
}
}

View File

@ -121,8 +121,7 @@ namespace OpenSim.Server.Handlers.Avatar
string xmlString = ServerUtils.BuildXmlResponse(result);
UTF8Encoding encoding = new UTF8Encoding();
return encoding.GetBytes(xmlString);
return Util.UTF8NoBomEncoding.GetBytes(xmlString);
}
return FailureResult();

View File

@ -152,10 +152,9 @@ namespace OpenSim.Server.Handlers.Friends
}
string xmlString = ServerUtils.BuildXmlResponse(result);
//m_log.DebugFormat("[FRIENDS HANDLER]: resp string: {0}", xmlString);
UTF8Encoding encoding = new UTF8Encoding();
return encoding.GetBytes(xmlString);
//m_log.DebugFormat("[FRIENDS HANDLER]: resp string: {0}", xmlString);
return Util.UTF8NoBomEncoding.GetBytes(xmlString);
}
byte[] StoreFriend(Dictionary<string, object> request)

View File

@ -226,10 +226,9 @@ namespace OpenSim.Server.Handlers.Grid
}
string xmlString = ServerUtils.BuildXmlResponse(result);
//m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
UTF8Encoding encoding = new UTF8Encoding();
return encoding.GetBytes(xmlString);
//m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
return Util.UTF8NoBomEncoding.GetBytes(xmlString);
}
byte[] GetRegionByUUID(Dictionary<string, object> request)
@ -256,9 +255,9 @@ namespace OpenSim.Server.Handlers.Grid
result["result"] = rinfo.ToKeyValuePairs();
string xmlString = ServerUtils.BuildXmlResponse(result);
//m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
UTF8Encoding encoding = new UTF8Encoding();
return encoding.GetBytes(xmlString);
return Util.UTF8NoBomEncoding.GetBytes(xmlString);
}
byte[] GetRegionByPosition(Dictionary<string, object> request)
@ -289,9 +288,9 @@ namespace OpenSim.Server.Handlers.Grid
result["result"] = rinfo.ToKeyValuePairs();
string xmlString = ServerUtils.BuildXmlResponse(result);
//m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
UTF8Encoding encoding = new UTF8Encoding();
return encoding.GetBytes(xmlString);
return Util.UTF8NoBomEncoding.GetBytes(xmlString);
}
byte[] GetRegionByName(Dictionary<string, object> request)
@ -318,9 +317,9 @@ namespace OpenSim.Server.Handlers.Grid
result["result"] = rinfo.ToKeyValuePairs();
string xmlString = ServerUtils.BuildXmlResponse(result);
//m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
UTF8Encoding encoding = new UTF8Encoding();
return encoding.GetBytes(xmlString);
return Util.UTF8NoBomEncoding.GetBytes(xmlString);
}
byte[] GetRegionsByName(Dictionary<string, object> request)
@ -361,9 +360,9 @@ namespace OpenSim.Server.Handlers.Grid
}
string xmlString = ServerUtils.BuildXmlResponse(result);
//m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
UTF8Encoding encoding = new UTF8Encoding();
return encoding.GetBytes(xmlString);
return Util.UTF8NoBomEncoding.GetBytes(xmlString);
}
byte[] GetRegionRange(Dictionary<string, object> request)
@ -410,9 +409,9 @@ namespace OpenSim.Server.Handlers.Grid
}
}
string xmlString = ServerUtils.BuildXmlResponse(result);
//m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
UTF8Encoding encoding = new UTF8Encoding();
return encoding.GetBytes(xmlString);
return Util.UTF8NoBomEncoding.GetBytes(xmlString);
}
byte[] GetDefaultRegions(Dictionary<string, object> request)
@ -440,9 +439,9 @@ namespace OpenSim.Server.Handlers.Grid
}
}
string xmlString = ServerUtils.BuildXmlResponse(result);
//m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
UTF8Encoding encoding = new UTF8Encoding();
return encoding.GetBytes(xmlString);
return Util.UTF8NoBomEncoding.GetBytes(xmlString);
}
byte[] GetFallbackRegions(Dictionary<string, object> request)
@ -481,9 +480,9 @@ namespace OpenSim.Server.Handlers.Grid
}
}
string xmlString = ServerUtils.BuildXmlResponse(result);
//m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
UTF8Encoding encoding = new UTF8Encoding();
return encoding.GetBytes(xmlString);
return Util.UTF8NoBomEncoding.GetBytes(xmlString);
}
byte[] GetHyperlinks(Dictionary<string, object> request)
@ -511,9 +510,9 @@ namespace OpenSim.Server.Handlers.Grid
}
}
string xmlString = ServerUtils.BuildXmlResponse(result);
//m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
UTF8Encoding encoding = new UTF8Encoding();
return encoding.GetBytes(xmlString);
return Util.UTF8NoBomEncoding.GetBytes(xmlString);
}
byte[] GetRegionFlags(Dictionary<string, object> request)
@ -537,11 +536,10 @@ namespace OpenSim.Server.Handlers.Grid
result["result"] = flags.ToString();
string xmlString = ServerUtils.BuildXmlResponse(result);
//m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
UTF8Encoding encoding = new UTF8Encoding();
return encoding.GetBytes(xmlString);
}
//m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
return Util.UTF8NoBomEncoding.GetBytes(xmlString);
}
#endregion

View File

@ -117,10 +117,9 @@ namespace OpenSim.Server.Handlers.GridUser
result["result"] = guinfo.ToKeyValuePairs();
string xmlString = ServerUtils.BuildXmlResponse(result);
//m_log.DebugFormat("[GRID USER HANDLER]: resp string: {0}", xmlString);
UTF8Encoding encoding = new UTF8Encoding();
return encoding.GetBytes(xmlString);
//m_log.DebugFormat("[GRID USER HANDLER]: resp string: {0}", xmlString);
return Util.UTF8NoBomEncoding.GetBytes(xmlString);
}
byte[] LoggedOut(Dictionary<string, object> request)
@ -189,10 +188,9 @@ namespace OpenSim.Server.Handlers.GridUser
result["result"] = guinfo.ToKeyValuePairs();
string xmlString = ServerUtils.BuildXmlResponse(result);
//m_log.DebugFormat("[GRID USER HANDLER]: resp string: {0}", xmlString);
UTF8Encoding encoding = new UTF8Encoding();
return encoding.GetBytes(xmlString);
//m_log.DebugFormat("[GRID USER HANDLER]: resp string: {0}", xmlString);
return Util.UTF8NoBomEncoding.GetBytes(xmlString);
}
byte[] GetGridUserInfos(Dictionary<string, object> request)
@ -231,8 +229,7 @@ namespace OpenSim.Server.Handlers.GridUser
}
string xmlString = ServerUtils.BuildXmlResponse(result);
UTF8Encoding encoding = new UTF8Encoding();
return encoding.GetBytes(xmlString);
return Util.UTF8NoBomEncoding.GetBytes(xmlString);
}
private bool UnpackArgs(Dictionary<string, object> request, out string user, out UUID region, out Vector3 position, out Vector3 lookAt)

View File

@ -279,13 +279,11 @@ namespace OpenSim.Server.Handlers.Hypergrid
}
string xmlString = ServerUtils.BuildXmlResponse(result);
//m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
UTF8Encoding encoding = new UTF8Encoding();
return encoding.GetBytes(xmlString);
return Util.UTF8NoBomEncoding.GetBytes(xmlString);
}
#endregion
#region Misc

View File

@ -217,9 +217,9 @@ namespace OpenSim.Server.Handlers.Asset
result["RESULT"] = "False";
string xmlString = ServerUtils.BuildXmlResponse(result);
//m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
UTF8Encoding encoding = new UTF8Encoding();
return encoding.GetBytes(xmlString);
return Util.UTF8NoBomEncoding.GetBytes(xmlString);
}
byte[] HandleGetInventorySkeleton(Dictionary<string,object> request)
@ -245,9 +245,9 @@ namespace OpenSim.Server.Handlers.Asset
result["FOLDERS"] = sfolders;
string xmlString = ServerUtils.BuildXmlResponse(result);
//m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
UTF8Encoding encoding = new UTF8Encoding();
return encoding.GetBytes(xmlString);
return Util.UTF8NoBomEncoding.GetBytes(xmlString);
}
byte[] HandleGetUserInventory(Dictionary<string, object> request)
@ -284,9 +284,9 @@ namespace OpenSim.Server.Handlers.Asset
}
string xmlString = ServerUtils.BuildXmlResponse(result);
//m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
UTF8Encoding encoding = new UTF8Encoding();
return encoding.GetBytes(xmlString);
return Util.UTF8NoBomEncoding.GetBytes(xmlString);
}
byte[] HandleGetRootFolder(Dictionary<string,object> request)
@ -300,9 +300,9 @@ namespace OpenSim.Server.Handlers.Asset
result["folder"] = EncodeFolder(rfolder);
string xmlString = ServerUtils.BuildXmlResponse(result);
//m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
UTF8Encoding encoding = new UTF8Encoding();
return encoding.GetBytes(xmlString);
return Util.UTF8NoBomEncoding.GetBytes(xmlString);
}
byte[] HandleGetFolderForType(Dictionary<string,object> request)
@ -317,9 +317,9 @@ namespace OpenSim.Server.Handlers.Asset
result["folder"] = EncodeFolder(folder);
string xmlString = ServerUtils.BuildXmlResponse(result);
//m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
UTF8Encoding encoding = new UTF8Encoding();
return encoding.GetBytes(xmlString);
return Util.UTF8NoBomEncoding.GetBytes(xmlString);
}
byte[] HandleGetFolderContent(Dictionary<string,object> request)
@ -358,9 +358,9 @@ namespace OpenSim.Server.Handlers.Asset
}
string xmlString = ServerUtils.BuildXmlResponse(result);
//m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
UTF8Encoding encoding = new UTF8Encoding();
return encoding.GetBytes(xmlString);
return Util.UTF8NoBomEncoding.GetBytes(xmlString);
}
byte[] HandleGetFolderItems(Dictionary<string,object> request)
@ -386,9 +386,9 @@ namespace OpenSim.Server.Handlers.Asset
result["ITEMS"] = sitems;
string xmlString = ServerUtils.BuildXmlResponse(result);
//m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
UTF8Encoding encoding = new UTF8Encoding();
return encoding.GetBytes(xmlString);
return Util.UTF8NoBomEncoding.GetBytes(xmlString);
}
byte[] HandleAddFolder(Dictionary<string,object> request)
@ -550,9 +550,9 @@ namespace OpenSim.Server.Handlers.Asset
result["item"] = EncodeItem(item);
string xmlString = ServerUtils.BuildXmlResponse(result);
//m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
UTF8Encoding encoding = new UTF8Encoding();
return encoding.GetBytes(xmlString);
return Util.UTF8NoBomEncoding.GetBytes(xmlString);
}
byte[] HandleGetFolder(Dictionary<string,object> request)
@ -567,9 +567,9 @@ namespace OpenSim.Server.Handlers.Asset
result["folder"] = EncodeFolder(folder);
string xmlString = ServerUtils.BuildXmlResponse(result);
//m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
UTF8Encoding encoding = new UTF8Encoding();
return encoding.GetBytes(xmlString);
return Util.UTF8NoBomEncoding.GetBytes(xmlString);
}
byte[] HandleGetActiveGestures(Dictionary<string,object> request)
@ -592,9 +592,9 @@ namespace OpenSim.Server.Handlers.Asset
result["ITEMS"] = items;
string xmlString = ServerUtils.BuildXmlResponse(result);
//m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
UTF8Encoding encoding = new UTF8Encoding();
return encoding.GetBytes(xmlString);
return Util.UTF8NoBomEncoding.GetBytes(xmlString);
}
byte[] HandleGetAssetPermissions(Dictionary<string,object> request)
@ -609,11 +609,10 @@ namespace OpenSim.Server.Handlers.Asset
result["RESULT"] = perms.ToString();
string xmlString = ServerUtils.BuildXmlResponse(result);
//m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
UTF8Encoding encoding = new UTF8Encoding();
return encoding.GetBytes(xmlString);
}
//m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
return Util.UTF8NoBomEncoding.GetBytes(xmlString);
}
private Dictionary<string, object> EncodeFolder(InventoryFolderBase f)
{

View File

@ -199,9 +199,9 @@ namespace OpenSim.Server.Handlers.Presence
result["result"] = pinfo.ToKeyValuePairs();
string xmlString = ServerUtils.BuildXmlResponse(result);
//m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
UTF8Encoding encoding = new UTF8Encoding();
return encoding.GetBytes(xmlString);
return Util.UTF8NoBomEncoding.GetBytes(xmlString);
}
byte[] GetAgents(Dictionary<string, object> request)
@ -240,11 +240,10 @@ namespace OpenSim.Server.Handlers.Presence
}
string xmlString = ServerUtils.BuildXmlResponse(result);
//m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
UTF8Encoding encoding = new UTF8Encoding();
return encoding.GetBytes(xmlString);
}
//m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
return Util.UTF8NoBomEncoding.GetBytes(xmlString);
}
private byte[] SuccessResult()
{

View File

@ -195,9 +195,9 @@ namespace OpenSim.Server.Handlers.UserAccounts
}
string xmlString = ServerUtils.BuildXmlResponse(result);
//m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
UTF8Encoding encoding = new UTF8Encoding();
return encoding.GetBytes(xmlString);
return Util.UTF8NoBomEncoding.GetBytes(xmlString);
}
byte[] StoreAccount(Dictionary<string, object> request)
@ -353,8 +353,7 @@ namespace OpenSim.Server.Handlers.UserAccounts
private byte[] ResultToBytes(Dictionary<string, object> result)
{
string xmlString = ServerUtils.BuildXmlResponse(result);
UTF8Encoding encoding = new UTF8Encoding();
return encoding.GetBytes(xmlString);
return Util.UTF8NoBomEncoding.GetBytes(xmlString);
}
}
}

View File

@ -132,8 +132,7 @@ namespace OpenSim.Services.Connectors
try
{
strBuffer = OSDParser.SerializeJsonString(args);
UTF8Encoding str = new UTF8Encoding();
buffer = str.GetBytes(strBuffer);
buffer = Util.UTF8NoBomEncoding.GetBytes(strBuffer);
}
catch (Exception e)
{