/// <summary> /// 增量下载文件 /// </summary> /// <param name="storageNode">GetStorageNode方法返回的存储节点</param> /// <param name="fileName">文件名</param> /// <param name="offset">从文件起始点的偏移量</param> /// <param name="length">要读取的字节数</param> /// <returns>文件内容</returns> public static byte[] DownloadFile(StorageNode storageNode, string fileName, long offset, long length) { fileName = GetFileName(storageNode.GroupName, fileName); using (var storageReqeust = DOWNLOAD_FILE.CreateRequest(storageNode.EndPoint, offset, length, storageNode.GroupName, fileName)) { var storageResponse = new DOWNLOAD_FILE.Response(storageReqeust.GetStorageResponse()); return(storageResponse.Content); } }
/// <summary> /// 异步下载 /// </summary> /// <param name="storageNode"></param> /// <param name="fileName"></param> /// <returns></returns> public static async Task <byte[]> DownloadFileAsync(StorageNode storageNode, string fileName) { fileName = GetFileName(storageNode.GroupName, fileName); using (var storageReqeust = DOWNLOAD_FILE.CreateRequest(storageNode.EndPoint, 0L, 0L, storageNode.GroupName, fileName)) { Task <byte[]> task = null; // storageReqeust.ReadBytesAsync(); await task; return(task.Result); } }
public override FDFSRequest GetRequest(params object[] paramList) { if (paramList.Length != 4) { throw new FDFSException("param count is wrong"); } var storageNode = (StorageNode)paramList[0]; var fileName = (string)paramList[1]; var offsetInfo = (Tuple <long, long>)paramList[2]; var downloadFile = new DOWNLOAD_FILE { ConnectionType = FDFSConnectionType.StorageConnection, EndPoint = storageNode.EndPoint, Callback = (IDownloadCallback)paramList[3] }; var groupNameByteCount = Util.StringByteCount(storageNode.GroupName); if (groupNameByteCount > 16) { throw new FDFSException("groupName is too long"); } var fileNameByteCount = Util.StringByteCount(fileName); int length = 16 + 16 + fileNameByteCount; downloadFile.SetBodyBuffer(length); int offset = 0; Util.LongToBuffer(offsetInfo.Item1, downloadFile.BodyBuffer, offset); offset += 8; Util.LongToBuffer(offsetInfo.Item2, downloadFile.BodyBuffer, offset); offset += 8; Util.StringToByte(storageNode.GroupName, downloadFile.BodyBuffer, offset, groupNameByteCount); if (groupNameByteCount < 16) { for (var i = offset + groupNameByteCount; i < offset + 16; i++) { downloadFile.BodyBuffer[i] = 0; } } offset += 16; Util.StringToByte(fileName, downloadFile.BodyBuffer, offset, fileNameByteCount); downloadFile.Header = new FDFSHeader(length, FDFSConstants.STORAGE_PROTO_CMD_DOWNLOAD_FILE, 0); return(downloadFile); }
/// <summary> /// 下载文件 /// </summary> /// <param name="storageNode">GetStorageNode方法返回的存储节点</param> /// <param name="fileName">文件名</param> /// <exception cref="System.Exception">读取网络资源异常</exception> /// <returns>文件内容</returns> public static byte[] DownloadFile(StorageNode storageNode, string fileName) { fileName = GetFileName(storageNode.GroupName, fileName); using (var storageReqeust = DOWNLOAD_FILE.CreateRequest(storageNode.EndPoint, 0L, 0L, storageNode.GroupName, fileName)) { // 同步下载 var storageResponse = new DOWNLOAD_FILE.Response(storageReqeust.GetStorageResponse()); if (storageResponse.Content == null) { throw new System.Exception(storageReqeust.Message); } return(storageResponse.Content); } }
/// <summary> /// /// </summary> /// <param name="paramList"> /// 1,IPEndPoint IPEndPoint-->the storage IPEndPoint /// 2,long offset-->file offset /// 3,long byteSize -->download file bytes /// 4,string groupName /// 5,string fileName /// </param> /// <returns></returns> public override FDFSRequest GetRequest(params object[] paramList) { if (paramList.Length != 5) { throw new FDFSException("param count is wrong"); } IPEndPoint endPoint = (IPEndPoint)paramList[0]; long offset = (long)paramList[1]; long byteSize = (long)paramList[2]; string groupName = (string)paramList[3]; string fileName = (string)paramList[4]; DOWNLOAD_FILE result = new DOWNLOAD_FILE(); result.Connection = ConnectionManager.GetStorageConnection(endPoint); if (groupName.Length > Consts.FDFS_GROUP_NAME_MAX_LEN) { throw new FDFSException("groupName is too long"); } long length = Consts.FDFS_PROTO_PKG_LEN_SIZE + Consts.FDFS_PROTO_PKG_LEN_SIZE + Consts.FDFS_GROUP_NAME_MAX_LEN + fileName.Length; byte[] bodyBuffer = new byte[length]; byte[] offsetBuffer = Util.LongToBuffer(offset); byte[] byteSizeBuffer = Util.LongToBuffer(byteSize); byte[] groupNameBuffer = Util.StringToByte(groupName); byte[] fileNameBuffer = Util.StringToByte(fileName); Array.Copy(offsetBuffer, 0, bodyBuffer, 0, offsetBuffer.Length); Array.Copy(byteSizeBuffer, 0, bodyBuffer, Consts.FDFS_PROTO_PKG_LEN_SIZE, byteSizeBuffer.Length); Array.Copy(groupNameBuffer, 0, bodyBuffer, Consts.FDFS_PROTO_PKG_LEN_SIZE + Consts.FDFS_PROTO_PKG_LEN_SIZE, groupNameBuffer.Length); Array.Copy(fileNameBuffer, 0, bodyBuffer, Consts.FDFS_PROTO_PKG_LEN_SIZE + Consts.FDFS_PROTO_PKG_LEN_SIZE + Consts.FDFS_GROUP_NAME_MAX_LEN, fileNameBuffer.Length); result.Body = bodyBuffer; result.Header = new FDFSHeader(length, Consts.STORAGE_PROTO_CMD_DOWNLOAD_FILE, 0); return(result); }
/// <summary> /// /// </summary> /// <param name="paramList"> /// 1,IPEndPoint IPEndPoint-->the storage IPEndPoint /// 2,long offset-->file offset /// 3,long byteSize -->download file bytes /// 4,string groupName /// 5,string fileName /// </param> /// <returns></returns> public override FDFSRequest GetRequest(params object[] paramList) { if (paramList.Length != 5) throw new FDFSException("param count is wrong"); IPEndPoint endPoint = (IPEndPoint)paramList[0]; long offset = (long)paramList[1]; long byteSize = (long)paramList[2]; string groupName = (string)paramList[3]; string fileName = (string)paramList[4]; DOWNLOAD_FILE result = new DOWNLOAD_FILE(); result.Connection = ConnectionManager.GetStorageConnection(endPoint); if (groupName.Length > Consts.FDFS_GROUP_NAME_MAX_LEN) throw new FDFSException("groupName is too long"); long length = Consts.FDFS_PROTO_PKG_LEN_SIZE + Consts.FDFS_PROTO_PKG_LEN_SIZE + Consts.FDFS_GROUP_NAME_MAX_LEN + fileName.Length; byte[] bodyBuffer = new byte[length]; byte[] offsetBuffer = Util.LongToBuffer(offset); byte[] byteSizeBuffer = Util.LongToBuffer(byteSize); byte[] groupNameBuffer = Util.StringToByte(groupName); byte[] fileNameBuffer = Util.StringToByte(fileName); Array.Copy(offsetBuffer, 0, bodyBuffer, 0, offsetBuffer.Length); Array.Copy(byteSizeBuffer, 0, bodyBuffer, Consts.FDFS_PROTO_PKG_LEN_SIZE, byteSizeBuffer.Length); Array.Copy(groupNameBuffer, 0, bodyBuffer, Consts.FDFS_PROTO_PKG_LEN_SIZE + Consts.FDFS_PROTO_PKG_LEN_SIZE, groupNameBuffer.Length); Array.Copy(fileNameBuffer, 0, bodyBuffer, Consts.FDFS_PROTO_PKG_LEN_SIZE + Consts.FDFS_PROTO_PKG_LEN_SIZE + Consts.FDFS_GROUP_NAME_MAX_LEN, fileNameBuffer.Length); result.Body = bodyBuffer; result.Header = new FDFSHeader(length, Consts.STORAGE_PROTO_CMD_DOWNLOAD_FILE, 0); return result; }