public void testOneByte()
 {
     TemporaryBuffer b = new TemporaryBuffer();
     byte test = (byte)new TestRng(getName()).nextInt();
     try
     {
         b.write(test);
         b.close();
         Assert.AreEqual(1, b.Length);
         {
             byte[] r = b.ToArray();
             Assert.NotNull(r);
             Assert.AreEqual(1, r.Length);
             Assert.AreEqual(test, r[0]);
         }
         {
             var o = new MemoryStream();
             b.writeTo(o, null);
             o.Close();
             byte[] r = o.ToArray();
             Assert.AreEqual(1, r.Length);
             Assert.AreEqual(test, r[0]);
         }
     }
     finally
     {
         b.destroy();
     }
 }
 public void testDestroyWhileOpen()
 {
     TemporaryBuffer b = new TemporaryBuffer();
     try
     {
         b.write(new TestRng(getName())
                 .nextBytes(TemporaryBuffer.DEFAULT_IN_CORE_LIMIT * 2));
     }
     finally
     {
         b.destroy();
     }
 }
 public void testEmpty()
 {
     TemporaryBuffer b = new TemporaryBuffer();
     try
     {
         b.close();
         Assert.AreEqual(0, b.Length);
         byte[] r = b.ToArray();
         Assert.IsNotNull(r);
         Assert.AreEqual(0, r.Length);
     }
     finally
     {
         b.destroy();
     }
 }
Exemple #4
0
        private void writeTo(Stream os)
        {
            MessageDigest foot = Constants.newMessageDigest();
            DigestOutputStream dos = new DigestOutputStream(os, foot);

            // Write the header.
            //
            byte[] tmp = new byte[128];
            Array.Copy(SIG_DIRC, 0, tmp, 0, SIG_DIRC.Length);
            NB.encodeInt32(tmp, 4, /* version */2);
            NB.encodeInt32(tmp, 8, entryCnt);
            dos.Write(tmp, 0, 12);

            // Write the individual file entries.
            //
            if (lastModified == DateTime.MinValue)
            {
                // Write a new index, as no entries require smudging.
                //
                for (int i = 0; i < entryCnt; i++)
                    sortedEntries[i].write(dos);
            }
            else
            {
                int smudge_s = (int)(lastModified.ToGitInternalTime());
                int smudge_ns = (int)(lastModified.Millisecond * 1000000); // [henon] <--- this could be done with much more precision in C# since DateTime has 100 nanosec ticks
                for (int i = 0; i < entryCnt; i++)
                {
                    DirCacheEntry e = sortedEntries[i];
                    if (e.mightBeRacilyClean(smudge_s, smudge_ns))
                        e.smudgeRacilyClean();
                    e.write(dos);
                }
            }

            if (tree != null)
            {
                TemporaryBuffer bb = new TemporaryBuffer();
                tree.write(tmp, bb);
                bb.close();

                NB.encodeInt32(tmp, 0, EXT_TREE);
                NB.encodeInt32(tmp, 4, (int)bb.Length);
                dos.Write(tmp, 0, 8);
                bb.writeTo(dos, null);
            }
            var hash = foot.Digest();
            os.Write(hash, 0, hash.Length);
            os.Close();
        }
