【问题标题】:Byte[] Split Up Into 1022 Length Sections Then Restored Not Matching UpByte[] 拆分为 1022 个长度的部分,然后恢复不匹配
【发布时间】:2015-03-17 21:21:09
【问题描述】:

我正在尝试获取 PDF 文档并通过 MVC 网站将其上传以存储到 SAP 结构中。 SAP 结构要求将字节数组分成 1022 个长度的部分。该程序似乎运行良好,直到我尝试从 SAP 中查看 PDF 文档。不幸的是,由于访问权限,我无法查看存储在 SAP 中的 PDF 数据。因此,我创建了一种 MOCK 程序来匹配发送到 SAP(fileContent)之前的字节数组,然后匹配从 SAP 返回后的样子(fileContentPostSAP)。

程序比较字节数组并在数组位置 1022 找到不匹配的值。

我的程序中是否存在导致字节数组不匹配的错误?它们应该完全匹配,对吧?

ClaimsIdentityMgr claimIdentityMgr = new ClaimsIdentityMgr();
ClaimsIdentity currentClaimsIdentity = claimIdentityMgr.GetCurrentClaimsIdentity();
var subPath = "~/App_Data/" + currentClaimsIdentity.EmailAddress;
var destinationPath = Path.Combine(Server.MapPath(subPath), "LG WM3455H Spec Sheet.pdf");
byte[] fileContent = System.IO.File.ReadAllBytes(destinationPath);


//pretend this is going to SAP
var arrList = SAPServiceRequestRepository.CreateByteListForStructure(fileContent);
var mockStructureList = new List<byte[]>();
foreach (byte[] b in arrList)
     mockStructureList.Add(b);

//now get it back from Mock SAP
var fileContentPostSAP = new byte[fileContent.Count()];
var rowCounter = 0;
var prevLength = 0;
foreach (var item in mockStructureList)
{
    if (rowCounter == 0)
        System.Buffer.BlockCopy(item, 0, fileContentPostSAP, 0, item.Length);
    else
        System.Buffer.BlockCopy(item, 0, fileContentPostSAP, prevLength, item.Length);
    rowCounter++;
    prevLength = item.Length;
}

//compare the orginal array with the new one
var areEqual = (fileContent == fileContentPostSAP);
for (var i = 0; i < fileContent.Length; i++)
{
    if (fileContent[i] != fileContentPostSAP[i])
        throw new Exception("i = " + i + " | fileContent[i] = " + fileContent[i] + " | fileContentPostSAP[i] = " + fileContentPostSAP[i]);
}

这里是 CreateByteListForStructure 函数:

public static List<byte[]> CreateByteListForStructure(byte[] fileContent)
{
    var returnList = new List<byte[]>();

    for (var i = 0; i < fileContent.Length; i += 1022)
    {
        if (fileContent.Length - i >= 1022)
        {
            var localByteArray = new byte[1022];
            System.Buffer.BlockCopy(fileContent, i, localByteArray, 0, 1022);
            returnList.Add(localByteArray);
        }
        else
        {
            var localByteArray = new byte[fileContent.Length - i];
            System.Buffer.BlockCopy(fileContent, i, localByteArray, 0, fileContent.Length - i);
            returnList.Add(localByteArray);
        }
    }

    return returnList;
}

【问题讨论】:

  • 不应该是这个:prevLength = item.Length; 是这个吗? prevLength += item.Length;(注意在= 运算符之前添加了+ 符号)。在我看来,好像您一遍又一遍地覆盖目标数组的开头。
  • 这是错误。您如何看待将其添加为答案而不是评论以便我可以标记它?
  • 发布了更完整的答案。

标签: c# arrays pdf sap


【解决方案1】:

代码中似乎有一个简单的错误。

这个循环,从块中重构数组的内容:

var prevLength = 0;
foreach (var item in mockStructureList)
{
    if (rowCounter == 0)
        System.Buffer.BlockCopy(item, 0, fileContentPostSAP, 0, item.Length);
    else
        System.Buffer.BlockCopy(item, 0, fileContentPostSAP, prevLength, item.Length);
    rowCounter++;
    prevLength = item.Length;
}

根据blocks的描述,每个block都是1022字节,也就是说第一次迭代后prevLength被设置为1022,但下一次迭代后又被设置为1022 .

prevLength 更正确的分配是这样的:

prevLength += item.Length;
           ^
           |
           +-- added this

这将正确地将输出数组中的指针一次向前移动一个块,而不是将其移动到第二块然后将其留在那里。

基本上,您将块 0 写入正确的位置,但所有其他块都在块 1 的顶部,将块 2 及以后的块留在输出数组中为零。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-13
    • 2021-11-11
    • 2021-08-17
    • 1970-01-01
    • 1970-01-01
    • 2021-09-10
    • 2015-10-05
    相关资源
    最近更新 更多