From aa32876715da8e7f148e057d0bf1d9d5af5b6f84 Mon Sep 17 00:00:00 2001 From: Marco Castelluccio Date: Thu, 25 Sep 2014 18:14:38 -0700 Subject: [PATCH] Override entire ByteArrayInputStream class in JS --- override.js | 99 ++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 87 insertions(+), 12 deletions(-) diff --git a/override.js b/override.js index 0a878f77..6b8b5edc 100644 --- a/override.js +++ b/override.js @@ -40,18 +40,93 @@ Override["java/lang/Math.min.(II)I"] = function(ctx, stack) { stack.push(a <= b ? a : b); } +Override["java/io/ByteArrayInputStream..([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..([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; }