ApplicationNameHelper.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /* Copyright 2016-present MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System;
  16. using MongoDB.Bson.IO;
  17. namespace MongoDB.Shared
  18. {
  19. internal static class ApplicationNameHelper
  20. {
  21. public static string EnsureApplicationNameIsValid(string applicationName, string paramName)
  22. {
  23. string message;
  24. if (!IsApplicationNameValid(applicationName, out message))
  25. {
  26. throw new ArgumentException(message, paramName);
  27. }
  28. return applicationName;
  29. }
  30. public static bool IsApplicationNameValid(string applicationName, out string message)
  31. {
  32. if (applicationName != null)
  33. {
  34. var utf8 = Utf8Encodings.Strict.GetBytes(applicationName);
  35. if (utf8.Length > 128)
  36. {
  37. message = "Application name exceeds 128 bytes after encoding to UTF8.";
  38. return false;
  39. }
  40. }
  41. message = null;
  42. return true;
  43. }
  44. }
  45. }