buf.writeInt32LE(value[, offset])有写2G入上限吗?
发布于 5 年前 作者 LeavesSky 4101 次浏览 来自 问答

QQ图片20200116094616.png 经计算,超过2048MB就不行了(多次尝试依旧如此);

4 回复
  1. 无源代码
  2. 错误信息很明显

@waitingsong 感谢!! 明白是超出了, 但是不了解V8的配置, 不知道如何解决, 上下文: 构建一个自定义结构的文件出来

function buildFile(filePath, zoomArr, xyArr) {
  fs.readFile(filePath, (err, data) => {
    if (err) {
      console.log('err :', err);
    }
    let count = 0;
    for (let a = 0; a < zoomArr.length; a++) {
      for (let b = 0; b < xyArr[a].lenXArr.length; b++) {
        for (let c = 0; c < xyArr[a].lenYArr.length; c++) {
          count++;
        }
      }
    }
    let bufTempLen = count * 8;
    let bufTemp = Buffer.allocUnsafe(bufTempLen);
    let offset = data.length;
    let buf = Buffer.concat([data, bufTemp], offset + bufTempLen);
    let position = buf.length;
    let pngBufferArr = [];
    let pngBufferLengthSize = 0;
    for (let n = 0; n < zoomArr.length; n++) {
      for (let x = 0; x < xyArr[n].lenXArr.length; x++) {
        for (let y = 0; y < xyArr[n].lenYArr.length; y++) {
          try {
            let pngPath = path.resolve(__dirname, 'download', String(zoomArr[n]), String(xyArr[n].lenXArr[x]), String(xyArr[n].lenYArr[y]) + '.png');
            //console.log(pngPath);
            let pngBuffer = fs.readFileSync(pngPath)
            pngBufferArr.push(pngBuffer);
            let pngSize = pngBuffer.length;
            if (pngSize < 1) {
              console.log('pngSize:',n, x, y);
			  console.log(position, pngPath);
              process.exit(0);
            }
            pngBufferLengthSize += pngSize;
            buf.writeInt32LE(position, offset);
            offset += 4;
            position += pngSize;
            buf.writeInt32LE(pngSize, offset);
            offset += 4;
          } catch (err) {
            if (err) {
			  console.log(err,'\n')
              console.log('err',n, x, y);
              process.exit(0);
            }
          }
        }
      }
    }
    let mapBuffer = Buffer.concat([buf, ...pngBufferArr], buf.length + pngBufferLengthSize);
    fs.writeFile(DZDT_FILE_PATH ? DZDT_FILE_PATH : './newMap.dzdt', mapBuffer, err => {
      if (err) {
        console.log('err :', err);
      }
    })
  })
}

@waitingsong 官方源码 : (做了限制)

const validateInt32 = hideStackFrames(
  (value, name, min = -2147483648, max = 2147483647) => {
    // The defaults for min and max correspond to the limits of 32-bit integers.
    if (!isInt32(value)) {
      if (typeof value !== 'number') {
        throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
      }
      if (!NumberIsInteger(value)) {
        throw new ERR_OUT_OF_RANGE(name, 'an integer', value);
      }
      throw new ERR_OUT_OF_RANGE(name, `>= ${min} && <= ${max}`, value);
    }
    if (value < min || value > max) {
      throw new ERR_OUT_OF_RANGE(name, `>= ${min} && <= ${max}`, value);
    }
  }
);

Link

@waitingsong 再次感谢, 我知道怎么改了, 过年好好补一下计算机基础(非科班,自学)

回到顶部