
org.swisspush.reststorage.s3.FileWriteStream Maven / Gradle / Ivy
package org.swisspush.reststorage.s3;
import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.Promise;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.streams.WriteStream;
import org.slf4j.Logger;
import java.io.OutputStream;
import java.io.IOException;
import static org.slf4j.LoggerFactory.getLogger;
public class FileWriteStream implements WriteStream {
private static final Logger log = getLogger(FileWriteStream.class);
private final OutputStream outputStream;
private Handler exceptionHandler;
private Handler drainHandler;
private boolean writeQueueFull = false;
private int maxQueueSize = 1024; // Default max queue size, can be modified
public FileWriteStream(OutputStream outputStream) {
this.outputStream = outputStream;
}
@Override
public WriteStream exceptionHandler(Handler handler) {
this.exceptionHandler = handler;
return this;
}
@Override
public void write(Buffer buffer, Handler> handler) {
try {
outputStream.write(buffer.getBytes());
outputStream.flush();
handler.handle(Future.succeededFuture());
} catch (IOException e) {
if (exceptionHandler != null) {
exceptionHandler.handle(e);
}
log.error("Error writing to stream", e);
handler.handle(Future.failedFuture(e));
}
}
@Override
public Future write(Buffer buffer) {
Promise promise = Promise.promise();
try {
outputStream.write(buffer.getBytes());
outputStream.flush();
promise.complete();
} catch (IOException e) {
if (exceptionHandler != null) {
exceptionHandler.handle(e);
}
log.error("Error writing to stream", e);
promise.fail(e);
}
return promise.future();
}
@Override
public void end(Handler> handler) {
try {
outputStream.flush();
outputStream.close();
handler.handle(Future.succeededFuture());
} catch (IOException e) {
if (exceptionHandler != null) {
exceptionHandler.handle(e);
}
log.error("Error when try to end stream", e);
handler.handle(Future.failedFuture(e));
}
}
@Override
public Future end() {
Promise promise = Promise.promise();
try {
outputStream.flush();
outputStream.close();
promise.complete();
} catch (IOException e) {
if (exceptionHandler != null) {
exceptionHandler.handle(e);
}
log.error("Error when try to end stream", e);
promise.fail(e);
}
return promise.future();
}
@Override
public WriteStream setWriteQueueMaxSize(int maxSize) {
this.maxQueueSize = maxSize;
return this;
}
@Override
public boolean writeQueueFull() {
// Implement custom logic to manage queue size if needed
return writeQueueFull;
}
@Override
public WriteStream drainHandler(Handler handler) {
this.drainHandler = handler;
return this;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy