Ejemplo n.º 1
0
            /// <exception cref="System.IO.IOException"></exception>
            public override byte[] ToByteArray()
            {
                if (onDiskFile == null)
                {
                    return(base.ToByteArray());
                }
                long len = Length();

                if (int.MaxValue < len)
                {
                    throw new OutOfMemoryException(JGitText.Get().lengthExceedsMaximumArraySize);
                }
                byte[]          @out = new byte[(int)len];
                FileInputStream @in  = new FileInputStream(onDiskFile);

                try
                {
                    IOUtil.ReadFully(@in, @out, 0, (int)len);
                }
                finally
                {
                    @in.Close();
                }
                return(@out);
            }
Ejemplo n.º 2
0
        /// <summary>Read an entire local file into memory as a byte array.</summary>
        /// <remarks>Read an entire local file into memory as a byte array.</remarks>
        /// <param name="path">location of the file to read.</param>
        /// <param name="max">
        /// maximum number of bytes to read, if the file is larger than
        /// this limit an IOException is thrown.
        /// </param>
        /// <returns>complete contents of the requested local file.</returns>
        /// <exception cref="System.IO.FileNotFoundException">the file does not exist.</exception>
        /// <exception cref="System.IO.IOException">the file exists, but its contents cannot be read.
        ///     </exception>
        public static byte[] ReadFully(FilePath path, int max)
        {
            FileInputStream @in = new FileInputStream(path);

            try
            {
                long sz = @in.GetChannel().Size();
                if (sz > max)
                {
                    throw new IOException(MessageFormat.Format(JGitText.Get().fileIsTooLarge, path));
                }
                byte[] buf = new byte[(int)sz];
                IOUtil.ReadFully(@in, buf, 0, buf.Length);
                return(buf);
            }
            finally
            {
                try
                {
                    @in.Close();
                }
                catch (IOException)
                {
                }
            }
        }
        public virtual void TestInCoreInputStream()
        {
            int cnt = 256;

            byte[] test            = new TestRng(Sharpen.Extensions.GetTestName()).NextBytes(cnt);
            TemporaryBuffer.Heap b = new TemporaryBuffer.Heap(cnt + 4);
            b.Write(test);
            b.Close();
            InputStream @in = b.OpenInputStream();

            byte[] act = new byte[cnt];
            IOUtil.ReadFully(@in, act, 0, cnt);
            NUnit.Framework.Assert.IsTrue(Arrays.Equals(test, act));
        }
Ejemplo n.º 4
0
        public virtual void TestInCoreInputStream()
        {
            int cnt = 256;

            byte[] test            = new TestRng(Sharpen.Extensions.GetTestName()).NextBytes(cnt);
            TemporaryBuffer.Heap b = new TemporaryBuffer.Heap(cnt + 4);
            b.Write(test);
            b.Close();
            InputStream @in = b.OpenInputStream();

            byte[] act = new byte[cnt];
            IOUtil.ReadFully(@in, act, 0, cnt);
            for (int i = 0; i < test.Length; i++)
            {
                Assert.AreEqual(test[i], act[i]);
            }
        }
Ejemplo n.º 5
0
 /// <summary>Read an entire local file into memory as a byte array.</summary>
 /// <remarks>Read an entire local file into memory as a byte array.</remarks>
 /// <param name="path">location of the file to read.</param>
 /// <returns>complete contents of the requested local file.</returns>
 /// <exception cref="System.IO.FileNotFoundException">the file does not exist.</exception>
 /// <exception cref="System.IO.IOException">the file exists, but its contents cannot be read.
 ///     </exception>
 public static byte[] ReadFully(FilePath path)
 {
     return(IOUtil.ReadFully(path, int.MaxValue));
 }