mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Simplify ContentHelper methods (#19367)
This commit is contained in:
+5
-38
@@ -60,28 +60,6 @@ namespace Microsoft.PowerShell.Commands
|
||||
}
|
||||
|
||||
internal static bool IsJson(string contentType)
|
||||
{
|
||||
contentType = GetContentTypeSignature(contentType);
|
||||
return CheckIsJson(contentType);
|
||||
}
|
||||
|
||||
internal static bool IsText(string contentType)
|
||||
{
|
||||
contentType = GetContentTypeSignature(contentType);
|
||||
return CheckIsText(contentType);
|
||||
}
|
||||
|
||||
internal static bool IsXml(string contentType)
|
||||
{
|
||||
contentType = GetContentTypeSignature(contentType);
|
||||
return CheckIsXml(contentType);
|
||||
}
|
||||
|
||||
#endregion Internal Methods
|
||||
|
||||
#region Private Helper Methods
|
||||
|
||||
private static bool CheckIsJson(string contentType)
|
||||
{
|
||||
if (string.IsNullOrEmpty(contentType))
|
||||
{
|
||||
@@ -102,7 +80,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
return isJson;
|
||||
}
|
||||
|
||||
private static bool CheckIsText(string contentType)
|
||||
internal static bool IsText(string contentType)
|
||||
{
|
||||
if (string.IsNullOrEmpty(contentType))
|
||||
{
|
||||
@@ -111,8 +89,8 @@ namespace Microsoft.PowerShell.Commands
|
||||
|
||||
// Any text, xml or json types are text
|
||||
bool isText = contentType.StartsWith("text/", StringComparison.OrdinalIgnoreCase)
|
||||
|| CheckIsXml(contentType)
|
||||
|| CheckIsJson(contentType);
|
||||
|| IsXml(contentType)
|
||||
|| IsJson(contentType);
|
||||
|
||||
// Further content type analysis is available on Windows
|
||||
if (Platform.IsWindows && !isText)
|
||||
@@ -141,7 +119,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
return isText;
|
||||
}
|
||||
|
||||
private static bool CheckIsXml(string contentType)
|
||||
internal static bool IsXml(string contentType)
|
||||
{
|
||||
if (string.IsNullOrEmpty(contentType))
|
||||
{
|
||||
@@ -157,17 +135,6 @@ namespace Microsoft.PowerShell.Commands
|
||||
return isXml;
|
||||
}
|
||||
|
||||
private static string GetContentTypeSignature(string contentType)
|
||||
{
|
||||
if (string.IsNullOrEmpty(contentType))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
string sig = contentType.Split(';', 2)[0].ToUpperInvariant();
|
||||
return sig;
|
||||
}
|
||||
|
||||
#endregion Private Helper Methods
|
||||
#endregion Internal Methods
|
||||
}
|
||||
}
|
||||
|
||||
+3
-8
@@ -383,13 +383,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
|
||||
internal static string DecodeStream(Stream stream, string characterSet, out Encoding encoding)
|
||||
{
|
||||
bool isDefaultEncoding = false;
|
||||
if (!TryGetEncoding(characterSet, out encoding))
|
||||
{
|
||||
// Use the default encoding if one wasn't provided
|
||||
encoding = ContentHelper.GetDefaultEncoding();
|
||||
isDefaultEncoding = true;
|
||||
}
|
||||
bool isDefaultEncoding = !TryGetEncoding(characterSet, out encoding);
|
||||
|
||||
string content = StreamToString(stream, encoding);
|
||||
if (isDefaultEncoding)
|
||||
@@ -433,7 +427,8 @@ namespace Microsoft.PowerShell.Commands
|
||||
}
|
||||
catch (ArgumentException)
|
||||
{
|
||||
encoding = null;
|
||||
// Use the default encoding if one wasn't provided
|
||||
encoding = ContentHelper.GetDefaultEncoding();
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
Reference in New Issue
Block a user