@Override public void stop() { // Clear class cache marshallableTypeHints.clear(); marshallerTL.clear(); }
public BufferSizePredictor getBufferSizePredictor(Object obj) { return obj == null ? NullBufferSizePredictor.INSTANCE : getBufferSizePredictor(obj.getClass()); }
@Override public boolean isMarshallable(Object o) throws Exception { Class<?> clazz = o.getClass(); boolean containsMarshallable = marshallableTypeHints.isKnownMarshallable(clazz); if (containsMarshallable) { boolean marshallable = marshallableTypeHints.isMarshallable(clazz); if (trace) log.tracef("Marshallable type '%s' known and is marshallable=%b", clazz.getName(), marshallable); return marshallable; } else { if (isMarshallableCandidate(o)) { boolean isMarshallable = true; try { objectToBuffer(o); } catch (Exception e) { isMarshallable = false; throw e; } finally { marshallableTypeHints.markMarshallable(clazz, isMarshallable); } return isMarshallable; } return false; } }
/** * Marks a particular type as being marshallable or not being not marshallable. * * @param type Class to mark as serializable or non-serializable * @param isMarshallable Whether the type can be marshalled or not. */ public void markMarshallable(Class<?> type, boolean isMarshallable) { MarshallingType marshallType = typeHints.get(type); if (marshallableUpdateRequired(isMarshallable, marshallType)) { boolean replaced = typeHints.replace(type, marshallType, new MarshallingType( Boolean.valueOf(isMarshallable), marshallType.sizePredictor)); if (replaced && trace) { log.tracef("Replacing '%s' type to be marshallable=%b", type.getName(), isMarshallable); } } else if (marshallType == null) { if (trace) { log.tracef("Cache '%s' type to be marshallable=%b", type.getName(), isMarshallable); } typeHints.put(type, new MarshallingType( Boolean.valueOf(isMarshallable), new AdaptiveBufferSizePredictor())); } }
@Override public BufferSizePredictor getBufferSizePredictor(Object o) { return marshallableTypeHints.getBufferSizePredictor(o.getClass()); }
@Override public byte[] objectToByteBuffer(Object o) throws IOException, InterruptedException { if (o != null) { BufferSizePredictor sizePredictor = marshallableTypeHints .getBufferSizePredictor(o.getClass()); byte[] bytes = objectToByteBuffer(o, sizePredictor.nextSize(o)); sizePredictor.recordSize(bytes.length); return bytes; } else { return objectToByteBuffer(null, 1); } }
@Override public ByteBuffer objectToBuffer(Object obj) throws IOException, InterruptedException { if (obj != null) { BufferSizePredictor sizePredictor = marshallableTypeHints .getBufferSizePredictor(obj.getClass()); int estimatedSize = sizePredictor.nextSize(obj); ByteBuffer byteBuffer = objectToBuffer(obj, estimatedSize); int length = byteBuffer.getLength(); // If the prediction is way off, then trim it if (estimatedSize > (length * 4)) { byte[] buffer = trimBuffer(byteBuffer); byteBuffer = new ByteBufferImpl(buffer, 0, buffer.length); } sizePredictor.recordSize(length); return byteBuffer; } else { return objectToBuffer(null, 1); } }