Advertisement
Chiddix

Mercury v0.0.1

Jul 29th, 2014
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.44 KB | None | 0 0
  1. package me.rabrg.mercury;
  2.  
  3. import java.io.DataInputStream;
  4. import java.io.DataOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.OutputStream;
  8. import java.lang.reflect.Field;
  9.  
  10. import me.rabrg.mercury.event.Event;
  11.  
  12. /**
  13.  *
  14.  * @author Ryan Greene
  15.  */
  16. public final class Mercury {
  17.  
  18.     private Class<Event>[] events;
  19.  
  20.     private int index;
  21.  
  22.     @SuppressWarnings("unchecked")
  23.     public Mercury() {
  24.         events = new Class[1];
  25.         index = 0;
  26.     }
  27.  
  28.     public void registerEvent(final Class<Event> event) {
  29.         if (index == events.length) {
  30.             @SuppressWarnings("unchecked")
  31.             final Class<Event>[] expanded = new Class[events.length + 1];
  32.             System.arraycopy(events, 0, expanded, 0, events.length);
  33.             events = expanded;
  34.         }
  35.         events[index++] = event;
  36.     }
  37.  
  38.     public void writeEvent(final Event event, final OutputStream outputStream) throws IllegalArgumentException, IllegalAccessException, IOException {
  39.         int opcode = -1;
  40.         for (int i = 0; i < events.length; i++) {
  41.             if (events[i] == event.getClass()) {
  42.                 opcode = i;
  43.                 break;
  44.             }
  45.         }
  46.         if (opcode == -1) {
  47.             throw new IllegalArgumentException("The specified event is not registered: " + event);
  48.         }
  49.         final DataOutputStream output = new DataOutputStream(outputStream);
  50.         output.writeByte(opcode);
  51.         for (final Field field : event.getClass().getDeclaredFields()) {
  52.             final Class<?> type = field.getType();
  53.             if (type == byte.class) {
  54.                 output.writeInt(field.getByte(field));
  55.             } else if (type == short.class) {
  56.                 output.writeShort(field.getShort(field));
  57.             } else if (type == int.class) {
  58.                 output.writeInt(field.getInt(field));
  59.             } else if (type == long.class) {
  60.                 output.writeLong(field.getLong(field));
  61.             } else if (type == float.class) {
  62.                 output.writeFloat(field.getFloat(field));
  63.             } else if (type == double.class) {
  64.                 output.writeDouble(field.getDouble(field));
  65.             } else if (type == boolean.class) {
  66.                 output.writeBoolean(field.getBoolean(field));
  67.             } else if (type == char.class) {
  68.                 output.writeChar(field.getChar(field));
  69.             } else if (type == String.class) {
  70.                 output.writeUTF((String) field.get(field));
  71.             }
  72.         }
  73.         output.flush();
  74.     }
  75.  
  76.     public Event readEvent(final InputStream inputStream) throws InstantiationException, IllegalAccessException, IOException {
  77.         final DataInputStream input = new DataInputStream(inputStream);
  78.         Event event = null;
  79.         try {
  80.             event = events[input.readByte()].newInstance();
  81.         } catch (final Exception e) {
  82.             throw new IllegalStateException("The input stream didn't correctly contain a registered event");
  83.         }
  84.         for (final Field field : event.getClass().getDeclaredFields()) {
  85.             final Class<?> type = field.getType();
  86.             if (type == byte.class) {
  87.                 field.setByte(event, input.readByte());
  88.             } else if (type == short.class) {
  89.                 field.setShort(event, input.readShort());
  90.             } else if (type == int.class) {
  91.                 field.setInt(event, input.readInt());
  92.             } else if (type == long.class) {
  93.                 field.setLong(event, input.readLong());
  94.             } else if (type == float.class) {
  95.                 field.setFloat(event, input.readFloat());
  96.             } else if (type == double.class) {
  97.                 field.setDouble(event, input.readDouble());
  98.             } else if (type == boolean.class) {
  99.                 field.setBoolean(event, input.readBoolean());
  100.             } else if (type == char.class) {
  101.                 field.setChar(event, input.readChar());
  102.             } else if (type == String.class) {
  103.                 field.set(event, input.readUTF());
  104.             }
  105.         }
  106.         return event;
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement