
io.vertx.groovy.core.streams.WriteStream.groovy Maven / Gradle / Ivy
/*
* 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.
*/
package io.vertx.groovy.core.streams;
import groovy.transform.CompileStatic
import io.vertx.lang.groovy.InternalHelper
import io.vertx.core.json.JsonObject
import io.vertx.core.Handler
/**
*
* Represents a stream of data that can be written to.
*
* Any class that implements this interface can be used by a {@link io.vertx.groovy.core.streams.Pump} to pump data from a ReadStream
* to it.
*/
@CompileStatic
public interface WriteStream extends StreamBase {
public Object getDelegate();
WriteStream exceptionHandler(Handler handler);
WriteStream write(T data);
void end();
void end(T t);
WriteStream setWriteQueueMaxSize(int maxSize);
boolean writeQueueFull();
WriteStream drainHandler(Handler handler);
}
@CompileStatic
class WriteStreamImpl implements WriteStream {
private final def io.vertx.core.streams.WriteStream delegate;
public WriteStreamImpl(Object delegate) {
this.delegate = (io.vertx.core.streams.WriteStream) delegate;
}
public Object getDelegate() {
return delegate;
}
/**
* Set an exception handler on the write stream.
* @param handler the exception handler
* @return a reference to this, so the API can be used fluently
*/
public WriteStream exceptionHandler(Handler handler) {
( /* Work around for https://jira.codehaus.org/browse/GROOVY-6970 */ (io.vertx.core.streams.WriteStream) this.delegate).exceptionHandler(handler);
return this;
}
/**
* Write some data to the stream. The data is put on an internal write queue, and the write actually happens
* asynchronously. To avoid running out of memory by putting too much on the write queue,
* check the {@link io.vertx.groovy.core.streams.WriteStream#writeQueueFull} method before writing. This is done automatically if using a {@link io.vertx.groovy.core.streams.Pump}.
* @param data the data to write
* @return a reference to this, so the API can be used fluently
*/
public WriteStream write(T data) {
((io.vertx.core.streams.WriteStream) this.delegate).write(InternalHelper.unwrapObject(data));
return this;
}
/**
* Ends the stream.
*
* Once the stream has ended, it cannot be used any more.
*/
public void end() {
((io.vertx.core.streams.WriteStream) this.delegate).end();
}
/**
* Same as {@link io.vertx.groovy.core.streams.WriteStream#end} but writes some data to the stream before ending.
* @param t
*/
public void end(T t) {
((io.vertx.core.streams.WriteStream) this.delegate).end(InternalHelper.unwrapObject(t));
}
/**
* Set the maximum size of the write queue to maxSize
. You will still be able to write to the stream even
* if there is more than maxSize
bytes in the write queue. This is used as an indicator by classes such as
* Pump
to provide flow control.
* @param maxSize the max size of the write stream
* @return a reference to this, so the API can be used fluently
*/
public WriteStream setWriteQueueMaxSize(int maxSize) {
((io.vertx.core.streams.WriteStream) this.delegate).setWriteQueueMaxSize(maxSize);
return this;
}
/**
* This will return true
if there are more bytes in the write queue than the value set using {@link io.vertx.groovy.core.streams.WriteStream#setWriteQueueMaxSize}
* @return true if write queue is full
*/
public boolean writeQueueFull() {
def ret = ((io.vertx.core.streams.WriteStream) this.delegate).writeQueueFull();
return ret;
}
/**
* Set a drain handler on the stream. If the write queue is full, then the handler will be called when the write
* queue has been reduced to maxSize / 2. See {@link io.vertx.groovy.core.streams.Pump} for an example of this being used.
* @param handler the handler
* @return a reference to this, so the API can be used fluently
*/
public WriteStream drainHandler(Handler handler) {
((io.vertx.core.streams.WriteStream) this.delegate).drainHandler(handler);
return this;
}
}