-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
874 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** | ||
* @file Buffer和ArrayBuffer转换 | ||
* @author mengke01([email protected]) | ||
*/ | ||
|
||
/** | ||
* Buffer转换成ArrayBuffer | ||
* | ||
* @param {Buffer} buffer 缓冲数组 | ||
* @return {ArrayBuffer} | ||
*/ | ||
function toArrayBuffer(buffer) { | ||
var length = buffer.length; | ||
var view = new DataView(new ArrayBuffer(length), 0, length); | ||
for (var i = 0, l = length; i < l; i++) { | ||
view.setUint8(i, buffer[i], false); | ||
} | ||
return view.buffer; | ||
} | ||
|
||
/** | ||
* ArrayBuffer转换成Buffer | ||
* | ||
* @param {ArrayBuffer} arrayBuffer 缓冲数组 | ||
* @return {Buffer} | ||
*/ | ||
function toBuffer(arrayBuffer) { | ||
if (Array.isArray(arrayBuffer)) { | ||
return new Buffer(arrayBuffer); | ||
} | ||
else { | ||
var length = arrayBuffer.byteLength; | ||
var view = new DataView(arrayBuffer, 0, length); | ||
var buffer = new Buffer(length); | ||
for (var i = 0, l = length; i < l; i++) { | ||
buffer[i] = view.getUint8(i, false); | ||
} | ||
} | ||
|
||
return buffer; | ||
} | ||
|
||
if (typeof exports !== 'undefined') { | ||
module.exports = { | ||
toArrayBuffer: toArrayBuffer, | ||
toBuffer: toBuffer | ||
}; | ||
} | ||
else { | ||
define( | ||
function (require) { | ||
return { | ||
toArrayBuffer: toArrayBuffer, | ||
toBuffer: toBuffer | ||
}; | ||
} | ||
); | ||
} | ||
|
||
|
Oops, something went wrong.