Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.rabrg.mercury;
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.lang.reflect.Field;
- import me.rabrg.mercury.event.Event;
- /**
- *
- * @author Ryan Greene
- */
- public final class Mercury {
- private Class<Event>[] events;
- private int index;
- @SuppressWarnings("unchecked")
- public Mercury() {
- events = new Class[1];
- index = 0;
- }
- public void registerEvent(final Class<Event> event) {
- if (index == events.length) {
- @SuppressWarnings("unchecked")
- final Class<Event>[] expanded = new Class[events.length + 1];
- System.arraycopy(events, 0, expanded, 0, events.length);
- events = expanded;
- }
- events[index++] = event;
- }
- public void writeEvent(final Event event, final OutputStream outputStream) throws IllegalArgumentException, IllegalAccessException, IOException {
- int opcode = -1;
- for (int i = 0; i < events.length; i++) {
- if (events[i] == event.getClass()) {
- opcode = i;
- break;
- }
- }
- if (opcode == -1) {
- throw new IllegalArgumentException("The specified event is not registered: " + event);
- }
- final DataOutputStream output = new DataOutputStream(outputStream);
- output.writeByte(opcode);
- for (final Field field : event.getClass().getDeclaredFields()) {
- final Class<?> type = field.getType();
- if (type == byte.class) {
- output.writeInt(field.getByte(field));
- } else if (type == short.class) {
- output.writeShort(field.getShort(field));
- } else if (type == int.class) {
- output.writeInt(field.getInt(field));
- } else if (type == long.class) {
- output.writeLong(field.getLong(field));
- } else if (type == float.class) {
- output.writeFloat(field.getFloat(field));
- } else if (type == double.class) {
- output.writeDouble(field.getDouble(field));
- } else if (type == boolean.class) {
- output.writeBoolean(field.getBoolean(field));
- } else if (type == char.class) {
- output.writeChar(field.getChar(field));
- } else if (type == String.class) {
- output.writeUTF((String) field.get(field));
- }
- }
- output.flush();
- }
- public Event readEvent(final InputStream inputStream) throws InstantiationException, IllegalAccessException, IOException {
- final DataInputStream input = new DataInputStream(inputStream);
- Event event = null;
- try {
- event = events[input.readByte()].newInstance();
- } catch (final Exception e) {
- throw new IllegalStateException("The input stream didn't correctly contain a registered event");
- }
- for (final Field field : event.getClass().getDeclaredFields()) {
- final Class<?> type = field.getType();
- if (type == byte.class) {
- field.setByte(event, input.readByte());
- } else if (type == short.class) {
- field.setShort(event, input.readShort());
- } else if (type == int.class) {
- field.setInt(event, input.readInt());
- } else if (type == long.class) {
- field.setLong(event, input.readLong());
- } else if (type == float.class) {
- field.setFloat(event, input.readFloat());
- } else if (type == double.class) {
- field.setDouble(event, input.readDouble());
- } else if (type == boolean.class) {
- field.setBoolean(event, input.readBoolean());
- } else if (type == char.class) {
- field.setChar(event, input.readChar());
- } else if (type == String.class) {
- field.set(event, input.readUTF());
- }
- }
- return event;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement