Example #1
0
        public virtual void TestWhole_SmallObject()
        {
            int type = Constants.OBJ_BLOB;

            byte[]  data = GetRng().NextBytes(300);
            RevBlob id   = tr.Blob(data);

            tr.Branch("master").Commit().Add("A", id).Create();
            tr.PackAndPrune();
            NUnit.Framework.Assert.IsTrue(wc.Has(id), "has blob");
            ObjectLoader ol = wc.Open(id);

            NUnit.Framework.Assert.IsNotNull(ol, "created loader");
            NUnit.Framework.Assert.AreEqual(type, ol.GetType());
            NUnit.Framework.Assert.AreEqual(data.Length, ol.GetSize());
            NUnit.Framework.Assert.IsFalse(ol.IsLarge(), "is not large");
            NUnit.Framework.Assert.IsTrue(Arrays.Equals(data, ol.GetCachedBytes()), "same content"
                                          );
            ObjectStream @in = ol.OpenStream();

            NUnit.Framework.Assert.IsNotNull(@in, "have stream");
            NUnit.Framework.Assert.AreEqual(type, @in.GetType());
            NUnit.Framework.Assert.AreEqual(data.Length, @in.GetSize());
            byte[] data2 = new byte[data.Length];
            IOUtil.ReadFully(@in, data2, 0, data.Length);
            NUnit.Framework.Assert.IsTrue(Arrays.Equals(data2, data), "same content");
            NUnit.Framework.Assert.AreEqual(-1, @in.Read(), "stream at EOF");
            @in.Close();
        }
Example #2
0
        public virtual void TestStandardFormat_SmallObject()
        {
            int type = Constants.OBJ_BLOB;

            byte[]       data = GetRng().NextBytes(300);
            byte[]       gz   = CompressStandardFormat(type, data);
            ObjectId     id   = ObjectId.ZeroId;
            ObjectLoader ol   = UnpackedObject.Open(new ByteArrayInputStream(gz), Path(id), id,
                                                    wc);

            NUnit.Framework.Assert.IsNotNull(ol, "created loader");
            NUnit.Framework.Assert.AreEqual(type, ol.GetType());
            NUnit.Framework.Assert.AreEqual(data.Length, ol.GetSize());
            NUnit.Framework.Assert.IsFalse(ol.IsLarge(), "is not large");
            NUnit.Framework.Assert.IsTrue(Arrays.Equals(data, ol.GetCachedBytes()), "same content"
                                          );
            ObjectStream @in = ol.OpenStream();

            NUnit.Framework.Assert.IsNotNull(@in, "have stream");
            NUnit.Framework.Assert.AreEqual(type, @in.GetType());
            NUnit.Framework.Assert.AreEqual(data.Length, @in.GetSize());
            byte[] data2 = new byte[data.Length];
            IOUtil.ReadFully(@in, data2, 0, data.Length);
            NUnit.Framework.Assert.IsTrue(Arrays.Equals(data2, data), "same content");
            NUnit.Framework.Assert.AreEqual(-1, @in.Read(), "stream at EOF");
            @in.Close();
        }
Example #3
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();
        }
 public static byte[]        bytes(this ObjectStream objectStream)
 {
     if (objectStream.notNull())
     {
         try
         {
             var bytes = new byte[objectStream.Available()];
             objectStream.Read(bytes);
             return(bytes);
         }
         catch (Exception ex)
         {
             ex.log("[API_NGit][ObjectStream][bytes]");
         }
     }
     return(new byte[] {});
 }
Example #5
0
        public virtual void TestPackFormat_LargeObject()
        {
            int type = Constants.OBJ_BLOB;

            byte[]   data = GetRng().NextBytes(streamThreshold + 5);
            ObjectId id   = new ObjectInserter.Formatter().IdFor(type, data);

            Write(id, CompressPackFormat(type, data));
            ObjectLoader ol;

            {
                FileInputStream fs = new FileInputStream(Path(id));
                try
                {
                    ol = UnpackedObject.Open(fs, Path(id), id, wc);
                }
                finally
                {
                    fs.Close();
                }
            }
            NUnit.Framework.Assert.IsNotNull(ol, "created loader");
            NUnit.Framework.Assert.AreEqual(type, ol.GetType());
            NUnit.Framework.Assert.AreEqual(data.Length, ol.GetSize());
            NUnit.Framework.Assert.IsTrue(ol.IsLarge(), "is large");
            try
            {
                ol.GetCachedBytes();
                NUnit.Framework.Assert.Fail("Should have thrown LargeObjectException");
            }
            catch (LargeObjectException tooBig)
            {
                NUnit.Framework.Assert.AreEqual(MessageFormat.Format(JGitText.Get().largeObjectException
                                                                     , id.Name), tooBig.Message);
            }
            ObjectStream @in = ol.OpenStream();

            NUnit.Framework.Assert.IsNotNull(@in, "have stream");
            NUnit.Framework.Assert.AreEqual(type, @in.GetType());
            NUnit.Framework.Assert.AreEqual(data.Length, @in.GetSize());
            byte[] data2 = new byte[data.Length];
            IOUtil.ReadFully(@in, data2, 0, data.Length);
            NUnit.Framework.Assert.IsTrue(Arrays.Equals(data2, data), "same content");
            NUnit.Framework.Assert.AreEqual(-1, @in.Read(), "stream at EOF");
            @in.Close();
        }
        /// <summary>
        /// TODO: Output should be formatted.
        /// </summary>
        public AstVisitAction Visit(FileRedirectionAst redirectionAst)
        {
            string outputPath = GetOutputFileName(redirectionAst);

            if (outputPath == null)
            {
                return(AstVisitAction.SkipChildren);
            }

            FileMode     fileMode = GetFileMode(redirectionAst);
            ObjectStream stream   = GetStream(redirectionAst);
            FileStream   file     = File.Open(outputPath, fileMode, FileAccess.Write);

            using (var streamWriter = new StreamWriter(file, Encoding.Unicode))
            {
                foreach (object obj in stream.Read())
                {
                    WriteObject(streamWriter, obj);
                }
            }
            return(AstVisitAction.SkipChildren);
        }
    static void Main(string[] args)
    {
        TestStruct test = new TestStruct();

        test.Bar = 100;
        test.Foo = 200;
        using (MemoryStream ms = new MemoryStream())
        {
            using (ObjectStream os = new ObjectStream(ms, false))
            {
                os.Write(test);
            }
            ms.Seek(0, SeekOrigin.Begin);
            Console.WriteLine(BitConverter.ToString(ms.ToArray()));
            using (ObjectStream os = new ObjectStream(ms, false))
            {
                TestStruct result = os.Read <TestStruct>();
                Console.WriteLine(result.Bar);
                Console.WriteLine(result.Foo);
            }
        }
        Console.ReadLine();
    }
Example #8
0
        public virtual void TestWhole_LargeObject()
        {
            int type = Constants.OBJ_BLOB;

            byte[]  data = GetRng().NextBytes(streamThreshold + 5);
            RevBlob id   = tr.Blob(data);

            tr.Branch("master").Commit().Add("A", id).Create();
            tr.PackAndPrune();
            NUnit.Framework.Assert.IsTrue(wc.Has(id), "has blob");
            ObjectLoader ol = wc.Open(id);

            NUnit.Framework.Assert.IsNotNull(ol, "created loader");
            NUnit.Framework.Assert.AreEqual(type, ol.GetType());
            NUnit.Framework.Assert.AreEqual(data.Length, ol.GetSize());
            NUnit.Framework.Assert.IsTrue(ol.IsLarge(), "is large");
            try
            {
                ol.GetCachedBytes();
                NUnit.Framework.Assert.Fail("Should have thrown LargeObjectException");
            }
            catch (LargeObjectException tooBig)
            {
                NUnit.Framework.Assert.AreEqual(MessageFormat.Format(JGitText.Get().largeObjectException
                                                                     , id.Name), tooBig.Message);
            }
            ObjectStream @in = ol.OpenStream();

            NUnit.Framework.Assert.IsNotNull(@in, "have stream");
            NUnit.Framework.Assert.AreEqual(type, @in.GetType());
            NUnit.Framework.Assert.AreEqual(data.Length, @in.GetSize());
            byte[] data2 = new byte[data.Length];
            IOUtil.ReadFully(@in, data2, 0, data.Length);
            NUnit.Framework.Assert.IsTrue(Arrays.Equals(data2, data), "same content");
            NUnit.Framework.Assert.AreEqual(-1, @in.Read(), "stream at EOF");
            @in.Close();
        }