Example #1
0
        /// <exception cref="System.IO.IOException"></exception>
        private static byte[] ToByteArray(ObjectId id)
        {
            ByteArrayOutputStream buf = new ByteArrayOutputStream(Constants.OBJECT_ID_LENGTH);

            id.CopyRawTo(buf);
            return(buf.ToByteArray());
        }
Example #2
0
        public virtual void TestDelta_SmallObjectChain()
        {
            ObjectInserter.Formatter fmt = new ObjectInserter.Formatter();
            byte[] data0 = new byte[512];
            Arrays.Fill(data0, unchecked ((byte)unchecked ((int)(0xf3))));
            ObjectId id0 = fmt.IdFor(Constants.OBJ_BLOB, data0);

            TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(64 * 1024);
            PackHeader(pack, 4);
            ObjectHeader(pack, Constants.OBJ_BLOB, data0.Length);
            Deflate(pack, data0);
            byte[]   data1  = Clone(unchecked ((int)(0x01)), data0);
            byte[]   delta1 = Delta(data0, data1);
            ObjectId id1    = fmt.IdFor(Constants.OBJ_BLOB, data1);

            ObjectHeader(pack, Constants.OBJ_REF_DELTA, delta1.Length);
            id0.CopyRawTo(pack);
            Deflate(pack, delta1);
            byte[]   data2  = Clone(unchecked ((int)(0x02)), data1);
            byte[]   delta2 = Delta(data1, data2);
            ObjectId id2    = fmt.IdFor(Constants.OBJ_BLOB, data2);

            ObjectHeader(pack, Constants.OBJ_REF_DELTA, delta2.Length);
            id1.CopyRawTo(pack);
            Deflate(pack, delta2);
            byte[]   data3  = Clone(unchecked ((int)(0x03)), data2);
            byte[]   delta3 = Delta(data2, data3);
            ObjectId id3    = fmt.IdFor(Constants.OBJ_BLOB, data3);

            ObjectHeader(pack, Constants.OBJ_REF_DELTA, delta3.Length);
            id2.CopyRawTo(pack);
            Deflate(pack, delta3);
            Digest(pack);
            PackParser ip = Index(pack.ToByteArray());

            ip.SetAllowThin(true);
            ip.Parse(NullProgressMonitor.INSTANCE);
            NUnit.Framework.Assert.IsTrue(wc.Has(id3), "has blob");
            ObjectLoader ol = wc.Open(id3);

            NUnit.Framework.Assert.IsNotNull(ol, "created loader");
            NUnit.Framework.Assert.AreEqual(Constants.OBJ_BLOB, ol.GetType());
            NUnit.Framework.Assert.AreEqual(data3.Length, ol.GetSize());
            NUnit.Framework.Assert.IsFalse(ol.IsLarge(), "is large");
            NUnit.Framework.Assert.IsNotNull(ol.GetCachedBytes());
            CollectionAssert.AreEquivalent(data3, ol.GetCachedBytes());
            ObjectStream @in = ol.OpenStream();

            NUnit.Framework.Assert.IsNotNull(@in, "have stream");
            NUnit.Framework.Assert.AreEqual(Constants.OBJ_BLOB, @in.GetType());
            NUnit.Framework.Assert.AreEqual(data3.Length, @in.GetSize());
            byte[] act = new byte[data3.Length];
            IOUtil.ReadFully(@in, act, 0, data3.Length);
            NUnit.Framework.Assert.IsTrue(Arrays.Equals(act, data3), "same content");
            NUnit.Framework.Assert.AreEqual(-1, @in.Read(), "stream at EOF");
            @in.Close();
        }
        /// <exception cref="System.Exception"></exception>
        private static byte[] Entry(FileMode mode, string name, ObjectId id)
        {
            ByteArrayOutputStream @out = new ByteArrayOutputStream();

            mode.CopyTo(@out);
            @out.Write(' ');
            @out.Write(Constants.Encode(name));
            @out.Write(0);
            id.CopyRawTo(@out);
            return(@out.ToByteArray());
        }
        /// <exception cref="System.IO.IOException"></exception>
        internal virtual void Write(byte[] tmp, OutputStream os)
        {
            int ptr = tmp.Length;

            tmp[--ptr] = (byte)('\n');
            ptr        = RawParseUtils.FormatBase10(tmp, ptr, childCnt);
            tmp[--ptr] = (byte)(' ');
            ptr        = RawParseUtils.FormatBase10(tmp, ptr, IsValid() ? entrySpan : -1);
            tmp[--ptr] = 0;
            os.Write(encodedName);
            os.Write(tmp, ptr, tmp.Length - ptr);
            if (IsValid())
            {
                id.CopyRawTo(tmp, 0);
                os.Write(tmp, 0, Constants.OBJECT_ID_LENGTH);
            }
            for (int i = 0; i < childCnt; i++)
            {
                children[i].Write(tmp, os);
            }
        }