Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Refactor io read/write methods into io, with readInto/writeInto in io…

…-engine
  • Loading branch information...
commit c333c0758f6e46d392cc67dad2eda6742064e8d7 1 parent 54891d8
Tom Robinson authored
Showing with 78 additions and 62 deletions.
  1. +20 −62 engines/rhino/lib/io-engine.js
  2. +58 −0 lib/io.js
View
82 engines/rhino/lib/io-engine.js
@@ -1,68 +1,35 @@
// IO: Rhino
-var ByteString = require("./binary").ByteString;
-
var IO = exports.IO = function(inputStream, outputStream) {
this.inputStream = inputStream;
this.outputStream = outputStream;
};
-IO.prototype.read = function(length) {
- var readAll = false,
- buffer = null,
- bytes = null,
- total = 0,
- index = 0,
- read = 0;
-
- if (arguments.length == 0) {
- readAll = true;
- }
- if (typeof length !== "number") {
- length = 1024;
- }
+IO.prototype.readInto = function(buffer, length, from) {
+ var bytes = buffer._bytes; // java byte array
+
+ var offset = buffer._offset;
+ if (typeof from === "number")
+ offset += from;
- buffer = java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, length);
+ if (length > bytes.length + offset)
+ throw "FIXME: Buffer too small. Throw or truncate?";
- do {
- read = this.inputStream.read(buffer, index, length - index);
-
- if (read < 0)
+ var total = 0,
+ bytesRead = 0;
+
+ while (total < length) {
+ bytesRead = this.inputStream.read(bytes, offset + total, length - total);
+ if (bytesRead < 0)
break;
-
- if (bytes) {
- bytes.write(buffer, index, read);
- index = 0;
- } else {
- index += read;
- if (index === buffer.length && readAll) {
- bytes = new java.io.ByteArrayOutputStream(length * 2);
- bytes.write(buffer, 0, length);
- index = 0;
- }
- }
- total += read;
-
-
- } while ((readAll || total < length) && read > -1);
-
- var resultBuffer, resultLength;
-
- if (bytes) {
- resultBuffer = bytes.toByteArray();
- } else if (total < buffer.length) {
- resultBuffer = java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, total);
- java.lang.System.arraycopy(buffer, 0, resultBuffer, 0, total);
- } else {
- resultBuffer = buffer;
+ total += bytesRead;
}
-
- resultLength = resultBuffer.length;
-
- if (total != resultLength || total !== resultBuffer.length)
- throw new Error("IO.read sanity check failed: total="+total+" resultLength="+resultLength+" resultBuffer.length="+resultBuffer.length);
- return new ByteString(resultBuffer, 0, resultBuffer.length);
+ return total;
+};
+
+IO.prototype.writeInto = function(buffer, from, to) {
+ this.outputStream.write(buffer._bytes, buffer._offset + from, to - from);
};
IO.prototype.copy = function (output, mode, options) {
@@ -76,15 +43,6 @@ IO.prototype.copy = function (output, mode, options) {
return this;
};
-IO.prototype.write = function(object, charset) {
- if (object === null || object === undefined || typeof object.toByteString !== "function")
- throw new Error("Argument to IO.write must have toByteString() method");
-
- var binary = object.toByteString(charset);
- this.outputStream.write(binary._bytes, binary._offset, binary.length);
- return this;
-};
-
IO.prototype.flush = function() {
this.outputStream.flush();
return this;
View
58 lib/io.js
@@ -2,12 +2,70 @@
var engine = require("io-engine");
+var ByteString = require("./binary").ByteString,
+ ByteArray = require("./binary").ByteArray,
+ B_COPY = require("binary-engine").B_COPY;
+
for (var name in engine) {
if (Object.prototype.hasOwnProperty.call(engine, name)) {
exports[name] = engine[name];
}
};
+exports.IO.prototype.readChunk = function(length) {
+ if (typeof length !== "number") length = 1024;
+
+ var buffer = new ByteArray(length);
+
+ var readLength = this.readInto(buffer, length, 0);
+
+ if (readLength <= 0)
+ return new ByteString();
+
+ return new ByteString(buffer._bytes, 0, readLength);
+};
+
+exports.IO.prototype.read = function(length) {
+ if (length !== undefined)
+ return this.readChunk(length);
+
+ var buffers = [],
+ total = 0;
+
+ while (true) {
+ var buffer = this.readChunk();
+ if (buffer.length > 0) {
+ buffers.push(buffer);
+ total += buffer.length;
+ }
+ else
+ break;
+ }
+
+ var buffer = new ByteArray(total),
+ dest = buffer._bytes,
+ copied = 0;
+
+ for (var i = 0; i < buffers.length; i++) {
+ var b = buffers[i],
+ len = b.length;
+ B_COPY(b._bytes, b._offset, dest, copied, len);
+ copied += len;
+ }
+
+ return new ByteString(dest, 0, copied);
+};
+
+exports.IO.prototype.write = function(object, charset) {
+ if (object === null || object === undefined || typeof object.toByteString !== "function")
+ throw new Error("Argument to IO.write must have toByteString() method");
+
+ var binary = object.toByteString(charset);
+ this.writeInto(binary, 0, binary.length);
+
+ return this;
+};
+
exports.IO.prototype.puts = function() {
this.write(arguments.length === 0 ? "\n" : Array.prototype.join.apply(arguments, ["\n"]) + "\n");
}
Please sign in to comment.
Something went wrong with that request. Please try again.