/// <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); }
/// <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)); }
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]); } }
/// <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)); }