Exemple #5
0
	    private String[] extractFileLines(Encoding[] csGuess)
        {
		    TemporaryBuffer[] tmp = new TemporaryBuffer[getParentCount() + 1];
		    try
            {
			    for (int i = 0; i < tmp.Length; i++)
				    tmp[i] = new TemporaryBuffer();
			    foreach (HunkHeader h in getHunks())
				    h.extractFileLines(tmp);

			    String[] r = new String[tmp.Length];
			    for (int i = 0; i < tmp.Length; i++) {
				    Encoding cs = csGuess != null ? csGuess[i] : null;
				    if (cs == null)
					    cs = Constants.CHARSET;
				    r[i] = RawParseUtils.decode(cs, tmp[i].ToArray());
			    }
			    return r;
		    }
            catch (IOException ioe)
            {
			    throw new Exception("Cannot convert script to text", ioe);
		    }
            finally
            {
			    foreach (TemporaryBuffer b in tmp)
                {
				    if (b != null)
					    b.destroy();
			    }
		    }
	    }
 public void testInCoreLimit_SwitchBeforeAppendByte()
 {
     TemporaryBuffer b = new TemporaryBuffer();
     byte[] test = new TestRng(getName())
            .nextBytes(TemporaryBuffer.DEFAULT_IN_CORE_LIMIT * 3);
     try
     {
         b.write(test, 0, test.Length - 1);
         b.write(test[test.Length - 1]);
         b.close();
         Assert.AreEqual(test.Length, b.Length);
         {
             byte[] r = b.ToArray();
             Assert.IsNotNull(r);
             Assert.AreEqual(test.Length, r.Length);
             Assert.IsTrue(test.SequenceEqual(r));
         }
         {
             MemoryStream o = new MemoryStream();
             b.writeTo(o, null);
             o.Close();
             byte[] r = o.ToArray();
             Assert.AreEqual(test.Length, r.Length);
             Assert.IsTrue(test.SequenceEqual(r));
         }
     }
     finally
     {
         b.destroy();
     }
 }
        public void testRandomWrites()
        {
            TemporaryBuffer b = new TemporaryBuffer();
            TestRng rng = new TestRng(getName());
            int max = TemporaryBuffer.DEFAULT_IN_CORE_LIMIT * 2;
            byte[] expect = new byte[max];
            try
            {
                int written = 0;
                bool onebyte = true;
                while (written < max)
                {
                    if (onebyte)
                    {
                        byte v = (byte)rng.nextInt();
                        b.write(v);
                        expect[written++] = v;
                    }
                    else
                    {
                        int len = Math.Min(rng.nextInt() & 127, max - written);
                        byte[] tmp = rng.nextBytes(len);
                        b.write(tmp, 0, len);
                        Array.Copy(tmp, 0, expect, written, len);
                        written += len;
                    }
                    onebyte = !onebyte;
                }
                Assert.AreEqual(expect.Length, written);
                b.close();

                Assert.AreEqual(expect.Length, b.Length);
                {
                    byte[] r = b.ToArray();
                    Assert.IsNotNull(r);
                    Assert.AreEqual(expect.Length, r.Length);
                    Assert.IsTrue(expect.SequenceEqual(r));
                }
                {
                    MemoryStream o = new MemoryStream();
                    b.writeTo(o, null);
                    o.Close();
                    byte[] r = o.ToArray();
                    Assert.AreEqual(expect.Length, r.Length);
                    Assert.IsTrue(expect.SequenceEqual(r));
                }
            }
            finally
            {
                b.destroy();
            }
        }
 public void testOneBlock_BulkWrite()
 {
     TemporaryBuffer b = new TemporaryBuffer();
     byte[] test = new TestRng(getName())
            .nextBytes(TemporaryBuffer.Block.SZ);
     try
     {
         b.write(test, 0, 2);
         b.write(test, 2, 4);
         b.write(test, 6, test.Length - 6 - 2);
         b.write(test, test.Length - 2, 2);
         b.close();
         Assert.AreEqual(test.Length, b.Length);
         {
             byte[] r = b.ToArray();
             Assert.IsNotNull(r);
             Assert.AreEqual(test.Length, r.Length);
             Assert.IsTrue(test.SequenceEqual(r));
         }
         {
             MemoryStream o = new MemoryStream();
             b.writeTo(o, null);
             o.Close();
             byte[] r = o.ToArray();
             Assert.AreEqual(test.Length, r.Length);
             Assert.IsTrue(test.SequenceEqual(r));
         }
     }
     finally
     {
         b.destroy();
     }
 }
 public void testOneBlockAndHalf_SingleWrite()
 {
     TemporaryBuffer b = new TemporaryBuffer();
     byte[] test = new TestRng(getName())
            .nextBytes(TemporaryBuffer.Block.SZ * 3 / 2);
     try
     {
         for (int i = 0; i < test.Length; i++)
             b.write(test[i]);
         b.close();
         Assert.AreEqual(test.Length, b.Length);
         {
             byte[] r = b.ToArray();
             Assert.IsNotNull(r);
             Assert.AreEqual(test.Length, r.Length);
             Assert.IsTrue(test.SequenceEqual(r));
         }
         {
             MemoryStream o = new MemoryStream();
             b.writeTo(o, null);
             o.Close();
             byte[] r = o.ToArray();
             Assert.AreEqual(test.Length, r.Length);
             Assert.IsTrue(test.SequenceEqual(r));
         }
     }
     finally
     {
         b.destroy();
     }
 }
 public void testOneBlockAndHalf_Copy()
 {
     TemporaryBuffer b = new TemporaryBuffer();
     byte[] test = new TestRng(getName())
            .nextBytes(TemporaryBuffer.Block.SZ * 3 / 2);
     try
     {
         var @in = new MemoryStream(test);
         // [caytchen] StreamReader buffers data After the very first Read, thus advancing the Position in the underlying stream - causing this test to fail
         //var inReader = new StreamReader(@in);
         b.write(@in.ReadByte());
         b.copy(@in);
         b.close();
         Assert.AreEqual(test.Length, b.Length);
         {
             byte[] r = b.ToArray();
             Assert.IsNotNull(r);
             Assert.AreEqual(test.Length, r.Length);
             Assert.IsTrue(test.SequenceEqual(r));
         }
         {
             MemoryStream o = new MemoryStream();
             b.writeTo(o, null);
             o.Close();
             byte[] r = o.ToArray();
             Assert.AreEqual(test.Length, r.Length);
             Assert.IsTrue(test.SequenceEqual(r));
         }
     }
     finally
     {
         b.destroy();
     }
 }
