|
|
@@ -9,7 +9,6 @@ package net.ranides.assira.io;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.io.OutputStream;
|
|
|
-import java.util.Arrays;
|
|
|
import net.ranides.assira.annotations.Meta;
|
|
|
import net.ranides.assira.events.*;
|
|
|
|
|
|
@@ -37,28 +36,28 @@ public final class OutputStreamThread extends OutputStream {
|
|
|
|
|
|
@Override
|
|
|
public void write(int data) {
|
|
|
- router.signalEvent(new IOWriteEvent(data));
|
|
|
+ router.signalEvent(IOEvent.write(data));
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void write(byte[] data) {
|
|
|
- router.signalEvent(new IOWriteArrayEvent(data));
|
|
|
+ router.signalEvent(IOEvent.write(data));
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void write(byte[] data, int offset, int length) {
|
|
|
- router.signalEvent(new IOWriteArrayEvent(data, offset, length));
|
|
|
+ router.signalEvent(IOEvent.write(data, offset, length));
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void close() {
|
|
|
- router.signalEvent(CLOSE);
|
|
|
+ router.signalEvent(IOEvent.close());
|
|
|
router.dispose();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void flush() {
|
|
|
- router.signalEvent(FLUSH);
|
|
|
+ router.signalEvent(IOEvent.flush());
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -73,14 +72,14 @@ public final class OutputStreamThread extends OutputStream {
|
|
|
* </p>
|
|
|
* @param handler
|
|
|
*/
|
|
|
- public void onError(EventListener<IOExceptionEvent> handler) {
|
|
|
- listeners.addEventListener(IOExceptionEvent.class, handler);
|
|
|
+ public void onError(EventListener<IOEvent.Failure> handler) {
|
|
|
+ listeners.addEventListener(IOEvent.Failure.class, handler);
|
|
|
}
|
|
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
|
@Meta.EventHandler
|
|
|
- void handleEvent(IOCloseEvent event) {
|
|
|
+ void handleEvent(IOEvent.Close event) {
|
|
|
try {
|
|
|
ostream.close();
|
|
|
} catch (IOException ex) {
|
|
|
@@ -89,7 +88,7 @@ public final class OutputStreamThread extends OutputStream {
|
|
|
}
|
|
|
|
|
|
@Meta.EventHandler
|
|
|
- void handleEvent(IOFlushEvent event) {
|
|
|
+ void handleEvent(IOEvent.Flush event) {
|
|
|
try {
|
|
|
ostream.flush();
|
|
|
} catch (IOException ex) {
|
|
|
@@ -98,7 +97,7 @@ public final class OutputStreamThread extends OutputStream {
|
|
|
}
|
|
|
|
|
|
@Meta.EventHandler
|
|
|
- void handleEvent(IOWriteArrayEvent event) {
|
|
|
+ void handleEvent(IOEvent.WriteByteArray event) {
|
|
|
try {
|
|
|
ostream.write(event.data());
|
|
|
} catch (IOException ex) {
|
|
|
@@ -107,7 +106,7 @@ public final class OutputStreamThread extends OutputStream {
|
|
|
}
|
|
|
|
|
|
@Meta.EventHandler
|
|
|
- void handleEvent(IOWriteEvent event) {
|
|
|
+ void handleEvent(IOEvent.WriteByte event) {
|
|
|
try {
|
|
|
ostream.write(event.data());
|
|
|
} catch (IOException ex) {
|
|
|
@@ -116,74 +115,7 @@ public final class OutputStreamThread extends OutputStream {
|
|
|
}
|
|
|
|
|
|
void handleError(IOException cause) {
|
|
|
- listeners.signalEvent(new IOExceptionEvent(ostream, cause));
|
|
|
- }
|
|
|
-
|
|
|
-/* ************************************************************************** */
|
|
|
-
|
|
|
-
|
|
|
- private static final IOCloseEvent CLOSE = new IOCloseEvent();
|
|
|
-
|
|
|
- private static final IOCloseEvent FLUSH = new IOCloseEvent();
|
|
|
-
|
|
|
- @SuppressWarnings("PMD")
|
|
|
- private static class IOEvent implements Event { }
|
|
|
-
|
|
|
- private static class IOCloseEvent extends IOEvent { };
|
|
|
-
|
|
|
- private static class IOFlushEvent extends IOEvent { };
|
|
|
-
|
|
|
- private static class IOWriteArrayEvent extends IOEvent {
|
|
|
- private final byte[] data;
|
|
|
-
|
|
|
- public IOWriteArrayEvent(byte[] data) {
|
|
|
- this.data = Arrays.copyOf(data, data.length);
|
|
|
- }
|
|
|
- public IOWriteArrayEvent(byte[] data, int offset, int length) {
|
|
|
- this.data = Arrays.copyOfRange(data, offset, offset+length);
|
|
|
- }
|
|
|
- public byte[] data() {
|
|
|
- return data; // NOPMD
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private static class IOWriteEvent extends IOEvent {
|
|
|
- private final int data;
|
|
|
-
|
|
|
- public IOWriteEvent(int data) {
|
|
|
- this.data = data;
|
|
|
- }
|
|
|
- public int data() {
|
|
|
- return data;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Zdarzenie wysyłane po wystąpieniu wyjątku IOException do obserwatorów
|
|
|
- * zarejestrowanych za pomocą metody {@link #onError}
|
|
|
- */
|
|
|
- public static class IOExceptionEvent extends IOEvent {
|
|
|
- private final OutputStream stream;
|
|
|
- private final IOException cause;
|
|
|
-
|
|
|
- private IOExceptionEvent(OutputStream stream, IOException cause) {
|
|
|
- this.stream = stream;
|
|
|
- this.cause = cause;
|
|
|
- }
|
|
|
- /**
|
|
|
- * Napotkany wyjątek
|
|
|
- * @return
|
|
|
- */
|
|
|
- public IOException cause() {
|
|
|
- return cause;
|
|
|
- }
|
|
|
- /**
|
|
|
- * Strumień, który spowodował wyjątek
|
|
|
- * @return
|
|
|
- */
|
|
|
- public OutputStream stream() {
|
|
|
- return stream;
|
|
|
- }
|
|
|
+ listeners.signalEvent(IOEvent.failure(cause));
|
|
|
}
|
|
|
|
|
|
}
|