public virtual void TestEncodeASCII_Number13() { long src = 13; byte[] exp = new byte[] { (byte)('1'), (byte)('3') }; byte[] res = Constants.EncodeASCII(src); NUnit.Framework.Assert.IsTrue(Arrays.Equals(exp, res)); }
/// <summary>Convert an ObjectId from hex characters.</summary> /// <remarks>Convert an ObjectId from hex characters.</remarks> /// <param name="str">the string to read from. Must be 40 characters long.</param> /// <returns>the converted object id.</returns> public static NGit.ObjectId FromString(string str) { if (str.Length != Constants.OBJECT_ID_STRING_LENGTH) { throw new ArgumentException("Invalid id: " + str); } return(FromHexString(Constants.EncodeASCII(str), 0)); }
/// <summary>Convert an ObjectId from hex characters.</summary> /// <remarks>Convert an ObjectId from hex characters.</remarks> /// <param name="str">the string to read from. Must be 40 characters long.</param> public virtual void FromString(string str) { if (str.Length != Constants.OBJECT_ID_STRING_LENGTH) { throw new ArgumentException(MessageFormat.Format(JGitText.Get().invalidId, str)); } FromHexString(Constants.EncodeASCII(str), 0); }
public virtual void TestEncodeASCII_Number13() { long src = 13; byte[] exp = new byte[] { (byte)('1'), (byte)('3') }; byte[] res = Constants.EncodeASCII(src); CollectionAssert.AreEquivalent(exp, res); }
/// <summary>Convert an AbbreviatedObjectId from hex characters.</summary> /// <remarks>Convert an AbbreviatedObjectId from hex characters.</remarks> /// <param name="str">the string to read from. Must be <= 40 characters.</param> /// <returns>the converted object id.</returns> public static NGit.AbbreviatedObjectId FromString(string str) { if (str.Length > Constants.OBJECT_ID_STRING_LENGTH) { throw new ArgumentException(MessageFormat.Format(JGitText.Get().invalidId, str)); } byte[] b = Constants.EncodeASCII(str); return(FromHexString(b, 0, b.Length)); }
public virtual void TestEncodeASCII_SimpleASCII() { string src = "abc"; byte[] exp = new byte[] { (byte)('a'), (byte)('b'), (byte)('c') }; byte[] res = Constants.EncodeASCII(src); NUnit.Framework.Assert.IsTrue(Arrays.Equals(exp, res)); NUnit.Framework.Assert.AreEqual(src, Sharpen.Runtime.GetStringForBytes(res, 0, res .Length, "UTF-8")); }
/// <summary>Compute the name of an object, without inserting it.</summary> /// <remarks>Compute the name of an object, without inserting it.</remarks> /// <param name="type">type code of the object to store.</param> /// <param name="data">complete content of the object.</param> /// <param name="off"> /// first position within /// <code>data</code> /// . /// </param> /// <param name="len"> /// number of bytes to copy from /// <code>data</code> /// . /// </param> /// <returns>the name of the object.</returns> public virtual ObjectId IdFor(int type, byte[] data, int off, int len) { MessageDigest md = Digest(); md.Update(Constants.EncodedTypeString(type)); md.Update(unchecked ((byte)' ')); md.Update(Constants.EncodeASCII(len)); md.Update(unchecked ((byte)0)); md.Update(data, off, len); return(ObjectId.FromRaw(md.Digest())); }
/// <summary>Format this builder's state as a commit object.</summary> /// <remarks>Format this builder's state as a commit object.</remarks> /// <returns> /// this object in the canonical commit format, suitable for storage /// in a repository. /// </returns> /// <exception cref="Sharpen.UnsupportedEncodingException"> /// the encoding specified by /// <see cref="Encoding()">Encoding()</see> /// is not /// supported by this Java runtime. /// </exception> public virtual byte[] Build() { ByteArrayOutputStream os = new ByteArrayOutputStream(); OutputStreamWriter w = new OutputStreamWriter(os, Encoding); try { os.Write(htree); os.Write(' '); TreeId.CopyTo(os); os.Write('\n'); foreach (ObjectId p in ParentIds) { os.Write(hparent); os.Write(' '); p.CopyTo(os); os.Write('\n'); } os.Write(hauthor); os.Write(' '); w.Write(Author.ToExternalString()); w.Flush(); os.Write('\n'); os.Write(hcommitter); os.Write(' '); w.Write(Committer.ToExternalString()); w.Flush(); os.Write('\n'); if (Encoding != Constants.CHARSET) { os.Write(hencoding); os.Write(' '); os.Write(Constants.EncodeASCII(Encoding.Name())); os.Write('\n'); } os.Write('\n'); if (Message != null) { w.Write(Message); w.Flush(); } } catch (IOException err) { // This should never occur, the only way to get it above is // for the ByteArrayOutputStream to throw, but it doesn't. // throw new RuntimeException(err); } return(os.ToByteArray()); }
public virtual void TestEncodeASCII_FailOnNonASCII() { string src = "Ūnĭcōde̽"; try { Constants.EncodeASCII(src); NUnit.Framework.Assert.Fail("Incorrectly accepted a Unicode character"); } catch (ArgumentException err) { NUnit.Framework.Assert.AreEqual("Not ASCII string: " + src, err.Message); } }
public virtual void TestFull_FromByteArray() { string s = "7b6e8067ec96acef9a4184b43210d583b6d2f99a"; byte[] b = Constants.EncodeASCII(s); AbbreviatedObjectId i = AbbreviatedObjectId.FromString(b, 0, b.Length); NUnit.Framework.Assert.IsNotNull(i); NUnit.Framework.Assert.AreEqual(s.Length, i.Length); NUnit.Framework.Assert.IsTrue(i.IsComplete); NUnit.Framework.Assert.AreEqual(s, i.Name); ObjectId f = i.ToObjectId(); NUnit.Framework.Assert.IsNotNull(f); NUnit.Framework.Assert.AreEqual(ObjectId.FromString(s), f); NUnit.Framework.Assert.AreEqual(f.GetHashCode(), i.GetHashCode()); }
/// <summary>Compute the name of an object, without inserting it.</summary> /// <remarks>Compute the name of an object, without inserting it.</remarks> /// <param name="objectType">type code of the object to store.</param> /// <param name="length"> /// number of bytes to scan from /// <code>in</code> /// . /// </param> /// <param name="in"> /// stream providing the object content. The caller is responsible /// for closing the stream. /// </param> /// <returns>the name of the object.</returns> /// <exception cref="System.IO.IOException">the source stream could not be read.</exception> public virtual ObjectId IdFor(int objectType, long length, InputStream @in) { MessageDigest md = Digest(); md.Update(Constants.EncodedTypeString(objectType)); md.Update(unchecked ((byte)' ')); md.Update(Constants.EncodeASCII(length)); md.Update(unchecked ((byte)0)); byte[] buf = Buffer(); while (length > 0) { int n = @in.Read(buf, 0, (int)Math.Min(length, buf.Length)); if (n < 0) { throw new EOFException("Unexpected end of input"); } md.Update(buf, 0, n); length -= n; } return(ObjectId.FromRaw(md.Digest())); }