Override entire ByteArrayInputStream class in JS

This commit is contained in:
Marco Castelluccio 2014-09-25 18:14:38 -07:00
Родитель c1bd8db7ec
Коммит aa32876715
1 изменённых файлов: 87 добавлений и 12 удалений

Просмотреть файл

@ -40,18 +40,93 @@ Override["java/lang/Math.min.(II)I"] = function(ctx, stack) {
stack.push(a <= b ? a : b);
}
Override["java/io/ByteArrayInputStream.<init>.([B)V"] = function(ctx, stack) {
var buf = stack.pop(), _this = stack.pop();
if (!buf) {
ctx.raiseExceptionAndYield("java/lang/NullPointerException");
}
_this.buf = buf;
_this.pos = _this.mark = 0;
_this.count = buf.length;
}
Override["java/io/ByteArrayInputStream.<init>.([BII)V"] = function(ctx, stack) {
var length = stack.pop(), offset = stack.pop(), buf = stack.pop(), _this = stack.pop();
if (!buf) {
ctx.raiseExceptionAndYield("java/lang/NullPointerException");
}
_this.buf = buf;
_this.pos = _this.mark = offset;
_this.count = (offset + length <= buf.length) ? (offset + length) : buf.length;
}
Override["java/io/ByteArrayInputStream.read.()I"] = function(ctx, stack) {
var _this = stack.pop();
var pos = _this.class.getField("pos", "I").get(_this);
var count = _this.class.getField("count", "I").get(_this);
var buf = _this.class.getField("buf", "[B").get(_this);
if (pos < count) {
var value = buf[pos++] & 0xFF;
_this.class.getField("pos", "I").set(_this, pos);
stack.push(value);
} else {
stack.push(-1);
}
stack.push((_this.pos < _this.count) ? (_this.buf[_this.pos++] & 0xFF) : -1);
}
Override["java/io/ByteArrayInputStream.read.([BII)I"] = function(ctx, stack) {
var len = stack.pop(), off = stack.pop(), b = stack.pop(), _this = stack.pop();
if (!b) {
ctx.raiseExceptionAndYield("java/lang/NullPointerException");
} else if ((off < 0) || (off > b.length) || (len < 0) ||
((off + len) > b.length) || ((off + len) < 0)) {
ctx.raiseExceptionAndYield("java/lang/IndexOutOfBoundsException");
}
if (_this.pos >= _this.count) {
stack.push(-1);
return;
}
if (_this.pos + len > _this.count) {
len = _this.count - _this.pos;
}
if (len <= 0) {
stack.push(0);
return;
}
b.set(_this.buf.subarray(_this.pos, _this.pos + len), off);
_this.pos += len;
stack.push(len);
}
Override["java/io/ByteArrayInputStream.skip.(J)J"] = function(ctx, stack) {
var nLong = stack.pop2(), _this = stack.pop();
var n = nLong.toNumber();
if (_this.pos + n > _this.count) {
n = _this.count - _this.pos;
}
if (n < 0) {
stack.push2(Long.fromNumber(0));
return;
}
_this.pos += n;
stack.push2(nLong);
}
Override["java/io/ByteArrayInputStream.available.()I"] = function(ctx, stack) {
var _this = stack.pop();
stack.push(_this.count - _this.pos);
}
Override["java/io/ByteArrayInputStream.mark.(I)V"] = function(ctx, stack) {
var readAheadLimit = stack.pop(), _this = stack.pop();
_this.mark = _this.pos;
}
Override["java/io/ByteArrayInputStream.reset.()V"] = function(ctx, stack) {
var _this = stack.pop();
_this.pos = _this.mark;
}