diff options
author | nulltoken <emeric.fermas@gmail.com> | 2013-09-07 22:57:51 +0400 |
---|---|---|
committer | nulltoken <emeric.fermas@gmail.com> | 2013-09-08 13:07:28 +0400 |
commit | cfd3c03cda7ec6a100e47cfbb1971f035fae5fd6 (patch) | |
tree | 93349f0fcac5ea31a6583e5cbf70948342959ca6 /LibGit2Sharp/OdbBackend.cs | |
parent | 0625fcee5985fe8b8ec1b8855d77d3fd3a351594 (diff) |
Small refactoring
Diffstat (limited to 'LibGit2Sharp/OdbBackend.cs')
-rw-r--r-- | LibGit2Sharp/OdbBackend.cs | 30 |
1 files changed, 18 insertions, 12 deletions
diff --git a/LibGit2Sharp/OdbBackend.cs b/LibGit2Sharp/OdbBackend.cs index 76551cec..35a4c6af 100644 --- a/LibGit2Sharp/OdbBackend.cs +++ b/LibGit2Sharp/OdbBackend.cs @@ -393,10 +393,7 @@ namespace LibGit2Sharp UIntPtr len, GitObjectType type) { - if (len.ToUInt64() > long.MaxValue) - { - return (int)GitErrorCode.Error; - } + long length = ConverToLong(len); OdbBackend odbBackend = MarshalOdbBackend(backend); if (odbBackend == null) @@ -406,9 +403,9 @@ namespace LibGit2Sharp try { - using (var stream = new UnmanagedMemoryStream((byte*)data, (long)len.ToUInt64())) + using (var stream = new UnmanagedMemoryStream((byte*)data, length)) { - return odbBackend.Write(new ObjectId(oid), stream, (long)len.ToUInt64(), type.ToObjectType()); + return odbBackend.Write(new ObjectId(oid), stream, length, type.ToObjectType()); } } catch (Exception ex) @@ -421,15 +418,12 @@ namespace LibGit2Sharp private static int WriteStream( out IntPtr stream_out, IntPtr backend, - UIntPtr length, + UIntPtr len, GitObjectType type) { stream_out = IntPtr.Zero; - if (length.ToUInt64() > long.MaxValue) - { - return (int)GitErrorCode.Error; - } + long length = ConverToLong(len); OdbBackend odbBackend = MarshalOdbBackend(backend); if (odbBackend == null) @@ -442,7 +436,7 @@ namespace LibGit2Sharp try { OdbBackendStream stream; - int toReturn = odbBackend.WriteStream((long)length.ToUInt64(), objectType, out stream); + int toReturn = odbBackend.WriteStream(length, objectType, out stream); if (toReturn == 0) { @@ -578,6 +572,18 @@ namespace LibGit2Sharp } } + internal static long ConverToLong(UIntPtr len) + { + if (len.ToUInt64() > long.MaxValue) + { + throw new InvalidOperationException( + string.Format("Provided length ({0}) exceeds long.MaxValue ({1}).", + len.ToUInt64(), long.MaxValue)); + } + + return (long)len.ToUInt64(); + } + /// <summary> /// Flags used by subclasses of OdbBackend to indicate which operations they support. /// </summary> |