private Class<?> readType(ByteBuffer buffer) throws IOException{ SerializerRegistration reg = Serializer.readClass(buffer); if (reg == null){ // either "void" or unknown val short id = buffer.getShort(buffer.position()-2); if (id == 0){ return void.class; } else{ logger.log(Level.WARNING, "Undefined class ID: {0}", id); throw new IOException(); // prevents message from being serialized } } return reg.getType(); }
public Registration( SerializerRegistration reg ) { this.id = reg.getId(); this.className = reg.getType().getName(); if( reg.getSerializer().getClass() != FieldSerializer.class ) { this.serializerClassName = reg.getSerializer().getClass().getName(); } }
public static void compile() { // Let's just see what they are here List<Registration> list = new ArrayList<Registration>(); for( SerializerRegistration reg : Serializer.getSerializerRegistrations() ) { Class type = reg.getType(); if( ignore.contains(type) ) continue; if( type.isPrimitive() ) continue; list.add(new Registration(reg)); } if( log.isLoggable(Level.FINE) ) { log.log( Level.FINE, "Number of registered classes:{0}", list.size()); for( Registration reg : list ) { log.log( Level.FINE, " {0}", reg); } } compiled = list.toArray(new Registration[list.size()]); INSTANCE = new SerializerRegistrationsMessage(compiled); Serializer.setReadOnly(true); }
/** * Write the class and object. * * @param buffer The buffer to write to. * @param object The object to write. * @throws IOException If serializing fails. */ public static void writeClassAndObject(ByteBuffer buffer, Object object) throws IOException { if (object == null) { buffer.putShort((short)-1); return; } SerializerRegistration reg = writeClass(buffer, object.getClass()); // If the caller (or us) has registered a generic base class (like Enum) // that is meant to steer automatic resolution for things like FieldSerializer // that have final classes in fields... then there are cases where the exact // type isn't known by the outer class. (Think of a message object // that has an Object field but tries to send an Enum subclass in it.) // In that case, the SerializerRegistration object we get back isn't // really going to be capable of recreating the object on the other // end because it won't know what class to use. This only comes up // in writeclassAndObejct() because we just wrote an ID to a more generic // class than will be readable on the other end. The check is simple, though. if( reg.getType() != object.getClass() ) { throw new IllegalArgumentException("Class has not been registered:" + object.getClass() + " but resolved to generic serializer for:" + reg.getType()); } reg.getSerializer().writeObject(buffer, object); }
@SuppressWarnings("unchecked") public <T> T readObject(ByteBuffer data, Class<T> c) throws IOException { int length = data.getInt(); Collection collection; try { collection = (Collection)c.newInstance(); } catch (Exception e) { log.log(Level.FINE, "[Serializer][???] Could not determine collection type. Using ArrayList."); collection = new ArrayList(length); } if (length == 0) return (T)collection; if (data.get() == (byte)1) { SerializerRegistration reg = Serializer.readClass(data); Class clazz = reg.getType(); Serializer serializer = reg.getSerializer(); for (int i = 0; i != length; ++i) { collection.add(serializer.readObject(data, clazz)); } } else { for (int i = 0; i != length; ++i) { collection.add(Serializer.readClassAndObject(data)); } } return (T)collection; }
/** * Read the class and the object. * * @param buffer Buffer to read from. * @return The Object that was read. * @throws IOException If serialization failed. */ @SuppressWarnings("unchecked") public static Object readClassAndObject(ByteBuffer buffer) throws IOException { SerializerRegistration reg = readClass(buffer); if (reg == NULL_CLASS) return null; if (reg == null) throw new SerializerException( "Class not found for buffer data." ); return reg.getSerializer().readObject(buffer, reg.getType()); }
private Class<?> readType(ByteBuffer buffer) throws IOException{ SerializerRegistration reg = Serializer.readClass(buffer); if (reg == null){ // either "void" or unknown val short id = buffer.getShort(buffer.position()-2); if (id == 0){ return void.class; } else{ logger.log(Level.WARNING, "Undefined class ID: {0}", id); throw new IOException(); // prevents message from being serialized } } return reg.getType(); }
private Class<?> readType(ByteBuffer buffer) throws IOException{ SerializerRegistration reg = Serializer.readClass(buffer); if (reg == null){ // either "void" or unknown val short id = buffer.getShort(buffer.position()-2); if (id == 0){ return void.class; } else{ logger.log(Level.WARNING, "Undefined class ID: {0}", id); throw new IOException(); // prevents message from being serialized } } return reg.getType(); }
@Override public <T> T readObject( ByteBuffer data, Class<T> c ) throws IOException { // Use serializer's support for class reading/writing SerializerRegistration reg = readClass(data); if( reg == null || reg.getType() == Void.class ) { return null; } Class type = reg.getType(); String name = delegate.readObject( data, String.class ); try { Field result = type.getDeclaredField(name); return c.cast(result); } catch( NoSuchFieldException e ) { throw new IOException( "Error resolving field:" + name + " on:" + type, e ); } }
public Registration( SerializerRegistration reg ) { this.id = reg.getId(); this.className = reg.getType().getName(); if( reg.getSerializer().getClass() != FieldSerializer.class ) { this.serializerClassName = reg.getSerializer().getClass().getName(); } }
public static void compile() { // Let's just see what they are here List<Registration> list = new ArrayList<Registration>(); for( SerializerRegistration reg : Serializer.getSerializerRegistrations() ) { Class type = reg.getType(); if( ignore.contains(type) ) continue; if( type.isPrimitive() ) continue; list.add(new Registration(reg)); } if( log.isLoggable(Level.FINE) ) { log.log( Level.FINE, "Number of registered classes:{0}", list.size()); for( Registration reg : list ) { log.log( Level.FINE, " {0}", reg); } } compiled = list.toArray(new Registration[list.size()]); INSTANCE = new SerializerRegistrationsMessage(compiled); Serializer.setReadOnly(true); }
/** * Write the class and object. * * @param buffer The buffer to write to. * @param object The object to write. * @throws IOException If serializing fails. */ public static void writeClassAndObject(ByteBuffer buffer, Object object) throws IOException { if (object == null) { buffer.putShort((short)-1); return; } SerializerRegistration reg = writeClass(buffer, object.getClass()); // If the caller (or us) has registered a generic base class (like Enum) // that is meant to steer automatic resolution for things like FieldSerializer // that have final classes in fields... then there are cases where the exact // type isn't known by the outer class. (Think of a message object // that has an Object field but tries to send an Enum subclass in it.) // In that case, the SerializerRegistration object we get back isn't // really going to be capable of recreating the object on the other // end because it won't know what class to use. This only comes up // in writeclassAndObejct() because we just wrote an ID to a more generic // class than will be readable on the other end. The check is simple, though. if( reg.getType() != object.getClass() ) { throw new IllegalArgumentException("Class has not been registered:" + object.getClass() + " but resolved to generic serializer for:" + reg.getType()); } reg.getSerializer().writeObject(buffer, object); }
public <T> T readObject( ByteBuffer data, Class<T> c ) throws IOException { // Use serializer's support for class reading/writing SerializerRegistration reg = readClass(data); if( reg == null ) { throw new SerializerException("Class not found for buffer data."); } if( reg.getId() == -1 ) { return null; } return c.cast(reg.getType()); }
@SuppressWarnings("unchecked") public <T> T readObject(ByteBuffer data, Class<T> c) throws IOException { int length = data.getInt(); Collection collection; try { collection = (Collection)c.newInstance(); } catch (Exception e) { log.log(Level.FINE, "[Serializer][???] Could not determine collection type. Using ArrayList."); collection = new ArrayList(length); } if (length == 0) return (T)collection; if (data.get() == (byte)1) { SerializerRegistration reg = Serializer.readClass(data); Class clazz = reg.getType(); Serializer serializer = reg.getSerializer(); for (int i = 0; i != length; ++i) { collection.add(serializer.readObject(data, clazz)); } } else { for (int i = 0; i != length; ++i) { collection.add(Serializer.readClassAndObject(data)); } } return (T)collection; }
@SuppressWarnings("unchecked") public <T> T readObject(ByteBuffer data, Class<T> c) throws IOException { int length = data.getInt(); Collection collection; try { collection = (Collection)c.newInstance(); } catch (Exception e) { log.log(Level.FINE, "[Serializer][???] Could not determine collection type. Using ArrayList."); collection = new ArrayList(length); } if (length == 0) return (T)collection; if (data.get() == (byte)1) { SerializerRegistration reg = Serializer.readClass(data); Class clazz = reg.getType(); Serializer serializer = reg.getSerializer(); for (int i = 0; i != length; ++i) { collection.add(serializer.readObject(data, clazz)); } } else { for (int i = 0; i != length; ++i) { collection.add(Serializer.readClassAndObject(data)); } } return (T)collection; }
/** * Read the class and the object. * * @param buffer Buffer to read from. * @return The Object that was read. * @throws IOException If serialization failed. */ @SuppressWarnings("unchecked") public static Object readClassAndObject(ByteBuffer buffer) throws IOException { SerializerRegistration reg = readClass(buffer); if (reg == NULL_CLASS) return null; if (reg == null) throw new SerializerException( "Class not found for buffer data." ); return reg.getSerializer().readObject(buffer, reg.getType()); }
/** * Read the class and the object. * * @param buffer Buffer to read from. * @return The Object that was read. * @throws IOException If serialization failed. */ @SuppressWarnings("unchecked") public static Object readClassAndObject(ByteBuffer buffer) throws IOException { SerializerRegistration reg = readClass(buffer); if (reg == NULL_CLASS) return null; if (reg == null) throw new SerializerException( "Class not found for buffer data." ); return reg.getSerializer().readObject(buffer, reg.getType()); }