zoukankan      html  css  js  c++  java
  • StreamHelper

     public static MemoryStream CreateMemoryStreamFromBytes(byte[] inputData)
            {
                if (inputData == null || inputData.Length == 0)
                {
                    return null;
                }
                MemoryStream result = new MemoryStream(inputData, false);
                return result;
            }
    
            /// <summary>
            /// Read the data based on byte array from a specific file which path is from input.
            /// </summary>
            /// <param name="localFilePath">File path you want read.</param>
            /// <returns>The file data based on byte array of input file. If input file path not exists, return null.</returns>
            public static byte[] ReadBytesFromFile(string localFilePath)
            {
                if (StringHelper.IsNullOrEmpty(localFilePath)
                    || !File.Exists(localFilePath))
                {
                    return null;
                }
                FileStream fs = new FileStream(localFilePath,
                    FileMode.Open, FileAccess.Read, FileShare.Read);
                byte[] result = new byte[fs.Length];
                int bufferSize = Convert.ToInt32((long)65536 > fs.Length ? fs.Length : (long)65536);
                int offset = 0;
                int readCount = 0;
                using (fs as IDisposable)
                {
                    while ((readCount = fs.Read(result, offset, bufferSize)) > 0)
                    {
                        offset += readCount;
                        bufferSize = bufferSize > result.Length - offset ?
                            result.Length - offset : bufferSize;
                    }
                }
                return result;
            }
    

      

  • 相关阅读:
    日志处理
    md5加密
    os 模块
    time模块
    函数的进阶
    参数 返回值
    文件操作
    集合 拷贝
    linux如何更快的远程拷贝?scp,tar,rsync?
    修改内核临时端口范围
  • 原文地址:https://www.cnblogs.com/Wolfmanlq/p/4556724.html
Copyright © 2011-2022 走看看