Exemple #11
0
	    private static byte[] readFully(Stream iStream)
        {
		    TemporaryBuffer b = new TemporaryBuffer();
		    try {
			    b.copy(iStream);
			    b.close();
			    return b.ToArray();
		    } finally {
			    b.destroy();
		    }
	    }
Exemple #12
0
        public void extractFileLines(TemporaryBuffer[] outStream)
        {
            byte[] buf = _file.Buffer;
            int ptr = _startOffset;
            int eol = RawParseUtils.nextLF(buf, ptr);
            if (EndOffset <= eol)
                return;

            // Treat the hunk header as though it were from the ancestor,
            // as it may have a function header appearing After it which
            // was copied out of the ancestor file.
            //
            outStream[0].write(buf, ptr, eol - ptr);

            bool break_scan = false;
            for (ptr = eol; ptr < EndOffset; ptr = eol)
            {
                eol = RawParseUtils.nextLF(buf, ptr);
                switch (buf[ptr])
                {
                    case (byte)' ':
                    case (byte)'\n':
                    case (byte)'\\':
                        outStream[0].write(buf, ptr, eol - ptr);
                        outStream[1].write(buf, ptr, eol - ptr);
                        break;
                    case (byte)'-':
                        outStream[0].write(buf, ptr, eol - ptr);
                        break;
                    case (byte)'+':
                        outStream[1].write(buf, ptr, eol - ptr);
                        break;
                    default:
                        break_scan = true;
                        break;
                }
                if (break_scan)
                    break;
            }
        }
 public void testLarge_SingleWrite()
 {
     TemporaryBuffer b = new TemporaryBuffer();
     byte[] test = new TestRng(getName()).nextBytes(TemporaryBuffer.DEFAULT_IN_CORE_LIMIT * 3);
     try
     {
         b.write(test);
         b.close();
         Assert.AreEqual(test.Length, b.Length);
         {
             byte[] r = b.ToArray();
             Assert.NotNull(r);
             Assert.AreEqual(test.Length, r.Length);
             Assert.IsTrue(test.SequenceEqual(r));
         }
         {
             MemoryStream o = new MemoryStream();
             b.writeTo(o, null);
             o.Close();
             byte[] r = o.ToArray();
             Assert.AreEqual(test.Length, r.Length);
             Assert.IsTrue(test.SequenceEqual(r));
         }
     }
     finally
     {
         b.destroy();
     }
 }
Exemple #14
0
		private void WriteTo(Stream os)
		{
			MessageDigest foot = Constants.newMessageDigest();
			var dos = new DigestOutputStream(os, foot);

			// Write the header.
			//
			var tmp = new byte[128];
			Array.Copy(SigDirc, 0, tmp, 0, SigDirc.Length);
			NB.encodeInt32(tmp, 4, /* version */2);
			NB.encodeInt32(tmp, 8, _entryCnt);
			dos.Write(tmp, 0, 12);

			// Write the individual file entries.
			//
			if (_lastModified == DateTime.MinValue)
			{
				// Write a new index, as no entries require smudging.
				//
				for (int i = 0; i < _entryCnt; i++)
				{
					_sortedEntries[i].write(dos);
				}
			}
			else
			{
				var smudge_s = _lastModified.ToUnixTime();
				var smudge_ns = _lastModified.Millisecond * 1000000; // [henon] <--- this could be done with much more precision in C# since DateTime has 100 nanosec ticks
				for (int i = 0; i < _entryCnt; i++)
				{
					DirCacheEntry e = _sortedEntries[i];
					if (e.mightBeRacilyClean(smudge_s, smudge_ns))
						e.smudgeRacilyClean();
					e.write(dos);
				}
			}

			if (_cacheTree != null)
			{
				var bb = new TemporaryBuffer();
				_cacheTree.write(tmp, bb);
				bb.close();

				NB.encodeInt32(tmp, 0, ExtTree);
				NB.encodeInt32(tmp, 4, (int)bb.Length);
				dos.Write(tmp, 0, 8);
				bb.writeTo(dos, null);
			}
			var hash = foot.Digest();
			os.Write(hash, 0, hash.Length);
			os.Close();
		}