/*
* Copyright 2014 Red Hat, Inc.
*
* Red Hat licenses this file to you under the Apache License, version 2.0
* (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
/** @module vertx-js/write_stream */
var utils = require('vertx-js/util/utils');
var StreamBase = require('vertx-js/stream_base');
var Future = require('vertx-js/future');
var Promise = require('vertx-js/promise');
var io = Packages.io;
var JsonObject = io.vertx.core.json.JsonObject;
var JsonArray = io.vertx.core.json.JsonArray;
var JWriteStream = Java.type('io.vertx.core.streams.WriteStream');
/**
Represents a stream of data that can be written to.
<p>
@class
*/
var WriteStream = function(j_val, j_arg_0) {
var j_writeStream = j_val;
var that = this;
var j_T = typeof j_arg_0 !== 'undefined' ? j_arg_0 : utils.unknown_jtype;StreamBase.call(this, j_val);
var __super_exceptionHandler = this.exceptionHandler;
var __super_write = this.write;
var __super_end = this.end;
var __super_end = this.end;
var __super_setWriteQueueMaxSize = this.setWriteQueueMaxSize;
var __super_writeQueueFull = this.writeQueueFull;
var __super_drainHandler = this.drainHandler;
/**
Set an exception handler on the write stream.
@public
@param handler {function} the exception handler
@return {WriteStream} a reference to this, so the API can be used fluently
*/
this.exceptionHandler = function(handler) {
var __args = arguments;
if (__args.length === 1 && (typeof __args[0] === 'function' || __args[0] == null)) {
j_writeStream["exceptionHandler(io.vertx.core.Handler)"](handler == null ? null : function(jVal) {
handler(utils.convReturnThrowable(jVal));
}) ;
return that;
} else if (typeof __super_exceptionHandler != 'undefined') {
return __super_exceptionHandler.apply(this, __args);
}
else throw new TypeError('function invoked with invalid arguments');
};
/**
Same as but with an <code>handler</code> called when the operation completes
@public
@param data {Object}
@param handler {function}
*/
this.write = function(data, handler) {
var __args = arguments;
if (__args.length === 2 && j_T.accept(__args[0]) && typeof __args[1] === 'function') {
j_writeStream["write(java.lang.Object,io.vertx.core.Handler)"](j_T.unwrap(data), function(ar) {
if (ar.succeeded()) {
handler(null, null);
} else {
handler(null, ar.cause());
}
});
} else if (__args.length === 1 && j_T.accept(__args[0])) {
var __prom = Promise.promise();
var __prom_completer_handler = function (result, cause) { if (cause === null) { __prom.complete(result); } else { __prom.fail(cause); } };
j_writeStream["write(java.lang.Object,io.vertx.core.Handler)"](j_T.unwrap(data), function(ar) {
if (ar.succeeded()) {
__prom_completer_handler(null, null);
} else {
__prom_completer_handler(null, ar.cause());
}
});
return __prom.future();
} else if (typeof __super_write != 'undefined') {
return __super_write.apply(this, __args);
}
else throw new TypeError('function invoked with invalid arguments');
};
/**
Same as but with an <code>handler</code> called when the operation completes
@public
@param data {Object}
@param handler {function}
*/
this.end = function() {
var __args = arguments;
if (__args.length === 1 && typeof __args[0] === 'function') {
j_writeStream["end(io.vertx.core.Handler)"](function(ar) {
if (ar.succeeded()) {
__args[0](null, null);
} else {
__args[0](null, ar.cause());
}
});
} else if (__args.length === 0) {
var __prom = Promise.promise();
var __prom_completer_handler = function (result, cause) { if (cause === null) { __prom.complete(result); } else { __prom.fail(cause); } };
j_writeStream["end(io.vertx.core.Handler)"](function(ar) {
if (ar.succeeded()) {
__prom_completer_handler(null, null);
} else {
__prom_completer_handler(null, ar.cause());
}
});
return __prom.future();
} else if (__args.length === 2 && j_T.accept(__args[0]) && typeof __args[1] === 'function') {
j_writeStream["end(java.lang.Object,io.vertx.core.Handler)"](j_T.unwrap(__args[0]), function(ar) {
if (ar.succeeded()) {
__args[1](null, null);
} else {
__args[1](null, ar.cause());
}
});
} else if (__args.length === 1 && j_T.accept(__args[0])) {
var __prom = Promise.promise();
var __prom_completer_handler = function (result, cause) { if (cause === null) { __prom.complete(result); } else { __prom.fail(cause); } };
j_writeStream["end(java.lang.Object,io.vertx.core.Handler)"](j_T.unwrap(__args[0]), function(ar) {
if (ar.succeeded()) {
__prom_completer_handler(null, null);
} else {
__prom_completer_handler(null, ar.cause());
}
});
return __prom.future();
} else if (typeof __super_end != 'undefined') {
return __super_end.apply(this, __args);
}
else throw new TypeError('function invoked with invalid arguments');
};
/**
Set the maximum size of the write queue to <code>maxSize</code>. You will still be able to write to the stream even
if there is more than <code>maxSize</code> items in the write queue. This is used as an indicator by classes such as
<code>Pump</code> to provide flow control.
<p/>
The value is defined by the implementation of the stream, e.g in bytes for a
{@link NetSocket}, the number of {@link Message} for a
{@link MessageProducer}, etc...
@public
@param maxSize {number} the max size of the write stream
@return {WriteStream} a reference to this, so the API can be used fluently
*/
this.setWriteQueueMaxSize = function(maxSize) {
var __args = arguments;
if (__args.length === 1 && typeof __args[0] ==='number') {
j_writeStream["setWriteQueueMaxSize(int)"](maxSize) ;
return that;
} else if (typeof __super_setWriteQueueMaxSize != 'undefined') {
return __super_setWriteQueueMaxSize.apply(this, __args);
}
else throw new TypeError('function invoked with invalid arguments');
};
/**
This will return <code>true</code> if there are more bytes in the write queue than the value set using {@link WriteStream#setWriteQueueMaxSize}
@public
@return {boolean} true if write queue is full
*/
this.writeQueueFull = function() {
var __args = arguments;
if (__args.length === 0) {
return j_writeStream["writeQueueFull()"]() ;
} else if (typeof __super_writeQueueFull != 'undefined') {
return __super_writeQueueFull.apply(this, __args);
}
else throw new TypeError('function invoked with invalid arguments');
};
/**
Set a drain handler on the stream. If the write queue is full, then the handler will be called when the write
queue is ready to accept buffers again. See {@link Pump} for an example of this being used.
<p/>
The stream implementation defines when the drain handler, for example it could be when the queue size has been
reduced to <code>maxSize / 2</code>.
@public
@param handler {function} the handler
@return {WriteStream} a reference to this, so the API can be used fluently
*/
this.drainHandler = function(handler) {
var __args = arguments;
if (__args.length === 1 && (typeof __args[0] === 'function' || __args[0] == null)) {
j_writeStream["drainHandler(io.vertx.core.Handler)"](handler) ;
return that;
} else if (typeof __super_drainHandler != 'undefined') {
return __super_drainHandler.apply(this, __args);
}
else throw new TypeError('function invoked with invalid arguments');
};
// A reference to the underlying Java delegate
// NOTE! This is an internal API and must not be used in user code.
// If you rely on this property your code is likely to break if we change it / remove it without warning.
this._jdel = j_writeStream;
};
WriteStream._jclass = utils.getJavaClass("io.vertx.core.streams.WriteStream");
WriteStream._jtype = {accept: function(obj) {
return WriteStream._jclass.isInstance(obj._jdel);
},wrap: function(jdel) {
var obj = Object.create(WriteStream.prototype, {});
WriteStream.apply(obj, arguments);
return obj;
},
unwrap: function(obj) {
return obj._jdel;
}
};
WriteStream._create = function(jdel) {var obj = Object.create(WriteStream.prototype, {});
WriteStream.apply(obj, arguments);
return obj;
}
module.exports = WriteStream;