GetChannel() public method

public GetChannel ( ) : FileChannel
return FileChannel
Example #1
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)
				{
				}
			}
		}
Example #2
0
		/// <exception cref="System.IO.IOException"></exception>
		public override void VisitFile(FileTreeEntry f)
		{
			FilePath path = new FilePath(GetCurrentDirectory(), f.GetName());
			FileInputStream @in = new FileInputStream(path);
			try
			{
				long sz = @in.GetChannel().Size();
				f.SetId(inserter.Insert(Constants.OBJ_BLOB, sz, @in));
				inserter.Flush();
			}
			finally
			{
				inserter.Release();
				@in.Close();
			}
		}
Example #3
0
 /// <summary>Copy the current file content into the temporary file.</summary>
 /// <remarks>
 /// Copy the current file content into the temporary file.
 /// <p>
 /// This method saves the current file content by inserting it into the
 /// temporary file, so that the caller can safely append rather than replace
 /// the primary file.
 /// <p>
 /// This method does nothing if the current file does not exist, or exists
 /// but is empty.
 /// </remarks>
 /// <exception cref="System.IO.IOException">
 /// the temporary file could not be written, or a read error
 /// occurred while reading from the current file. The lock is
 /// released before throwing the underlying IO exception to the
 /// caller.
 /// </exception>
 /// <exception cref="Sharpen.RuntimeException">
 /// the temporary file could not be written. The lock is released
 /// before throwing the underlying exception to the caller.
 /// </exception>
 public virtual void CopyCurrentContent()
 {
     RequireLock();
     try
     {
         FileInputStream fis = new FileInputStream(@ref);
         try
         {
             if (fsync)
             {
                 FileChannel @in = fis.GetChannel();
                 long pos = 0;
                 long cnt = @in.Size();
                 while (0 < cnt)
                 {
                     long r = os.GetChannel().TransferFrom(@in, pos, cnt);
                     pos += r;
                     cnt -= r;
                 }
             }
             else
             {
                 byte[] buf = new byte[2048];
                 int r;
                 while ((r = fis.Read(buf)) >= 0)
                 {
                     os.Write(buf, 0, r);
                 }
             }
         }
         finally
         {
             fis.Close();
         }
     }
     catch (FileNotFoundException)
     {
     }
     catch (IOException ioe)
     {
         // Don't worry about a file that doesn't exist yet, it
         // conceptually has no current content to copy.
         //
         Unlock();
         throw;
     }
     catch (RuntimeException ioe)
     {
         Unlock();
         throw;
     }
     catch (Error ioe)
     {
         Unlock();
         throw;
     }
 }
Example #4
0
				/// <exception cref="NGit.Errors.MissingObjectException"></exception>
				/// <exception cref="System.IO.IOException"></exception>
				public override ObjectStream OpenStream()
				{
					FileInputStream @in = new FileInputStream(p);
					long sz = @in.GetChannel().Size();
					int type = this.GetType();
					BufferedInputStream b = new BufferedInputStream(@in);
					return new ObjectStream.Filter(type, sz, b);
				}
Example #5
0
 // do nothing
 /// <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];
         int actSz = IOUtil.ReadFully(@in, buf, 0);
         if (actSz == sz)
         {
             byte[] ret = new byte[actSz];
             System.Array.Copy(buf, 0, ret, 0, actSz);
             return ret;
         }
         return buf;
     }
     finally
     {
         try
         {
             @in.Close();
         }
         catch (IOException)
         {
         }
     }
 }