Updated example.

This commit is contained in:
Alan Kligman 2012-10-15 11:30:45 -04:00
parent fcb66dac52
commit 238b37c467
3 changed files with 141 additions and 49 deletions

1
.gitignore vendored
View File

@ -1 +0,0 @@
filesystemapi.js

View File

@ -18,12 +18,24 @@ require.config({
require(["src/buffer"], function(Buffer) {
try {
/*
var string1 = "\u00BD + \u00BC";
var string2 = " = \u00BE";
var buffer1 = new Buffer(string1);
var buffer2 = new Buffer(string2);
console.log(buffer1.toString(), ";", buffer2.toString());
console.log(Buffer.concat([buffer1, buffer2]).toString());
var buffer3 = Buffer.concat([buffer1, buffer2]);
console.log(buffer3.toString());
var buffer4 = buffer3.slice(0, 7);
console.log(buffer4.toString());
*/
var buffer = new Buffer(4);
buffer.writeUInt32LE(0xDEADBEEF, 0);
console.log(0xDEADBEEF | 0x0);
console.log(buffer.readUInt32LE(0));
buffer.writeUInt32BE(0xDEADBEEF, 0);
console.log(buffer.readUInt32BE(0));
// console.log(buffer.readUInt16LE(0));
} catch(e) {
console.log(e);
}

View File

@ -11,47 +11,20 @@ Redistribution and use in source and binary forms, with or without modification,
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
define(function(require) {
;(function(window, undefined) {
'use strict';
/** Detect free variable `exports` */
var freeExports = (typeof exports == 'object') && exports &&
(typeof global == 'object' && global && global == global.global && (window = global)) ? exports : undefined;
/**
* http://closure-library.googlecode.com/svn/docs/closure_goog_crypt_crypt.js.source.html
* Turns a string into an array of bytes; a "byte" being a JS number in the
* range 0-255.
* @param {string} str String value to arrify.
* @return {Array.<number>} Array of numbers corresponding to the
* UCS character codes of each character in str.
*/
var stringToByteArray = function(string, buffer) {
var p = 0;
for (var i = 0; i < string.length; i++) {
var c = string.charCodeAt(i);
while (c > 0xff) {
buffer[p++] = c & 0xff;
c >>= 8;
}
buffer[p++] = c;
}
return buffer;
};
/**
* http://closure-library.googlecode.com/svn/docs/closure_goog_crypt_crypt.js.source.html
* Turns an array of numbers into the string given by the concatenation of the
* characters to which the numbers correspond.
* @param {Array} array Array of numbers representing characters.
* @return {string} Stringification of the array.
*/
var byteArrayToString = function(array) {
return String.fromCharCode.apply(null, array);
};
/**
* http://closure-library.googlecode.com/svn/docs/closure_goog_crypt_crypt.js.source.html
* [source]http://closure-library.googlecode.com/svn/docs/closure_goog_crypt_crypt.js.source.html
* Converts a JS string to a UTF-8 "byte" array.
* @param {string} str 16-bit unicode string.
* @return {Array.<number>} UTF-8 byte array.
*/
stringToUtf8ByteArray = function(string, offset, length, buffer) {
function stringToUtf8ByteArray(string, offset, length, buffer) {
// TODO(user): Use native implementations if/when available
string = string.replace(/\r\n/g, '\n');
var p = 0;
@ -73,12 +46,12 @@ define(function(require) {
/**
* http://closure-library.googlecode.com/svn/docs/closure_goog_crypt_crypt.js.source.html
* [source]http://closure-library.googlecode.com/svn/docs/closure_goog_crypt_crypt.js.source.html
* Converts a UTF-8 byte array to JavaScript's 16-bit Unicode.
* @param {Array.<number>} bytes UTF-8 byte array.
* @return {string} 16-bit Unicode string.
*/
utf8ByteArrayToString = function(bytes) {
function utf8ByteArrayToString(bytes) {
// TODO(user): Use native implementations if/when available
var out = [], pos = 0, c = 0;
while (pos < bytes.length) {
@ -110,12 +83,17 @@ define(function(require) {
object instanceof Float64Array;
}
function BufferOverflowError(){ Error.apply(this, arguments); }
BufferOverflowError.prototype = new Error();
BufferOverflowError.prototype.name = "BufferOverflowError";
BufferOverflowError.prototype.constructor = BufferOverflowError;
function Buffer(arg, optArg) {
this.__buffer__;
if(Array.isArray(arg) || isTypedArray(arg)) {
this.__buffer__ = new Uint8Array(arg);
} else if("string" === typeof arg) {
// FIXME: parse optArg to see if we should use non-default encoding
// FIXME: support other encodings
this.__buffer__ = new Uint8Array(Buffer.byteLength(arg));
this.write(arg);
} else if("number" === typeof arg) {
@ -128,6 +106,7 @@ define(function(require) {
return object instanceof Buffer;
};
Buffer.byteLength = function byteLength(string, optEncoding) {
// FIXME: support other encodings
return unescape(encodeURIComponent(string)).length;
};
Buffer.concat = function concat(list, optTotalLength) {
@ -147,22 +126,17 @@ define(function(require) {
}
return target;
};
Buffer.prototype.get = function get(offset) {
return this.__buffer__[offset];
};
Buffer.prototype.set = function set(offset, value) {
this.__buffer__[offset] = value;
return value;
};
Buffer.prototype.length = function length() {
return this.__buffer__.length;
};
Buffer.prototype.write = function write(string, optOffset, optLength, optEncoding) {
// FIXME: support other encodings
optOffset = (undefined === optOffset) ? 0 : optOffset;
optLength = (undefined === optLength) ? Buffer.byteLength(string) : optLength;
stringToUtf8ByteArray(string, optOffset, optLength, this.__buffer__);
};
Buffer.prototype.toString = function toString(optEncoding, optStart, optEnd) {
// FIXME: support other encodings
optStart = (undefined === optStart) ? 0 : optStart;
optEnd = (undefined === optEnd) ? this.__buffer__.length : optEnd;
var source;
@ -189,8 +163,115 @@ define(function(require) {
Buffer.prototype.slice = function slice(optStart, optEnd) {
var bufferSlice = new Buffer();
bufferSlice.__buffer__ = this.__buffer__.subarray(optStart, optEnd);
return bufferSlice;
};
Buffer.prototype.readUInt8 = function readUInt8(offset, optNoAssert) {
if(!optNoAssert && offset > this.__buffer__.length - 1) {
throw new BufferOverflowError();
}
return this.__buffer__[offset];
};
Buffer.prototype.writeUInt8 = function writeUInt8(value, offset, optNoAssert) {
if(!optNoAssert && offset > this.__buffer__.length - 1) {
throw new BufferOverflowError();
}
this.__buffer__[offset] = value;
};
Buffer.prototype.readUInt16BE = function readUInt16BE(offset, optNoAssert) {
if(!optNoAssert && offset > this.__buffer__.length - 2) {
throw new BufferOverflowError();
}
var buffer = this.__buffer__;
return buffer[offset]<<8 | buffer[offset+1];
};
Buffer.prototype.readUInt16LE = function readUInt16LE(offset, optNoAssert) {
if(!optNoAssert && offset > this.__buffer__.length - 2) {
throw new BufferOverflowError();
}
var buffer = this.__buffer__;
return buffer[offset+1]<<8 | buffer[offset];
};
Buffer.prototype.writeUInt16BE = function writeUInt16BE(value, offset, optNoAssert) {
if(!optNoAssert && offset > this.__buffer__.length - 2) {
throw new BufferOverflowError();
}
var buffer = this.__buffer__;
buffer[offset] = value>>8 & 0xFF;
buffer[offset+1] = value & 0xFF;
};
Buffer.prototype.writeUInt16LE = function writeUInt16LE(value, offset, optNoAssert) {
if(!optNoAssert && offset > this.__buffer__.length - 2) {
throw new BufferOverflowError();
}
var buffer = this.__buffer__;
buffer[offset] = value & 0xFF;
buffer[offset+1] = value>>8 & 0xFF;
};
Buffer.prototype.readUInt32BE = function readUInt32BE(offset, optNoAssert) {
if(!optNoAssert && offset > this.__buffer__.length - 4) {
throw new BufferOverflowError();
}
var buffer = this.__buffer__;
return buffer[offset]<<24 | buffer[offset+1]<<16 | buffer[offset+2]<<8 | buffer[offset+3];
};
Buffer.prototype.readUInt32LE = function readUInt32LE(offset, optNoAssert) {
if(!optNoAssert && offset > this.__buffer__.length - 4) {
throw new BufferOverflowError();
}
var buffer = this.__buffer__;
return buffer[offset+3]<<24 | buffer[offset+2]<<16 | buffer[offset+1]<<8 | buffer[offset];
};
Buffer.prototype.writeUInt32BE = function writeUInt32BE(value, offset, optNoAssert) {
if(!optNoAssert && offset > this.__buffer__.length - 4) {
throw new BufferOverflowError();
}
var buffer = this.__buffer__;
buffer[offset] = value>>24 & 0xFF;
buffer[offset+1] = value>>16 & 0xFF;
buffer[offset+2] = value>>8 & 0xFF;
buffer[offset+3] = value & 0xFF;
};
Buffer.prototype.writeUInt32LE = function writeUInt32LE(value, offset, optNoAssert) {
if(!optNoAssert && offset > this.__buffer__.length - 4) {
throw new BufferOverflowError();
}
var buffer = this.__buffer__;
buffer[offset] = value & 0xFF;
buffer[offset+1] = value>>8 & 0xFF;
buffer[offset+2] = value>>16 & 0xFF;
buffer[offset+3] = value>>24 & 0xFF;
};
return Buffer;
/*--------------------------------------------------------------------------*/
// expose the API (borrowed from Lodash: https://raw.github.com/bestiejs/lodash/v0.6.1/lodash.js)
// some AMD build optimizers, like r.js, check for specific condition patterns like the following:
if (typeof define == 'function' && typeof define.amd == 'object' && define.amd) {
// Expose api to the global object even when an AMD loader is present in
// case Lo-Dash was injected by a third-party script and not intended to be
// loaded as a module. The global assignment can be reverted in the Lo-Dash
// module via its `noConflict()` method.
window.B = Buffer;
// define as an anonymous module so, through path mapping, it can be
// referenced as the "underscore" module
define(function() {
return Buffer;
});
}
// check for `exports` after `define` in case a build optimizer adds an `exports` object
else if (freeExports) {
// in Node.js or RingoJS v0.8.0+
if (typeof module == 'object' && module && module.exports == freeExports) {
(module.exports = api).B = Buffer;
}
// in Narwhal or RingoJS v0.7.0-
else {
freeExports.B = Buffer;
}
}
else {
// in a browser or Rhino
window.B = Buffer;
}
}(this));